ui-router-server / prerender / prerender
Function: prerender()
function prerender(options): Promise<PrerenderResult>;Defined in: prerender.ts:468
Emits a static site from a mount table's verdicts: a shell verdict becomes <subpath>/index.html holding whatever render returns, or a rules line when render answers a RenderedRedirect, a response state becomes the body or rules line its respond answer carries, a redirect becomes a rules line and no page, a delegating state is listed in delegated and skipped, and the otherwise projection becomes the 404 document. A verdict kind this package doesn't know throws rather than being counted.
Rules lines follow each mount's config.trailingSlash policy. Where it spells a page or a response state 'always' or 'never', the output lands at the path listed and the other spelling gets the 308 line onto it that the server answers; list each path in its canonical spelling, as href() formats it under `applyUrlConfig`. Under 'as-requested' neither spelling gets a line. A redirect's line is written for both spellings of its path, since a static host may append the slash. Under a base, every generated line carries it.
It knows no renderer: render turns each shell verdict into the file's text, so any template engine, or a settle and a string, can fill it. lit-ui-router-ssr's prerender wraps this loop with a lit render.
Parameters
| Parameter | Type | Description |
|---|---|---|
options | PrerenderOptions | the mount table, the paths, the render, and the hooks |
Returns
Promise<PrerenderResult>
what was emitted, planned or written
Throws
an Error when neither mounts nor resolver is supplied
Throws
an Error when base is relative or a full URL, or differs from the resolver's
Throws
an Error when a mount of a resolver that declares no base already carries base
Throws
an Error when the 404 document needs a default probe and only a resolver is supplied
Throws
an Error when a path resolves to a verdict kind this package doesn't know
Throws
an Error when the 404 document's render answers a redirect
Throws
an Error when a path matches a response state and no respond is supplied, or its answer is neither a 2xx nor a 3xx with a Location
Example
import type { UIRouter } from '@uirouter/core';
import { mkdir, writeFile } from 'node:fs/promises';
import { dirname } from 'node:path';
import { prerender, type PrerenderOptions } from 'ui-router-server/prerender';
import { settle } from 'ui-router-server/ssr';
declare const mounts: PrerenderOptions['mounts'];
declare const router: UIRouter;
declare function page(router: UIRouter): string;
const result = await prerender({
mounts,
outDir: 'dist',
write: async (file, body) => {
await mkdir(dirname(file), { recursive: true });
await writeFile(file, body);
},
paths: ['/', '/sheet/7B', '/legacy'],
render: async (verdict, { path }) => {
await settle(router, path);
return page(router);
},
});