HtmlServerHttp unstable
Serves a routed effect/ui app's pages through HttpRouter, with
HtmlServer.renderToHtml doing the rendering.
It is a separate entry point, effect/ui/html/HtmlServerHttp, and
effect/ui/html doesn't re-export it. A browser bundle that imports
effect/ui/html then takes in none of the effect/http modules this
module needs, even when it uses a namespace such as Html as a value.
Layers
Serves a routed app's pages through HttpRouter: a GET * route that
renders the component at the request's URL and answers with a document.
When to use
Use when the app's root takes { route } and mounts in the browser with
Router.props(routes), and every service the render needs can be built
once with the server.
Details
When built, the Layer applies base to route matching and printed links.
Closing its Scope restores the previous base. Omitting base leaves the
routes' base unchanged.
Per request it resolves the URL with routes. A URL that doesn't resolve
is a 404, and a redirected or normalised one is a 301 to
routes.print(route), or a 302 when search params were also dropped as
invalid. A URL with search params dropped as invalid is a 302 to the same
URL without them, keeping the others as written. Otherwise it renders with HtmlServer.renderToHtml under
Router.static at the URL, with Router.props(routes) as the props,
the same props the client hydrates with. The response is
document(rendered) as text/html.
The status is 404 for the catch-all "/*" route and 200 otherwise.
status(route, rendered) overrides it, for example to return 500 when
rendered.failures is nonempty. budget and static are renderToHtml's options.
The Layer captures the component's services when it is built, so provide
them with Layer.provide. It needs no request service.
Gotchas
A service provided to the Layer is shared by every request. Render with
HtmlServer.renderToHtml in your own handler when a service belongs to the
request, such as the current user.
See
- HtmlServer.renderToHtml for services that belong to the request
Signature
declare function layerHttp<Name extends string, Model, Msg, Events, R, V extends Elements, U extends Routes>(component: Members<Name, { readonly route: U["Type"];}, Model, Msg, Events, R, V, any, never> & HtmlRoot<V>, options: { readonly base?: string; readonly budget?: Input; readonly document: (rendered: Rendered<Model>) => string; readonly routes: U; readonly static?: boolean; readonly status?: (route: U["Type"], rendered: Rendered<Model>) => number;}): Layer<never, never, HttpRouter | Exclude<R, Host | Services>>Example Serving a routed app
import { Layer, Schema } from "effect"import { HttpRouter } from "effect/http"import { Component, Route, Router } from "effect/ui"import { HtmlServer } from "effect/ui/html"import { layerHttp } from "effect/ui/html/HtmlServerHttp"
const AppRoute = Route.union({ Home: Route.make("/"), NotFound: Route.make("/*")})
const App = Component.define({ name: "App", props: Schema.Struct({ route: AppRoute }), model: Schema.Struct({ route: AppRoute }), message: Schema.Never}).make({ init: ($) => ({ route: $.props.route }), update: ($) => $.model, view: (h, { route }) => h.el("h1", route._tag === "Home" ? "Home" : "Page not found")})
const Pages = layerHttp(App, { routes: AppRoute, budget: "300 millis", document: (page) => `<!doctype html><html ${page.htmlAttributes}> <head><meta charset="utf-8">${page.head}</head> <body ${page.bodyAttributes}> <div id="app">${page.html}</div> <script type="application/json" id="state">${HtmlServer.serializeState(page.state)}</script> <script type="module" src="/client.js"></script> </body></html>`})
const { dispose, handler } = HttpRouter.toWebHandler(Pages, { disableLogger: true })const response = await handler(new Request("http://localhost/nowhere"))response.status // => 404await dispose()