blog   |   24 Nov, 2021

React JS: Progressive hydration

In the current digital landscape, the race to deliver content swiftly to users while securing a prominent position in search engine results is a common pursuit. Balancing these objectives, however, can pose challenges, leading some to resort to short-term strategies that prove effective initially but may become unwieldy in the long run.

Today, I'll share a practical technique for rendering pages on the server-side and deferring hydration on the client-side. This approach offers several key benefits:

  1. Reduced First Time-To-Interact on the Client-Side: By adopting progressive hydration, the time it takes for a user to interact with the content on the client-side is significantly minimized.

  2. Decreased First Paint Time on the Client-Side: Progressive hydration contributes to a faster first paint time, enhancing the overall user experience.

  3. Faster Content Delivery to Users: Content is served more rapidly to users without noticeable differences in their experience.

The concept of Progressive Hydration was introduced by Google during the Google I/O 2019 event, where they outlined its general principles. This technique aims to minimize the hydration of server-side content on the client, allowing for prioritized hydration of specific content when needed, as opposed to hydrating the entire server-side content during page load.

A popular method to implement this is by encapsulating your component within the Intersection Observer. This enables the hydration of the required component only when it is about to enter the viewport. Consequently, the page gets hydrated progressively as the user scrolls. If a user merely visits the page without scrolling, they avoid the additional time previously allocated for hydration.

For those interested in designing a progressive hydration component, here's a conceptual guide for implementation:

Given the necessity for fully rendered HTML from the server for SEO purposes, your component should include a check to distinguish between server and client-side rendering, producing different content in each scenario. During server-side rendering, the component returns the full HTML with no additional processing. Conversely, during client-side rendering, only the top-level HTML element that will be rendered is produced initially, ensuring there is no need for hydration. When the component is about to enter the viewport, the actual rendering occurs. Since this is a completely client-side rendering, there is no requirement to hydrate or attach event listeners, as the browser handles these tasks during rendering.

To address potential hydration warnings resulting from differing HTML between SSR and CSR, it's advisable to use suppressHydrationWarning={true}. This suppresses hydration warnings during content comparison, preventing console errors. However, caution is advised in the use of suppressHydrationWarning.

In summary, Progressive Hydration offers an effective means to progressively hydrate your component, resulting in reduced time-to-interact and first paint time. This method optimizes the user experience without compromising the integrity of server-side rendering for SEO purposes.