Server-side rendering with Declarative Shadow DOM (DSD) for zero-FOUC page loads.
When web components render client-side, the browser shows raw HTML until JS loads and components call render() — a flash of unstyled content (FOUC). This is especially bad on slow networks.
Brownie's SSR module runs the actual component code in Node.js using minimal DOM polyfills. Components execute their real render() methods, and the output is wrapped in <template shadowrootmode="open"> — the browser creates shadow roots during HTML parsing, before any JS loads.
node examples/ssr/server.js
# Open http://localhost:3000import { createSSR } from '@cixzhang/brownie/ssr';
import Button from '@cixzhang/brownie/components/brow-button';
import Card from '@cixzhang/brownie/components/brow-card';
// 1. Create SSR instance (sets up polyfills, intercepts registration)
const ssr = await createSSR();
// 2. Import components — they self-register with Brownie
// (must be dynamic imports so polyfills are in place first)
const Button = (await import('brownie/components/brow-button.js')).default;
const Card = (await import('brownie/components/brow-card.js')).default;
// 3. Render components with DSD
const buttonHtml = ssr.dsd(Button, { variant: 'primary' }, 'Click me');
const cardHtml = ssd(Card, { padding: 'spacing-6' }, buttonHtml);
// 4. Generate a complete HTML page
// page() scans body HTML for <brow-*> tags and auto-generates:
// - Inlined hydrate script
// - Base CSS + theme CSS
// - Client import statements for each used component
// - Brownie.expect() + Brownie.ready() wiring
const html = ssr.page({
title: 'My App',
body: cardHtml,
onReady: `console.log('hydrated');`,
});The hydrate script is automatically inlined by page() — no additional setup needed. It patches attachShadow() to return existing DSD shadow roots instead of throwing, enabling seamless hydration when component modules load.
Returns a singleton SSR instance. Sets up DOM polyfills (CSSStyleSheet, HTMLElement, customElements) and intercepts Brownie.register() to build a component registry.
Render a component with Declarative Shadow DOM.
tagOrClass— Component class reference (Button) or tag name string ('brow-button')attrs— HTML attributes objectlightHtml— Light DOM content (children)
Generate a complete HTML document with auto-wired hydration.
title— Page titlebody— HTML contenthead— Extra<head>contentonReady— JS to run insideBrownie.ready().then(() => { ... })extraComponents— Additional tag names to import (not found in body scan)
Get component CSS only (no DSD wrapper).
Get component shadow DOM innerHTML only (no CSS, no DSD wrapper).
createSSR()polyfillsCSSStyleSheet,HTMLElement,customElementsin Node- Component modules are imported — they call
Brownie.register()which builds the registry dsd()instantiates a component by its class reference, sets attributes, callsconnectedCallback()- The real
render()method produces shadow DOM HTML - Output wraps shadow DOM in
<template shadowrootmode="open">with CSS inline - Browser parses the DSD template, creating the shadow root before JS loads
- When component modules load client-side,
attachShadow()returns the existing DSD root connectedCallback()callsrender()— same content, no flicker
// ✅ Explicit, tree-shakeable, IDE autocomplete
import Button from '@cixzhang/brownie/components/brow-button';
dsd(Button, { variant: 'primary' }, 'Click me');
// ✅ Also works — string tag name
dsd('brow-button', { variant: 'primary' }, 'Click me');Class references prevent silent failures — if you forgot to import a component, you get an immediate error, not a runtime lookup miss.
No filesystem scanning. Components self-register when imported. The server imports only what it needs. Scales to hundreds of components — the registry is a Map with O(1) lookup.