HtmlServerVite unstable
A Vite plugin that renders an effect/ui app on the server in
vite dev and prerenders its pages in vite build.
effect/ui/html doesn't re-export it.
The app's server entry exports a default Layer that adds its routes to
HttpRouter, such as HtmlServerHttp.layerHttp, and prerender, the
paths to prerender, as an array or an Effect of one. It renders its pages
with document from virtual:effect/ui/document, which fills
index.html with HtmlServer.document. Add
effect/ui/html/HtmlServerViteDocument to compilerOptions.types for
that module's types. index.html holds the static markup and the
<!--app-html--> marker where the page goes.
In dev, Vite answers module, asset and /@vite requests first, and every
other request goes to the server entry. The template is index.html
after Vite's HTML transforms. Editing a module or index.html reloads
the server entry on the next request.
In a build, it builds the client, then the server entry, using the
client's index.html as the template. It requests each prerender
path at origin with HtmlServer.Static set to true and writes a
200 to <path>.html, or index.html for a path ending in /, and a
404 to 404.html. Any other status fails the build. When
serverOutDir is omitted, the server build isn't kept.
With serverOutDir, the build emits a server entry and declaration file.
By default, the entry is
<serverOutDir>/index.js (or index.mjs when the app's package isn't
"type": "module"). It exports handler and dispose from
HttpRouter.toWebHandler. A host serves files from the client outDir
first and passes remaining requests to handler. serverOutDir must
not overlap the client outDir.
When another renderer builds the page shell, reduce index.html to its
asset tags and a <!--app-html--> marker directly in <body>, so the
shell renders the container the client hydrates. document inserts
html and head as given, so pass the shell's markup there and spread
the rest of rendered. Append the shell's own <html> or <body>
attributes to htmlAttributes or bodyAttributes, after a space. Give
<title> and each <html> or <body> attribute a single owner, since
neither is merged.
For a non-root Vite base, pass import.meta.env.BASE_URL as base
to layerHttp and BrowserRouter.layer. The dev server and the
serverOutDir handler receive URLs with the base prefix. Prerender
paths are app-relative: with base: "/docs/", "/about" is requested
as "/docs/about" but written to about.html.
Prerender paths are read before the entry's Layer sets the routes' base.
Relative bases keep routes at the root. Full-URL bases do too in builds,
but in dev Vite sets BASE_URL to the URL's path, such as "/static/",
and routes use that path as their base.
Gotchas
Every prerendered page is filled from the root index.html. With a
relative base such as "./", its ./assets/โฆ URLs only resolve from
pages at the root, so people/1.html loses its assets. Use an absolute
base for nested pages.
vite build --watch isn't supported and fails before building. Use
vite dev to rebuild on changes.
vite build imports the server entry in Node to read prerender, even
when no paths are listed. Entries targeting other runtimes, such as
Cloudflare Workers, must also be importable in Node at build time.
During server rendering, Vite either bundles a package or leaves it to
Node, and the two load separate copies of effect. A component library
that imports effect/ui must load effect the same way as the app.
Otherwise the app can't render the library's views: the page renders
without them and the server logs a view error. By default Vite leaves
both effect and installed libraries to Node, so they share one copy.
Vite bundles effect when it's listed in ssr.noExternal, or linked
from outside node_modules as in a workspace. Then list each component
library in ssr.noExternal too.
Example Configuring the plugin
import { plugin } from "effect/ui/html/HtmlServerVite"
// vite.config.ts: export default { plugins: [ssr] }const ssr = plugin({ entry: "/src/entry.server.ts" })
// src/entry.server.ts://// import { document } from "virtual:effect/ui/document"// export default layerHttp(App, { routes: AppRoute, document, base: import.meta.env.BASE_URL })// export const prerender = [AppRoute.Home.href()]Example
import { plugin } from "effect/ui/html/HtmlServerVite"
// vite.config.ts: `export default config`. `acme-ui` exports views// built with `effect/ui`, so it's bundled along with `effect`.const config = { plugins: [plugin({ entry: "/src/entry.server.ts" })], ssr: { noExternal: ["effect", "acme-ui"] }}Example Shell from another renderer
import type { HtmlServer } from "effect/ui/html"import { document } from "virtual:effect/ui/document"
// src/entry.server.ts: pass `page` to `layerHttp` as `document`.const page = (rendered: HtmlServer.Rendered<unknown>): string => { const shell = renderShell(rendered.html) // the other renderer return document({ ...rendered, html: shell.body, head: shell.head + rendered.head })}Constructors
Models
The options of plugin.
Signature
interface Options { readonly entry: string; readonly origin?: string; readonly serverOutDir?: string;}The subset of a Vite plugin that plugin returns. It is assignable to
Vite's Plugin without importing vite.
Signature
interface Plugin { readonly name: string; readonly sharedDuringBuild: true; buildApp(builder: Builder): Promise<void>; buildEnd(this: { readonly environment: object; }): Promise<void>; config(config: unknown, env: { readonly isPreview?: boolean; }): object; configureServer(server: DevServer): () => void; load(this: LoadContext, id: string): Promise<string | undefined>; resolveId(source: string): string | undefined;}