Ssr unstable
Server rendering tests: render a tree on the server, hydrate it in a fresh document, and check that hydration adopted what the server rendered.
ssr fails with SsrError when:
- an instance the server rendered didn't adopt its Model, or its Model, encoded again after adoption, differs from the server's. Props that drift between the server and the client show up here, as paths that don't line up;
- a node the server rendered outside a deferred instance was rebuilt instead of adopted, or a marker is left behind;
- the server's merged head differs from the client's first merge. JSON-LD
is compared canonically,
classas a set anddata-*per name; - the head sync warns about the server's document;
- a shipped query entry doesn't decode and encode back as it was shipped,
args key included, once the state has been through
HtmlServer.serializeStateandJSON.parse; - the client fetched a hydrated entry into its cache inside the fetch-free
window, unless the render was
static. A caller-only fetch, which stores nothing, is allowed; - an instance was still waiting for its Layer when the budget was spent;
- a problem was reported, on either side.
Example Checking a page hydrates
import { ssr } from "effect/ui/html/testing"import { Effect, Schema } from "effect"import { Component, Head } from "effect/ui"import { TestHost } from "effect/ui/testing"
const App = Component.define({ name: "App", model: Schema.Struct({}), message: Schema.Never}).make({ init: () => ({}), update: ($) => $.model, head: () => [Head.title("Home")], view: (h) => h.el("h1", "Home")})
const test = Effect.gen(function*() { const result = yield* ssr(App, {}) return result.adopted // ["/App"]}).pipe(Effect.scoped, Effect.provide(TestHost.layer()))Constructors
Renders a component tree on the server, hydrates the HTML in a fresh
document with the same props, waits for root.adopted, and checks that
hydration adopted what the server rendered.
Details
ssr runs on TestHost's scheduler and the test clock, so it works
under it.effect. Both sides run with the caller's services, so provide
the ones the tree needs, such as Router.memory, around it.
props may be an Effect, such as Router.props(AppRoute). Each side runs
it once, before init, as a real server and browser would.
The server phase drives itself: it steps the clock to each wake of the
tree's own sleepers, a fetch or a Layer build, until the render is idle or
budget (default 500 ms) is spent. Once the server's Scope has closed the
clock is set to exactly the start plus budget, and the client mounts
there. While an instance waits for its Layer before adopting, the client steps
the clock the same way, for up to budget more, and then fails naming the
waiting paths. A Layer that sleeps on a clock other than the one it is
given is not seen: either phase then spends its whole budget before it
gives up.
The head check compares the server's folded head with the client's fold
of the server's state, made once every instance has adopted: each
instance's head as it evaluated at its adoption, before any onMount
work or later update. An instance that waited for its Layer adopts with
the placement it had, its props, slots and headless closure, even when its
parent rendered a new one while it waited, and its subtree counts as it
was then. The latest placement reaches it right after.
frames projects the container after each client commit, starting from
the server's markup, for root.frames(). It defaults to the container's
text.
budget and static are passed to the server render, and query to
the client mount only: the server's cache is per request. With static
there is no fetch-free window, so its check is skipped; an entry is then
stale at hydration, and refetched, unless its query's staleTime is
"Infinity".
Gotchas
Any problem fails ssr, including Command.ServerCommandIssued on the
server. To test one, render with Renderer.prerender instead.
In browser tests, pages with a popover or modal dialog open in the server
render hydrate in an off-screen iframe, removed when the Scope closes.
Other pages hydrate without a window. On those pages, opening a popover
or modal dialog first on the client throws InvalidStateError, including
after hydration or in an instance that waited for its Layer.
Signature
declare function ssr<Name extends string, Props, Model, Msg, Events, R, V extends Elements, RP = never>(component: Members<Name, Props, Model, Msg, Events, R, V, any, never> & HtmlRoot<V>, props: PropsInput<Props, never, RP>, options?: { readonly budget?: Input; readonly frames?: (container: Element) => string; readonly logSize?: number; readonly query?: QueryOptions; readonly static?: boolean;}): Effect<Ssr<Model, Msg, Events, Props>, SsrError, Scope | TestHost | Exclude<R, Host> | Exclude<RP, Host>>Errors
Hydration didn't adopt what the server rendered, or a check around it
failed. problems lists each one, by path, head identity or query.
Signature
declare class SsrError extends YieldableError<this> & { readonly _tag: "SsrError";} & Readonly<{ readonly problems: readonly Array<string>;}> { constructor(args: { readonly problems: readonly Array<string>; }); message: string;}Models
What ssr hands back once hydration has adopted.
Details
html, model and head are the server's. state is the server's
state as the client read it, after HtmlServer.serializeState and
JSON.parse. document is the hydrated page and container the app's
element in it. When hydration uses an iframe, both belong to its realm, so
container instanceof HTMLElement is false: use
document.defaultView.HTMLElement. root is the client tree, registered
with TestHost so TestHost.settle waits for it; root.fetches() lists
the client's fetches. adopted lists the paths whose Model the client
adopted. log is the client's log up to adoption and serverLog the
server's, both unbounded unless logSize says otherwise. commands lists
all init Commands run by adopted instances, by position. misses lists
query entries the server's cache had in flight when the budget was spent,
or never started with a budget of 0. The client fetches those.
Signature
interface Ssr<out Model, in Msg, out Events = never, in Props = never> { readonly adopted: readonly Array<string>; readonly commands: readonly Array<{ readonly index: number; readonly name: string; readonly path: string; }>; readonly container: Element; readonly document: Document; readonly head: HtmlHead; readonly html: string; readonly log: readonly Array<Entry>; readonly misses: readonly Array<{ readonly logKey: string; readonly query: string; }>; readonly model: Model | undefined; readonly root: Root<Model, Msg, Events, Props>; readonly serverLog: readonly Array<Entry>; readonly state: State;}