Server rendering & hydration
Static and dynamic styles with SSR, streaming, hydration and CMS content.
Static attributes: nothing to do#
With the build plugin, static nib attributes are in the stylesheet before the HTML arrives. With
rewrite: true the server HTML already carries the classes.
Dynamic values: collect and inline#
useNib (and the Vue/Svelte equivalents) generate CSS during the server render. Put it in the document so
the first paint is styled; the client runtime adopts the same <style id="nib">:
const rt = new NibRuntime({ watch: false, ...config });
const app = renderToString(<NibProvider runtime={rt}><App /></NibProvider>);
const css = collectNibStyles(rt);Streaming (Next.js App Router)#
Flush collected CSS with useServerInsertedHTML in a client component:
"use client";
import { useServerInsertedHTML } from "next/navigation";
import { collectNibStyles, useNibRuntime } from "@nibcss/react";
export function NibStreamStyles() {
const rt = useNibRuntime();
useServerInsertedHTML(() => <style id="nib" dangerouslySetInnerHTML={{ __html: collectNibStyles(rt) }} />);
return null;
}Hydration warnings#
Warning | Cause | Fix |
|---|---|---|
attribute mismatch on | the runtime added |
|
| the runtime rewrote a | use |
style flash on navigation | CSS for dynamic values not collected | render |
Content from a CMS or database#
Markup that arrives at request time was never seen by the build. Compile it on the server with the engine and send the CSS alongside — see the Next.js guide.