ui-router-server / location / installServerLocation
Variable: installServerLocation
const installServerLocation: <T>(router, options?) => T;Defined in: location.ts:146
Installs `serverLocationPlugin` on router, points it at the requested url, and returns the same router.
It takes a router rather than constructing one: a server render drives whatever UIRouter its client uses — lit-ui-router's UIRouterLit included — and constructing one here pins that choice. Build the router yourself and hand it over; it comes back at the type it went in as.
The url is set before any transition runs, so router.start() heads for the state the request asked for. start() returns nothing, and the transition settles asynchronously, resolves included: await it before reading the router. transitionService.onSuccess fires when it lands; stateService.go() returns the transition promise where start() does not.
Type Parameters
| Type Parameter | Description |
|---|---|
T extends UIRouter | the router type, returned unchanged |
Parameters
| Parameter | Type | Description |
|---|---|---|
router | T | a freshly constructed router, with no location plugin yet |
options? | ServerLocationOptions | the request's url, the mount's shape, and the url shape |
Returns
T
router, for chaining
Example
import { servicesPlugin, UIRouter } from '@uirouter/core';
import { installServerLocation } from 'ui-router-server/location';
const { pathname, search } = new URL(request.url);
const router = new UIRouter();
router.plugin(servicesPlugin);
installServerLocation(router, { url: pathname + search, strictMode: false });
states.forEach((state) => router.stateRegistry.register(state));
const settled = new Promise<void>((resolve) => {
router.transitionService.onSuccess({}, () => resolve());
});
router.start();
await settled;