Skip to content

ui-router-server / vite / serverRouterPlugin

Function: serverRouterPlugin() ​

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

Defined in: vite.ts:440

Wraps a ServerRouter 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 at the mount's actual HTML entry when it isn't served at the base.

With prerender, vite build runs the entry after the client build and fails when a listed path matches no route.

Parameters ​

ParameterType
routerServerRouter
options?ServerRouterPluginOptions

Returns ​

ServerRouterPlugin

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