Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/routes/(5)guides/(2)custom-primitives.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
51 changes: 51 additions & 0 deletions src/routes/(5)guides/(8)ssr-safe-code.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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):`

Expand Down Expand Up @@ -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 `<div>` inside `<p>` or `<tr>` directly inside `<table>`.
Expand All @@ -384,6 +434,7 @@ Use `createUniqueId`, which derives the id from the owner tree on both sides.
- The server runs module scope once per process, the component body once per request, and every compute function; it never runs an effect function or an `onSettled` callback.
- Put browser-only work in an effect function or `onSettled`; use `isServer` for helpers that have no owner, and `clientOnly` for a component that cannot be made safe.
- `isServer` prevents the crash and does not make the two renders agree; keep it out of JSX that decides which elements exist.
- Create the same signals, memos, and effects on both sides; put browser-only work inside the effect function or `onSettled`, not behind an early `return` on `isServer`.
- Compute `Math.random()`, `Date.now()`, and other per-run values once, on the server, and pass the result as data.
- Use `createUniqueId` for ids that appear in both a `for` attribute and an `id` attribute.
- Render the value the server has, then update from the browser after hydration; or move the data to a cookie or session so the server has it.
Expand Down
Loading