Loading...

SSR Safety & Enhancement Audit

RULES.md

When creating a new component or theme, or working in an existing one, audit it for server-side rendering (SSR) without assuming issues exist. HAXcms server-renders elements with Lit SSR (`@lit-labs/ssr`) plus a small inert DOM shim. `hax audit ssr` (report-only, in the `create` repo) shows how an element fares; run it and read the saved HTML (`--save-html`). The server runs only the constructor, `willUpdate` and `render`; `connectedCallback`, `firstUpdated`, `updated` and `disconnectedCallback` do not run. Safety: (1) Keep the constructor, `willUpdate`, `render` and anything they call free of `document.body`, `document.head`, `localStorage` / `sessionStorage`, `location` and layout measurements. The shim supplies an inert `window`, event listeners, `matchMedia`, observers and query / child / class / style APIs, so those do not throw but find nothing, and `getBoundingClientRect` returns zeros. (2) Put browser-only work, including reading storage and measuring layout, in `connectedCallback`, `firstUpdated` or `updated`. (3) Guard any browser API the server can reach with `globalThis` (`typeof globalThis.document !== 'undefined'`, `globalThis.localStorage && ...`) or a try / catch, never a bare `window` / `document` reference, and do not use `?.`. (4) Add listeners and observers in `connectedCallback` and remove them in `disconnectedCallback`, so they keep working if the element is moved. (5) Keep import-time side effects out of helper modules; an element module defining its own tag is expected. (6) Load browser-only dependencies with dynamic `import()` from client-only lifecycle methods. (7) An element that cannot render on the server (WebGL, audio, real layout) sets `static ssr = false`, optionally with `static ssrSkipReason`, instead of throwing. Enhancement (go beyond not breaking): (8) Make the first render useful without JavaScript: default text, slotted content and attribute defaults should produce meaningful HTML on the server, not an empty shell. `radar-chart` is a good model, its server output holds the SVG, a text description and a data table. (9) The shim's `children` is empty, so data the first render needs must arrive through properties or attributes, not be read from light DOM children after upgrade; if an element keeps authored data in light DOM, make sure the render is still meaningful without it. (10) In templates, use attribute bindings instead of property bindings on native elements: `?checked` / `?disabled` rather than `.checked` / `.disabled` (SSR writes `.checked=${false}` out as `checked="false"`, which browsers read as on until hydration), and `src=${v}` / `href=${v}` (with `ifDefined` when it can be undefined) rather than `.src` / `.href` (SSR does not write a property binding at all, so an `<iframe .src=${url}>` reaches the server HTML with no address). `hax audit ssr` flags both. (11) Put attributes that crawlers or styles need (RDFa `typeof`, state that CSS selects on) in the rendered template, a reflected property or authored content, not in `setAttribute` calls inside `firstUpdated`, which the server never runs. (12) Keep `render()` deterministic (no `Math.random()`, `Date.now()` or generated ids) and use `static styles` rather than runtime-injected styles. (13) If the content lives only in a property or in the shadow render (a video address, a number, images), provide a `progressiveEnhancement` hook in `haxHooks()` that returns fallback light DOM markup (a link, text, an `<img>`) for crawlers, RSS, markdown exports and no-JS readers. HAX runs the hook on save and appends its output after the element's existing light DOM children, so make it save once: either set `saveOptions.wipeSlot: true` when the element has no authored slot content to keep, or mark the fallback (`data-prog-fallback`) and remove it in an `editModeChanged` hook when edit mode starts. Put it in a slot the shadow root does not render (the default slot when only named slots are rendered, or `slot="prog-fallback"`) so it is never drawn beside the real element, escape every value and accept only http(s) or relative URLs. (14) For themes, keep the skip link and named regions in the server-rendered template and do not depend on `document.body` or global state at import time. When the audit finds a gap, fix it if it is in scope of the change; otherwise note it so it can be tracked. Rationale and test cases: KNOWLEDGE.md entry "SSR audit of checklist, reflective nudge and radar chart". In PRAW RULES.md (scope: webcomponents). Nothing enforces this yet.

Status: gap

Connects to

Links