0.0.1-development • Published 1 year ago

@fractal-web/next-ssr-hydration v0.0.1-development

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

next-ssr-hydration

npm package Build Status Downloads Issues Code Coverage Commitizen Friendly Semantic Release

An attempt to make lazy hydration with ssr-prerendering work in the modern Next.js 13/14.

Current versions of Next.js rely on build-time compilation/transformation of the source code so it's very important how you write and structure your code.

You have to not forget that use client directive and split the components into files in the right way. Below is an example that works.

Install

npm install @fractal-web/next-ssr-hydration

Usage

// ProductsShowcase.lazy.tsx

'use client'; // DONT FORGET

import dynamic from 'next/dynamic';

import {
  passThroughLoading,
  withHydrationOnDemand,
} from '@fractal-web/next-ssr-hydration';

// the use of `dynamic` instead of `lazy` is important
// that way Next.js' compiler loads the styles of the component eagerly
const ProductsShowcase = dynamic(() => import('./ProductsShowcase'), {
  loading: passThroughLoading,
});

export const ProductsShowcaseLazy = withHydrationOnDemand({
  id: 'ProductsShowcaseLazy', // in the current implementation should be unique per rendered component
  on: ['visible'], // will load the js-bundle of the component and hydrate it when it gets visible in the viewport
})(ProductsShowcase);