Skip to content

woywro/next-lazy-hydration-on-scroll

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

63 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Logo

next-lazy-hydration-on-scroll

Hydrate components dynamically as the user scrolls
๐Ÿš€ View Demo

๐ŸŒŸ Motivation

Next.js, being a React framework, follows React's hydration process. In Next.js each page is pre-rendered into HTML on the server. This HTML is then sent to the browser, where it is displayed to the user almost immediately, providing a fast initial load time.

Once the JavaScript associated with the page is downloaded and executed, React takes over in the browser, "rehydrates" the static content by attaching event listeners to the DOM, and makes it interactive. This means that any buttons will become clickable, forms submitable, and any client-side functionality defined in your React components will become active (for e.g. hooks like useEffect).

In short, hydration is key to combining the best of both worlds:

  • the speed of a statically generated website
  • the interactivity of a single-page application (SPA).

What is the Problem with Hydration?

Hydration process takes time. The more components we have, the longer the hydration process will take. This means that the user might experience a delay before the page becomes fully interactive.

What is the Solution?

To optimize this, it's important to minimize the number of components rendered initially and consider techniques like code splitting to load components only when they are needed.

Note: Even if you use next/dynamic to import every component on the page, they will still be executed on the initial page load unless they are conditionally rendered.

If you are using Next.js with app directory you can use streaming which allows for selective hydration. However, if your app uses pages directory (which does not support streaming), you can consider suspending hydration manually for e.g. by hydrating components on scroll.

Example use

โš™๏ธ Installation

Install the package using one of the following commands:

npm install next-lazy-hydration-on-scroll

pnpm install next-lazy-hydration-on-scroll

yarn add next-lazy-hydration-on-scroll

๐Ÿš€ Usage

import { lazyHydrate } from "next-lazy-hydration-on-scroll";

const Component = lazyHydrate(
  () => import("../components/Component"),
  {
    LoadingComponent: () => <div>Loading...</div>,
  }
);

๐Ÿ› ๏ธ How next-lazy-hydration-on-scroll Works?

It delays the hydration of pre-rendered HTML and splits js into smaller chunks that are loaded on scroll. ร“

  • ๐ŸŒ Server-Side Rendering: Initially, pages are rendered server-side with static components.
  • ๐Ÿ“ฆ Dynamic Imports: Components are set up for dynamic import, reducing initial load size.
  • ๐ŸŒ€ Client-Side Placeholders: Non-interactive placeholders are rendered client-side initially.
  • ๐Ÿ‘€ Scroll Detection: Uses IntersectionObserver to detect when components enter the viewport.
  • โšก Conditional Hydration: Visible placeholders are replaced with interactive components on-the-fly.

๐Ÿš€ Performance Impact

Optimizing the hydration process significantly impacts Total Blocking Time (TBT) by reducing the amount of work on the main thread. This decrease in work load allows the main thread to remain more responsive, minimizing the time it spends blocked so the page becomes interactive faster.

  1. Optimized Version (Component3 is lazy loaded and hydrated on scroll) Optimized performance

  2. Unoptimized Version (Component3 is statically imported) Unoptimized performance

โš ๏ธ Additional Info

This package uses next/dynamic under the hood. Code splitting in Next.js work on a file basis. Ensure individual components you intend to load separately are in distinct files. Otherwise, dynamic import won't work.