Code-splitting with SSR

When rendering our application on the server, we have to tell the client which bundles to download on the initial page load. Open the server's index.js file to implement this logic. Import the react-loadable dependencies at the top of the file, as follows:

import Loadable, { Capture } from 'react-loadable';import { getBundles } from 'react-loadable/webpack';

We import the Loadable module itself, but also the Capture module. The last one is rendered along with your server-rendered application to collect all modules or components that were rendered for the current route that the user is visiting. It allows us to include those bundles along with the initial HTML that our server returns. To let our back end know which ...

Get Hands-On Full-Stack Web Development with GraphQL and React now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.