Code-Splitting is a feature supported by bundlers like Webpack which allows you to create smaller chunks which you can then “lazily load” on demand. React provides React.lazy to render a dynamic import as a regular component. Let us look at code below,
const LazyComponent = React.lazy(() => import('./LazyComponent'));
This will create a different chunk for LazyComponent. Now, we can use this to lazy load our components straight away, but there is a catch.
React.lazy and Suspense are not yet available for server-side rendering. They recommend loadable components to Code Split our app this way if we want Code splitting with server rendering.
Now, let’s recap the two aspects which we will be focusing on:
- We can bail out of hydration phase using the dangerouslySetInnerHtml trick.
- We can lazy load our chunks using Code Splitting with loadable components.
Now, the question is, when do we fetch these chunks and hydrate the Component. One possible solution is, just before they are entering viewport. This is referred as Progressive hydration. In Google I/O ’19, we get a gist of what it is and how we can implement this today (they did mention about React’s roadmap to ship this). So, let’s look at the code as how can we achive it.
Since the Server side rendering is going to happen in a single pass, you need to handle your component’s markup differently on the server side. You can simply create the component independently and the pass this as a prop to the root component. The Code would look something like below:
So, once we handled the Server rendering (and else block of render method). Let’s have a closer look at the if block of render() method now. When the first time render() will be called on client side, it will wrap the component with InView and will make use of dangerouslySetInnerHtml to avoid hydration cost. The InView component makes use of Intersection Observer to observe an element’s visibility.
Now, once the user is about to enter viewport, the handleInviewChange will trigger which will fetch the component chunk making good use of loadable and then toggle the component state to trigger rendering. This way, now the component is ready to handle user interactions.
Conclusion: In my opinion, both idle-until-urgent and progressive hydration can complement each other to build a better progressive web app.
While Progressive hydration makes sense on below the fold components which can significantly (like carousels) reduce the initial chunk size required to boot the app but it comes up with an overhead of special Server Side handling, on the other hand Idle-Until-Urgent is pretty quick and easy to implement(making it a better candidate for light weight components).