SSR
Server-side rendering with @nano_kit/router is handled by a dedicated SSR renderer package for your framework. The router itself provides the building blocks — page-level Stores$ and Head$ — while the adapter takes care of the server renderer and client hydration.
Stores Data Dehydration
Section titled “Stores Data Dehydration”Export a Stores$ function from a page module to declare which signals should be dehydrated on the server and rehydrated on the client. The SSR renderer discovers it automatically.
Mark individual signals as hydratable using hydratable from @nano_kit/store:
import { signal, hydratable } from '@nano_kit/store'import { useSignal } from '@nano_kit/react'
/* Tie the signal to a unique key for dehydration */export const $data = hydratable('homeData', signal({ text: 'Hello World' }))
export function Stores$() { return [$data]}
export default function Home() { const data = useSignal($data)
return <h1>{data.text}</h1>}For pages that use DI, Stores$ is called within the request’s InjectionContext, so you can use inject() inside it:
import { inject, hydratable } from '@nano_kit/store'import { User$ } from './stores/user'
export function Stores$() { const { $user } = inject(User$)
return [$user]}Stores$ is called on the server only and is removed from the client build, see Server-Only Exports.
Head Management
Section titled “Head Management”Export a Head$ function from a page module to declare per-page document metadata. See Head Management for all available tag descriptors.
import { title, meta } from '@nano_kit/router'
export function Head$() { return [ title('Home — My App'), meta({ name: 'description', content: 'Welcome to my app' }) ]}
export default function Home() { /* ... */ }Head$ is also called within the request’s InjectionContext, so you can use inject() inside it:
import { inject } from '@nano_kit/store'import { title } from '@nano_kit/router'import { User$ } from './stores/user'
export function Head$() { const { $user } = inject(User$)
return [ title(() => `${$user().name} — My App`) ]}The SSR renderer reads Head$ and injects the tags into the HTML response automatically.
Layout modules can export Head$ too: the title, lang, dir and meta of a page replace the same tags of its layouts, see Layout Head$.
Response Status
Section titled “Response Status”The SSR renderer responds with 200 for a page and with 404 for the notFound page. Export statusCode from a page module to respond with another status:
export const statusCode = 410
export default function Gone() { /* ... */ }When the status depends on the data, export a StatusCode$ function. It is called within the request’s InjectionContext once the stores from Stores$ are dehydrated, so it reads what they have loaded:
import { inject } from '@nano_kit/store'import { Post$ } from './stores/post'
export function Stores$() { const { $post } = inject(Post$)
return [$post]}
export function StatusCode$() { const { $post } = inject(Post$)
return $post() ? undefined : 404}
export default function Post() { /* The post, or a note that there is none */ }Return undefined when the page has nothing to say: the renderer then takes statusCode, or 200 without it. Only the status changes, the page is rendered as usual, so the page itself shows that there is no such post.
StatusCode$ should read only the stores returned from Stores$, as the renderer waits for no others.
Layout modules can export StatusCode$ and statusCode too. The page answers first, with its StatusCode$ and then with its statusCode, and its layouts follow, from the nearest one. So a layout that loads a blog responds with 404 when there is no such blog, for every page inside it that gives no status of its own.
Server-Only Exports
Section titled “Server-Only Exports”Stores$ and StatusCode$ are called on the server only. The SSR Vite plugin removes them from the client build, so neither they nor the code that only they use reach the browser.
The plugin goes by one sign: the module has a default export. From such a module it removes the exports named Stores$, StatusCode$ and Cache$, a name the router keeps for the page cache, whether they are declared there or re-exported by name. A module without a default export is left as it is.
So keep the default export of a page in the same module as its Stores$ and StatusCode$:
export function Stores$() { /* ... */ }
export function StatusCode$() { /* ... */ }
export default function Post() { /* ... */ }A page kept in a folder needs this too. If only the index file has the default export, the plugin never sees Stores$ behind export *, and it stays in the client build:
/* ❌ `Stores$` of `./Post` stays in the client build */export * from './Post'export { Post as default } from './Post'
/* ✅ `./Post` has the default export itself */export * from './Post'export { default } from './Post'The stores a page shares with others need nothing special. A function that only the removed Stores$ uses, like the PostStores$ of a block or the Stores$ of another module, is dropped from the client build by tree-shaking:
import { PostStores$ } from '../blocks/Post'
export function Stores$() { return [...inject(PostStores$)]}Setting Up SSR
Section titled “Setting Up SSR”The low-level setup (virtual navigation, renderer class, client hydration) is handled by the SSR renderer. See the SSR renderer docs for a complete setup guide.