[]previous and next page

redirect

Creates the signal to throw for a redirect.

Creates the signal to throw for a redirect. It is the router's own signal (so beforeLoad and loaders understand it) with an HTTP status attached, which the server uses for the Location response and for the { redirect } answer of a server function.

Import#

ts
import { redirect } from "@shadow-js/dusk";

Signature#

ts
function redirect(to: string, options?: RedirectOptions): RedirectSignal

function redirect<P extends string>(
  target: RedirectTarget<P> & { readonly status?: RedirectStatus },
): RedirectSignal

Parameters#

Name Description
to Target: an app path (/login), an absolute URL whose origin the server allows, or an object { to, params?, search?, hash?, status?, replace? } with the typed params and search that links take.
options Status and history behavior (the string form).

Returns#

The signal. throw it.

Throws#

  • RangeError for a status that is not 301, 302, 303, 307 or 308.

Examples#

ts
export const requireUser = () => { if (!signedIn()) throw redirect("/login", { status: 303 }); };
ts
export const toPost = () => redirect({ to: "/posts/:id", params: { id: "7" }, search: { tab: "edit" }, status: 308 });