---
url: >-
  https://lit-ui-router.dev/reference/ui-router-server/prerender/functions/prerender.md
---
[ui-router-server](../../index.md) / [prerender](../index.md) / prerender

# Function: prerender()

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

Defined in: [prerender.ts:468](https://github.com/simshanith/lit-ui-router/blob/740888c2b2ba358c4dfa52a122e927df6a604b12/packages/ui-router-server/src/prerender.ts#L468)

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](../interfaces/RenderedRedirect.md), 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\`](../../url-config/functions/applyUrlConfig.md).
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](../interfaces/PrerenderOptions.md#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`](../interfaces/PrerenderOptions.md) | the mount table, the paths, the render, and the hooks |

## Returns

`Promise`<[`PrerenderResult`](../interfaces/PrerenderResult.md)>

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

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