HtmlHydrate unstable
Hydration: mounts a component tree over the HTML HtmlServer.renderToHtml
rendered of it, adopting the server's nodes and the Model each instance
rendered with. An app that mounts with BrowserHost.mount doesn't carry it.
Constructors
hydrateEffect
unstableMounts a component tree over the HTML HtmlServer.renderToHtml rendered of
it in container, for the life of the current Scope, and returns its root.
When to use
Use when the caller needs the root: to read its Model, send it a message,
update its props with setProps, or wait for root.adopted, which
completes when no hydrating instance is still waiting. hydrate is the
form that runs an app.
Details
It mounts, checks the root and hydrates exactly as hydrate does, and
fails with the props Effect's error.
Signature
declare function hydrateEffect<Name extends string, Props, Model, Msg, Events, R, V extends Elements, RF = never, RP = never, EP = never>(component: Members<Name, Props, Model, Msg, Events, R, V, any, never> & HtmlRoot<V>, props: PropsInput<Props, EP, RP>, container: Element, state: State, options?: Omit<MountOptions<V, RF, Events>, "key">): Effect<Root<Model, Msg, Props>, EP, Scope | Exclude<R, Host> | Exclude<RF, Host> | Exclude<RP, Host>>Layers
A Layer that mounts a component tree over the HTML
HtmlServer.renderToHtml rendered of it in container, for as long as
the Layer is built. It provides no service: launch it with
Layer.launch, or merge it into the app's Layer.
When to use
Use to run a server-rendered app in the browser, in place of
BrowserHost.mount. Reach for hydrateEffect when the caller needs the
root, to send it messages, update its props or wait for root.adopted.
Details
Pass the state from HtmlServer.renderToHtml, with the server's HTML
already in container and the same props, to adopt the server's nodes
instead of building them. Router.props gives the same props on both
sides when both Routers are at the same URL, and decodeProps reads the
props serialized with includeProps: true. If the props Effect fails,
the Layer fails with that error and leaves the server HTML unchanged.
Options are as in BrowserHost.mount, except key: hydration reads the
server's key from state automatically.
Each instance adopts its server Model and replays init only to recover
its Commands, so the events init emits are dropped; server Commands the
Model already has the result of are skipped, and those that missed the
budget run now. onMount fires on adoption. Where an instance's view
stops matching the server's nodes, the nodes before stay adopted and the
rest is built fresh, and an instance the server deferred is built fresh.
An instance whose state decodes only asynchronously keeps the server's
nodes until it has.
Custom elements can be defined before hydrating. A defined custom element and its descendants keep attributes the view doesn't write; attributes the view writes take its value. Adoption also follows children into wrappers added by the custom element and preserves trailing nodes, except hydration markers. If child adoption fails, leftover nodes are removed instead.
The trade-off: server-only attributes and trailing nodes can remain on or inside defined custom elements when the client view no longer renders them.
An instance the server rendered with its provided Layer ready, whose
Layer is still building here, keeps the server's nodes until the Layer is
ready, and everything else is live at once. Its LayerPending entry says
Awaiting. One whose Layer was pending or failed on the server adopts at
once, and its feature slots keep the server's fallback nodes while their
status is the one the server shipped. A waiting instance is inert: its
buttons do nothing, links still navigate through the Router, and a
<form> submits natively. What the user types or picks in its fields
stays, and reaches the fields' input and change handlers when it adopts.
Clicks and submits aren't kept, whether made on a waiting instance or
before the client runs. Head sync starts once no instance is waiting.
The cache hydrates from the state's ~query entries. Until adopted
completes or the Router first navigates, a watch, get or prefetch
over a hydrated entry doesn't fetch, even when it is stale.
Signature
declare function hydrate<Name extends string, Props, Model, Msg, Events, R, V extends Elements, RF = never, RP = never, EP = never>(component: Members<Name, Props, Model, Msg, Events, R, V, any, never> & HtmlRoot<V>, props: PropsInput<Props, EP, RP>, container: Element, state: State, options?: Omit<MountOptions<V, RF, Events>, "key">): Layer<never, EP, Exclude<R, Host> | Exclude<RF, Host> | Exclude<RP, Host>>Example Hydrating a server-rendered app
import { HtmlHydrate, Html } from "effect/ui/html"import { Effect, Layer, Schema } from "effect"import { Renderer } from "effect/ui"
const App = Html.component({ name: "App", model: Schema.Struct({}), message: Schema.Never}).make({ init: () => ({}), update: ($) => $.model, view: (h) => h.h1("Hello")})
// The page embeds the state `HtmlServer.renderToHtml` returned.declare const state: Renderer.State
// In the browser entry module.Effect.runFork(Layer.launch(HtmlHydrate.hydrate(App, {}, document.getElementById("app")!, state)))Utils
decodeProps
unstableReturns an Effect that decodes root props from server state using the
component's props Schema, failing with Schema.SchemaError if missing or invalid.
When to use
Use as the props of hydrate or hydrateEffect when the server rendered
with includeProps: true and the client has no other source for them,
such as a component embedded in a page another renderer built.
Details
Hydration propagates decoding errors without changing the server HTML.
Recover with Effect.orElseSucceed to supply fallback props; hydration
then adopts the nodes that still match.
Signature
declare function decodeProps<Props>(component: { readonly props: Codec<Props, any>;}, state: State): Effect<Props, SchemaError>Example Hydrating with the server's props
import { HtmlHydrate, Html } from "effect/ui/html"import { Effect, Layer, Schema } from "effect"import { Renderer } from "effect/ui"
const News = Html.component({ name: "News", props: Schema.Struct({ items: Schema.Array(Schema.String) }), model: Schema.Struct({}), message: Schema.Never}).make({ init: () => ({}), update: ($) => $.model, view: (h) => h.ul.children(...h.props.items.map((item) => h.li.key(item).children(item)))})
// From `HtmlServer.renderToHtml(News, props, { includeProps: true })`.declare const state: Renderer.State
const app = HtmlHydrate.hydrate( News, HtmlHydrate.decodeProps(News, state), document.getElementById("news")!, state)Effect.runFork(Layer.launch(app))