ui-router-server / prerender / prerender
Function: prerender()
function prerender(options): Promise<PrerenderResult>;Defined in: prerender.ts:337
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.
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 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 { mkdir, writeFile } from 'node:fs/promises';
import { dirname } from 'node:path';
import { prerender } from 'ui-router-server/prerender';
import { settle } from 'ui-router-server/ssr';
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);
},
});