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

# Function: serverRouterPlugin()

```ts
function serverRouterPlugin(router, options?): ServerRouterPlugin;
```

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

Wraps a [ServerRouter](../../index/interfaces/ServerRouter.md) as a Vite plugin. The middleware installs in the
PRE position — synchronously inside `configureServer`, before Vite adds
its own middlewares — which is exactly where `connect-history-api-fallback`
sits and the only position that beats Vite's always-200 HTML fallback: a
redirect answers 302 and a mount-owned miss answers 404 before Vite can
serve `index.html`. The adapter's `shouldHandle` still lets module and
asset fetches pass untouched, essential under Vite where a mount's module
URLs share paths with its routes.

`serveShell` defaults to rewriting `req.url` to the mount base and
`next()`ing into Vite's HTML/transform layer (dev) or `sirv` (preview);
point [ConnectAdapterOptions.shellPath](../../connect/interfaces/ConnectAdapterOptions.md#shellpath) at the mount's actual HTML entry
when it isn't served at the base.

With [prerender](../interfaces/ServerRouterPluginOptions.md#prerender), `vite build`
runs the entry after the client build and fails when a listed path matches
no route.

## Parameters

| Parameter | Type |
| ------ | ------ |
| `router` | [`ServerRouter`](../../index/interfaces/ServerRouter.md) |
| `options?` | [`ServerRouterPluginOptions`](../interfaces/ServerRouterPluginOptions.md) |

## Returns

[`ServerRouterPlugin`](../interfaces/ServerRouterPlugin.md)

## Throws

from the build step when the entry has no default export function,
the built `index.html` is missing, or a listed path matches no route

## Example

```ts
import type { ServerRouter } from 'ui-router-server';
import { serverRouterPlugin } from 'ui-router-server/vite';
import { defineConfig } from 'vite';

declare const router: ServerRouter;

export default defineConfig({
  plugins: [serverRouterPlugin(router, { prerender: { entry: './src/prerender.ts' } })],
});
```
