diff --git a/src/routes/(5)guides/(2)custom-primitives.mdx b/src/routes/(5)guides/(2)custom-primitives.mdx index a618133928..35749c15e9 100644 --- a/src/routes/(5)guides/(2)custom-primitives.mdx +++ b/src/routes/(5)guides/(2)custom-primitives.mdx @@ -290,6 +290,7 @@ Add a mug, refresh, and the mug is still in the cart. Neither the `onSettled` callback nor the effect function runs during server rendering. The server renders an empty cart, and the browser fills the draft in after hydration. For a check the body itself has to make, [`isServer`](/reference/solid-web/rendering-ssr/is-server) from `@solidjs/web` is a build-time constant, `true` in the server build and `false` in the browser build. +Use it to choose a value, not to return before the primitive creates its signals and effects, which must exist on both sides for hydration ids to match. The [SSR-safe code](/guides/ssr-safe-code) guide covers the cases where the two sides render different first frames. ## Run outside a component @@ -369,7 +370,8 @@ Pass `() => props.breakpoint`, and return the accessor. ### The primitive works in the browser and crashes the server render Setup that touches `window`, `matchMedia`, or `localStorage` is in the body of the primitive. -Move it into an `onSettled` callback or an effect function, neither of which runs on the server, or guard it with `isServer`. +Move it into an `onSettled` callback or an effect function, neither of which runs on the server. +Do not return early on `isServer` before the primitive creates its effects: that shifts the hydration ids of everything rendered after it, as [Primitives that return early on the server](/guides/ssr-safe-code#primitives-that-return-early-on-the-server) shows. The [SSR-safe code](/guides/ssr-safe-code) guide has the full checklist. ## Recap diff --git a/src/routes/(5)guides/(8)ssr-safe-code.mdx b/src/routes/(5)guides/(8)ssr-safe-code.mdx index 7c4fb0953f..7822ddd98c 100644 --- a/src/routes/(5)guides/(8)ssr-safe-code.mdx +++ b/src/routes/(5)guides/(8)ssr-safe-code.mdx @@ -155,6 +155,48 @@ A helper like this returns `false` on the server and possibly `true` in the brow For a component that cannot be made safe, such as a map or a rich text editor whose library touches `window` at import, [`clientOnly`](/reference/solid-web/rendering-ssr/client-only) renders a fallback on the server and swaps the component in after hydration. [Keep it off the server](/guides/integrate-non-solid-code#keep-it-off-the-server) covers it, including the `{ lazy: true }` option. +## Primitives that return early on the server + +A focus-trap primitive returns as soon as it sees `isServer`, because there is nothing to trap on the server. +The menu button after it renders in the server HTML, and after hydration its clicks do nothing. + +During hydration every computation a component creates, each signal with a function, memo, effect, and `onSettled` callback, takes the next hydration id of its owner. +The server creates the same computations and reserves the same ids, including for effects it never runs. +When a primitive skips its computations on the server only, the browser ends up ahead by one id for each of them, and every element rendered after it looks for a key the server never wrote: + +```ts +import { createEffect } from "solid-js"; +import { isServer } from "@solidjs/web"; + +// Avoid: the server skips the effect, so every id after it shifts +export function createFocusTrap( + target: () => HTMLElement | undefined, + trapFocus: (event: KeyboardEvent) => void +) { + if (isServer) return; + createEffect(target, (el) => { + if (!el) return; + el.addEventListener("keydown", trapFocus); + return () => el.removeEventListener("keydown", trapFocus); + }); +} + +// Prefer: the effect exists on both sides, and only its effect function is browser-only +export function createFocusTrap( + target: () => HTMLElement | undefined, + trapFocus: (event: KeyboardEvent) => void +) { + createEffect(target, (el) => { + if (!el) return; + el.addEventListener("keydown", trapFocus); + return () => el.removeEventListener("keydown", trapFocus); + }); +} +``` + +In the `Prefer` version the server creates the effect and runs its compute function, so both sides allocate the same ids, and only the browser attaches the listener. +The effect function and the `onSettled` callback already skip the server, so browser-only work inside them needs no `isServer` check. + ## Values that differ on every run The product page shows a promotion banner to half of its visitors. @@ -332,6 +374,7 @@ The two sides rendered different elements at the same position: a conditional th The client rendered a template root the server never emitted under that id. The message continues with what happened: `A detached element was created instead; its subtree will not appear in the document or become interactive.` Look for a branch that runs only in the browser, such as `Show when={!isServer}` or a per-run value, or for a subtree hydrated under a different `renderId` than the server used; the message names that case, and [Controlling hydration](/concepts/rendering-and-ssr#controlling-hydration) covers it. +A primitive that returns early on `isServer` before creating its computations causes the same miss for every element after it; see [Primitives that return early on the server](#primitives-that-return-early-on-the-server). ### `Hydration completed with N unclaimed server-rendered node(s):` @@ -364,6 +407,13 @@ The two sides now both render, and they render different structures. Find the value that differs: a `Math.random()` or `Date.now()` in a condition, a check on `isServer` inside JSX, or data one side has and the other does not. [Values that differ on every run](#values-that-differ-on-every-run) and [Data the server does not have](#data-the-server-does-not-have) cover the fixes. +### A button renders but does nothing after hydration + +The server HTML shows the element, and clicking it does nothing, in development and in production. +Development warns `Hydration key miss` for the element and `Hydration completed with N unclaimed server-rendered node(s)` for the server's copy; production shows nothing. +The browser created a detached copy with the handlers attached, and the server's copy stays on the page. +Look above the element for a primitive that returns early on `isServer`, and for a branch that renders only in the browser; [Primitives that return early on the server](#primitives-that-return-early-on-the-server) covers the first. + ### The build fails with `The HTML provided is malformed` One JSX template nests elements the browser's parser would move, such as `
` or `