Guards
Server mode only. A guard.ts can stop a request before the page renders, send the visitor to the sign-in page, or add headers to the response.
On this page
Placement
src/routes/
guard.ts
layout.tsx
page.tsx
admin/
guard.ts
page.tsx
[id]/
page.tsxA guard.ts can sit in a directory at any level. It runs before a request for any URL below that directory is handled. When several lie along a URL, they run outer first, one at a time.
Opening /admin/42 with this layout runs the root guard.ts, then admin/guard.ts. The page renders only when both let the request through.
A guard.ts in a group runs only for the routes inside that group. For one that redirects / alone to a locale’s URL, see Routing.
Return value
src/routes/admin/guard.tsimport { href } from '@k8ordo/framework';
import { cookies, redirect } from '@k8ordo/framework/server';
import type { Guard } from '@k8ordo/framework/server';
import { sessionOf } from '../../lib/session.server';
const guard: Guard<'/admin'> = async () => {
const session = await sessionOf(cookies().get('session'));
if (session === null) redirect(href('/login'));
`redirect()` ends it with a 303; `href()` adds Vite’s `base`
if (!session.admin) return new Response('admins only', { status: 403 });
A returned `Response` is the response
};
Returning nothing lets the request through
export default guard;A guard ends a request three ways. redirect(url) answers with a 303 to that URL. notFound() answers with the nearest not-found.tsx under a 404. Both throw, so the lines after them never run. An answer that is not a redirect, such as a 401 or a 403, is a returned Response. Whichever way, guards further in and the page do not run, and the cookies and headers the guard wrote first go out with the answer.
redirect() takes a URL, not a pattern in the route table’s terms like a redirect.ts target, so build it with href(), as a link does; the result carries Vite’s base. Called while a page renders, redirect() fails with an error that starts redirect() ends what answers the request.
A guard receives { request, params }. request is the Request for the page: a client navigation’s payload request is handed over under the page’s URL, query included, so new URL(request.url).pathname is the page’s path either way. params are the parameters of the directory’s pattern, still the raw strings from the URL.
The type is Guard from @k8ordo/framework/server, with the directory’s pattern as its type argument. The generated route table checks every guard.ts against that pattern as well.
Note
Returning anything but a Response or undefined is an error: a guard.ts ends the request with redirect() or notFound(), or by returning a Response, and returns nothing to let it through.
Response headers
src/routes/guard.tsimport { responseHeaders } from '@k8ordo/framework/server';
export default function guard() {
responseHeaders().set('x-content-type-options', 'nosniff');
}responseHeaders() returns the Headers of the final response. Headers set on it are added even when the guard lets the request through, whether the response is the page, its payload, the not-found, or a Response a guard further in returned. A header with the same name is replaced.
responseHeaders() can be called only inside a guard.ts, a route.ts or a Server Action. Called in a page render, it fails with an error that starts responseHeaders() belongs to what answers the request. Called outside a request, the error starts responseHeaders() needs a request.
The Content-Security-Policy header is written in the root guard.ts too. See CSP.
Rewriting after the render
There is no next() that runs the page and hands its answer back to be rewritten. A page streams, and its headers are sent before its body is finished. A guard decides before the page starts rendering.
Covered requests
These requests below its directory are covered:
- a page, and its payload fetched by a client navigation
- a
HEADfor that page - a Server Action posted to that page
- a
route.tsor anot-found.tsxbelow it
The root guard.ts also runs for a URL no route matched.
Note
A guard does not protect a Server Action itself. An action is a function any page can call, and it is posted to the URL of the page that calls it. Check what the action needs inside the action as well.
Execution order
A guard runs after the params declarations have read the params, and before the Server Action and the render. A redirect.ts answers before any guard. A guard under [locale] already has the locale the URL names, and a Server Action after it can read the cookies the guard wrote.