|
3 | 3 | [](https://travis-ci.org/joernb/react-scripts-with-ssr)
|
4 | 4 |
|
5 | 5 | This is a fork of [react-scripts](https://github.com/facebook/create-react-app/tree/master/packages/react-scripts), which adds support for server-side rendering (SSR).
|
6 |
| -react-scripts is a part of [Create React App](https://github.com/facebook/create-react-app), which can be used with a customized version of react-scripts. |
| 6 | + |
| 7 | +`react-scripts` is a part of [Create React App](https://github.com/facebook/create-react-app), which can be used with a customized version of `react-scripts`. |
| 8 | + |
| 9 | +## Getting Started |
| 10 | + |
| 11 | +Install create-react-app: |
| 12 | +```sh |
| 13 | +npm i -g create-react-app |
| 14 | +``` |
| 15 | + |
| 16 | +Generate a new project (add `--typescript` for TypeScript support): |
| 17 | +```sh |
| 18 | +npx create-react-app my-app --scripts-version react-scripts-with-ssr |
| 19 | +cd my-app |
| 20 | +``` |
| 21 | + |
| 22 | +Start a local webpack dev server with integrated server-side rendering: |
| 23 | +```sh |
| 24 | +npm start |
| 25 | +``` |
| 26 | + |
| 27 | +Build a production version and test server-side rendering locally: |
| 28 | +```sh |
| 29 | +npm run build |
| 30 | +npm run serve |
| 31 | +``` |
| 32 | + |
| 33 | +## How it works |
| 34 | + |
| 35 | +The script will generate an additional entry point for server-side rendering in `src/index.ssr.js` (or `src/index.ssr.tsx`). It exports an express-style request handler, that looks like this: |
| 36 | + |
| 37 | +```js |
| 38 | +export default (request, response) => { |
| 39 | + // ... |
| 40 | + response.status(200).send("..."); |
| 41 | +}; |
| 42 | +``` |
| 43 | + |
| 44 | +Webpack will compile this entry point as a separate library `build/ssr.js`. In production, that library may be imported by an executable node script, which sets up an express server and plugs in the request handler. Such a script is provided as an example at `react-scripts-with-ssr/scripts/serve.js`, which you may start with `npm run serve` to test your production builds. That script however is not part of the compilation. It is up to you to integrate the request handler into your server-side runtime environment. |
| 45 | + |
| 46 | +During development, the request handler will be integrated into the local webpack dev server. Start it with `npm start` and open `http://localhost:3000` to see the server-side rendered output. |
| 47 | + |
| 48 | +If `src/index.ssr.js` exports a function called `devServerHandler`, it will be invoked and its return value will be used as a request handler during development instead. This gives your entry point the possibility to make environment-specific adjustments. For example, all assets are compiled in-memory during development and should not be served from the real file system. |
| 49 | + |
| 50 | +## Contribute |
| 51 | + |
| 52 | +### Merge react-scripts updates |
| 53 | + |
| 54 | +Clone create-react-app and create a subtree branch for react-scripts: |
| 55 | + |
| 56 | +```sh |
| 57 | +git clone git@github.com:facebook/create-react-app.git create-react-app |
| 58 | +cd create-react-app |
| 59 | +git checkout react-scripts@2.1.3 |
| 60 | +git subtree split -P packages/react-scripts -b react-scripts-v2.1.3 |
| 61 | +``` |
| 62 | + |
| 63 | +Merge the subtree branch into this project: |
| 64 | + |
| 65 | +```sh |
| 66 | +git remote add upstream ../create-react-app |
| 67 | +git merge upstream react-scripts-v2.1.3 |
| 68 | +``` |
0 commit comments