Skip to content

ui-router-server / prerender / prerender

Function: prerender() ​

ts
function prerender(options): Promise<PrerenderResult>;

Defined in: prerender.ts:300

Emits a static site from a mount table's verdicts: a shell verdict becomes <subpath>/index.html holding whatever render returns, a redirect becomes a rules line and no page, and the otherwise projection becomes the 404 document.

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 ​

ParameterTypeDescription
optionsPrerenderOptionsthe 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

Example ​

ts
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);
  },
});