Data Loading
Load route data on the server with typed loaders.
Updated: 2026-07-26
Route loader
Export loader(ctx) from a page module. It runs before the component renders, on the server for the initial request and on the client during navigation. Its result arrives as ctx.data.
src/pages/docs/[slug].tsx
import type { SiteRouteContext } from '@rigbyhost/karui/ssr';
export async function loader(ctx: SiteRouteContext) {
const post = await db.posts.find(ctx.params.slug);
return { post };
}
export default function Page(ctx: SiteRouteContext) {
const { post } = ctx.data as Awaited<ReturnType<typeof loader>>;
return <article>{post.title}</article>;
}Not found
Call notFound() from a loader or component to hand rendering to your 404 entity with a 404 status.
tsx
import { notFound } from '@rigbyhost/karui/router';
export async function loader(ctx) {
const post = await getPost(ctx.params.slug);
if (!post) notFound();
return post;
}Handoff to the client
The server serializes its loader result into the page, so the first client render reuses that data instead of refetching. Without it the hydrated tree could differ from the markup it is adopting.
The payload key defaults to __KARUI_DATA__ and is configurable with dataKey.