|
| 1 | +# Load performance optimizations |
| 2 | + |
| 3 | +## Works out of the box |
| 4 | + |
| 5 | +### Prerendered HTML |
| 6 | + |
| 7 | +`react-snap` prerenders HTML, so the browser can start to render content or download required resources ASAP. |
| 8 | + |
| 9 | +### preconnect for third-party resources |
| 10 | + |
| 11 | +`react-snap` tracks all third-party connections during rendering and will place appropriate preconnect links in the `header`. |
| 12 | + |
| 13 | +### If you are using code splitting feature of Webpack |
| 14 | + |
| 15 | +`react-snap` will remove chunk scripts from the HTML and instead will place preload links in the `header`. |
| 16 | + |
| 17 | +### If you are using CSS-in-JS solution |
| 18 | + |
| 19 | +`react-snap` will prerender all styles and save in the HTML. |
| 20 | + |
| 21 | +## Requires configuration |
| 22 | + |
| 23 | +This is a brief overview of what described in Readme and [recipes](recipes.md). |
| 24 | + |
| 25 | +### inlineCss |
| 26 | + |
| 27 | +With this configuration enabled `react-snap` will inline critical CSS and stylesheet links will be loaded in a nonblocking manner with the help of [loadCss](https://www.npmjs.com/package/fg-loadcss). |
| 28 | + |
| 29 | +### cacheAjaxRequests |
| 30 | + |
| 31 | +If you are doing AJAX requests (to the same domain), `react-snap` can cache this data in the window. Think of it as a poor man's Redux rehydration. |
| 32 | + |
| 33 | +### http2PushManifest |
| 34 | + |
| 35 | +`react-snap` can record all resources (scripts, styles, images) required for the page and write down this data to the JSON file. You can use this JSON file to generate HTTP2 server pushes or Link headers. |
| 36 | + |
| 37 | +### If you are using Redux |
| 38 | + |
| 39 | +Use `window.snapSaveState` callback to store Redux state, so it can be used to rehydrate on the client side. |
| 40 | + |
| 41 | +### If you are using loadable-components |
| 42 | + |
| 43 | +Use `window.snapSaveState` callback to store `loadable-components` state, so it can be used to rehydrate on the client side. |
| 44 | + |
| 45 | +### If you are using Apollo |
| 46 | + |
| 47 | +Use `window.snapSaveState` callback to store `Apollo` state, so it can be used to rehydrate on the client side. |
| 48 | + |
| 49 | +**Caution**: I didn't test this one. If you use it please let me know. |
| 50 | + |
| 51 | +```js |
| 52 | +// Grab the state from a global variable injected into the server-generated HTML |
| 53 | +const preloadedState = window.__APOLLO_STORE__ |
| 54 | + |
| 55 | +// Allow the passed state to be garbage-collected |
| 56 | +delete window.__APOLLO_STORE__ |
| 57 | + |
| 58 | +const client = new ApolloClient({ |
| 59 | + initialState: preloadedState, |
| 60 | +}); |
| 61 | + |
| 62 | +// Tell react-snap how to save state |
| 63 | +window.snapSaveState = () => ({ |
| 64 | + "__APOLLO_STORE__": client.store.getState() |
| 65 | +}); |
| 66 | +``` |
0 commit comments