Lightweight boilerplate project to setup a React 17 web application on AWS Lambda using the Serverless Framework.
- Universal app; server-side rendering with dynamic configuration context passed from backend to browser.
- Self-contained; no additional setup steps necessary other than running
npx sls deploy
. - Lightweight; no mandatory
redux
,react-router
,sass
,less
or any other 3rd party dependency for full flexibility. - React "Fast Refresh" (previously known as "Hot Reloading") using the React Refresh Webpack Plugin.
- Built-in support for code splitting and tree shaking to optimize page loading times.
- Full TypeScript support using Babel 7 and Webpack 5, including custom module resolution.
- Working Jest test environment.
Looking for the plain JavaScript version of this boilerplate?
The idea is that we use AWS Lambda to serve the dynamic part of our app, the server-side logic, and perform the server-side rendering. For all static data like images, stylesheets and even the app's index.tsx
that is loaded in the browser, we use an S3 bucket for public hosting.
This combination makes our app fast and incredibly scalable. AWS will spin up new Lambda instances once your number of users increases, handling even the largest spikes fully automatically, while incurring virtually no costs when your app isn't used. At the same time S3 provides a robust and fast platform for your static content so you don't have to waste your own computing resources.
All resources, including the S3 bucket for hosting static content, are created and configured automatically when your app is deployed the first time. You can make changes to the default setup by updating your serverless.yml
to your linking.
serverless-react-boilerplate/
│
├── public/ - Public assets such as images
│ ├── favicon.ico - Favicon
│ └── manifest.json - Web page manifest
│
├── src/
│ ├── browser/
│ │ └── ... - Client-side code running in the browser as well as during server-side rendering
│ ├── components/
│ │ └── ... - React components
│ └── server/
│ └── ... - Server-side code running on AWS Lambda
│
├── handler.ts - AWS Lambda function handler
├── serverless.yaml - Project configuration
├── babel.config.js - Babel configuration
├── jest.config.js - Jest configuration
├── webpack.browser.config.js - Webpack configuration for client-side code
├── webpack.server.config.js - Webpack configuration for the Lambda backend
└── ...
The project is based on the Serverless Framework and makes use of a number of plugins:
- Webpack Plugin - We use Webpack for packaging our sources.
- Offline Plugin - The Serverless Offline Plugin allows you to run Serverless applications locally as if they would be deployed on AWS. This is especially helpful for testing web applications and APIs without having to deploy them anywhere.
- Scripts Plugin - Run shell scripts as part of your Serverless workflow
- S3 Deploy Plugin - Deploy files to S3 buckets. This is used for uploading static content like images and the generated
main.js
.
Though we use the same source code for both the server-side and browser rendering, the project will be packaged into two distinct bundles:
- Backend code running on AWS Lambda. The main entry point is
./src/server/render.tsx
. It contains the handler function that is invoked by AWS Lambda.The packaging is controlled bywebpack.server.config.js
and optimized for Node.js 12. - Frontend code hosted in an S3 bucket and loaded by the browser. Main entry point is
./src/browser/index.tsx
. It's packaged using thewebpack.browser.config.js
, optimized for web browsers. The output files will have their content hash added to their names to enable long term caching in the browser.
webpack.browser.config.js
defines some default code splitting settings that optimize browser loading times and should make sense for most projects:
- Shared compoments (in the
src/components
folder) are loaded in a separatecomponents.js
chunk. - All external Node modules (in the
node_modules/
folder) are loaded in thevendor.js
chunk. External modules usually don't change as often as the rest of your application and this split will improve browser caching for your users. - The rest of the application is loaded in the
main.js
chunk.
Update the serverless.yaml
with your project name and additional resources you might need.
The frontend as well as the server-side code running on AWS Lambda share a common application configuration. Currently it is used for injecting the application name from the public/manifest.json
as well as setting the public host names. You can extend the configuration by adding your own variables to src/server/config.tsx
. They will become available in both your backend and frontend code via the useConfig
hook:
import useConfig from "../components/useConfig";
export default function MyComponent() {
const config = useConfig();
return (
// ...
)
}
I would recommend to create a separate Serverless service that provides the frontend with an API and protect it via Amazon Cognito, a custom Authorizer or even just an API Key. Mixing React with pure backend API functions is possible and perfectly fine, but in my experience it quickly becomes a hassle and you need to take special care not to leak anything to the browser that's not supposed to be known there.
The goal of this boilerplate is to offer a minimal setup that can be used as a basis for pretty much all your React needs. A lot of people love Redux, rely on React Router or need other external modules. I have intentionally left these out of the boilerplate code but it should be trivial to add them, following the standard documentation steps.
If you are interested in integrating with React Router, checkout out the Added React Router example configuration Pull Request.
Similar to the statement above, I have decided against integrating with a specific framework. The boilerplate uses plain and simple CSS and integrating another system should be easy enough.
To keep this repository lightweight no ESLint rules are included. There are many different plugins and people tend to prefer different coding styles. The existing code should be easily adaptable to any style you personally prefer. I recommend using Prettier to format your code automatically and a default configuration is already part of this repository, defined in package.json
. In addition, I recommend adding ESLint and Husky to your project to ensure your coding guidelines are followed.
You can test the setup locally. No direct access to AWS is needed. This allows developers to write and test code even if not everyone has full deployment access.
For local testing run the following command and open your web browser at http://localhost:3000/. Static content such as images will be served via the Webpack DevServer running on http://localhost:8080. Note that the app has to be deployed first before you will be able to run locally.
npm start
Testing is set up as well, using Jest and will execute all *.test.ts
and *.test.tsx
ffiles in the src/
directory:
npm test
The whole application can be deployed with a single command:
npx sls deploy
And finally to remove all AWS resources again run:
npx sls remove
This will delete all resources but the distribution S3 bucket. As it still contains the bundles you will have to delete it manually for now.
- Updated to React 17
- React "Fast Refresh" (previously known as "Hot Reloading") using the React Refresh Webpack Plugin.
- Built-in support for code splitting and tree shaking to optimize page loading times.
- Full TypeScript support using Babel 7 and Webpack 5, including custom module resolution.
- Handle server side errors more gracefully. Update
handler.ts
to add your own custom error handling code such as Youch. - Code cleanup and simplification