Skip to content

ui-router-server / ssr / redirectedTo

Function: redirectedTo() ​

ts
function redirectedTo(router, transition): SettledRedirect | undefined;

Defined in: ssr.ts:273

Reports where transition was redirected to, so a host answers the request with a redirect instead of the page it landed on. A transition is redirected when a redirectTo, data or function, or a hook returning a target state moved it, a change of params alone included. The status is the redirectStatusCode of the state the redirect chain started from, the state the url matched.

A url rule that rewrites the url (when('/old', '/new')) starts a fresh transition rather than redirecting one, so it reports nothing: the verdict answers a mount's pattern rules before render. A redirect with location: false leaves the url where it was and reports nothing either.

Call it with the transition settle resolved. On a router that serves more than one path, settle on the path the router already stands on resolves with the transition that brought it there, which may be an earlier path's redirect.

Parameters ​

ParameterTypeDescription
routerUIRouterthe router transition ran on, read for the url it landed on
transitionTransitionthe transition settle resolved with

Returns ​

SettledRedirect | undefined

the landed url and status, or undefined when nothing redirected it

Example ​

ts
import { redirectedTo, settle } from 'ui-router-server/ssr';

// `router` serves the mount at /releases; `subpath` is the request's path within it.
const redirect = redirectedTo(router, await settle(router, subpath));

if (redirect)
  return new Response(null, {
    status: redirect.status,
    headers: { Location: `/releases${redirect.url}` },
  });