Options and files
The options of framework(), the files src/routes/ may hold, and what each entry point of @k8ordo/framework exports. How to use each is in the guide pages.
On this page
Entry points
@k8ordo/framework splits its entry points by where the code runs. No entry point that a deployed application loads imports Vite.
@k8ordo/framework: the types and functions the application’s code imports. Thefieldaparamsandsrc/env.tsare written with comes from@k8ordo/field.@k8ordo/framework/vite:framework(), the plugin forvite.config.ts, andscriptHash(), which hashes an inline script. It loads Vite.@k8ordo/framework/server: server mode only. The functions and types that code inside the handler imports. In static mode, a module that imports a value from it fails the build. It needs nothing from Node.js butAsyncLocalStorage, so it runs wherever the handler does.@k8ordo/framework/env: either mode.env, the server’s environment as theserverdeclaration insrc/env.tsread it, for code the handler runs. A module the browser loads that imports it fails the build.@k8ordo/framework/serve:serve(), which runs a build on Node.js. A server mode build’sdist/server.jscalls it, and a server of your own can too. A static mode build it serves the way a static host does.@k8ordo/framework/vercel:vercel(), the plugin that writes the build for Vercel, in either mode, and the types of its options,VercelOptionsandVercelRoute.@k8ordo/framework/generated: what the generated.k8ordo/files import. Code written by hand does not import it.k8ordo generate: the package’s command. Run in the application’s directory, it writes.k8ordo/from the Vite config and builds nothing.
framework
Import from @k8ordo/framework/vite
The Vite plugin that builds the application. It returns an array holding React’s plugin and the RSC pipeline, so pass it to plugins as it is.
framework(options: FrameworkOptions): PluginOption[]Parameters
options.mode'static' | 'server''static'or'server', with no default.'static'renders pages at build time, into an app that runs with no server.'server'renders pages per request, in an app that runs on a server.options.reactCompilerboolean- Either mode. With
true, client components are compiled with React Compiler (the Rust one, through@vitejs/plugin-react’scompiler). The application needsoxc-transform-reactinstalled. Server Components, and the HTML a client component renders on the server, are left as written.falseby default. options.headersReadonly<Record<string, string>>- Either mode. Headers every response carries, whether a page, a file of the build, a
404or a redirect.vite dev,vite previewandservesend them, the handler adds them to its own answers under server mode, and a static build writes them into_headers(orvercel()’s routes). A header aguard.tsor aroute.tsset under the same name stays. options.sitestring- Required under static mode. The origin the site is served from, with no path, such as
https://example.com. Every request the build makes to a page or aroute.tsis under it, so it is the origin of therequest.urlaroute.tsreceives. The build also writessitemap.xml, listing the pages it wrote at this origin; aroute.tsthat writessitemap.xmlis used instead. options.cspReadonly<Record<string, readonly string[]>>- Static mode only. The Content-Security-Policy every page carries, as an array of sources per directive. The build adds the hashes of the framework’s scripts and writes it into a
<meta>. The application’s own inline scripts are allowed by writing their hashes here:scriptHash()from@k8ordo/framework/vitereturns the hash of a script the application writes, andcolorSchemeScriptHash()that of the@k8ordo/color-schemescript.
Returns
PluginOption[] — An array of Vite plugins.
Caveats
siteorcspwritten besidemode: 'server'is a type error.- The route files always live in
src/routes/. No option moves them. - A key the mode does not take fails when the config loads, naming the keys it does:
framework() takes no routesDir — under mode: 'static' it takes mode, reactCompiler, headers, site, csp. - With
reactCompiler: trueand nooxc-transform-reactinstalled,vite devandvite buildstop as soon as they read the config, with React’s plugin’s errorReact Compiler requires the optional …. Thereact({ compiler: true })it names is the optionframework()passes for you. - When
cspholds a directive that works only from a header, or'strict-dynamic',framework()fails as soon as it is called, with the errorthe "csp" option cannot go into a page's <meta> as it is.
vite.config.tsimport { framework } from '@k8ordo/framework/vite';
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [
framework({
mode: 'static',
site: 'https://example.com',
}),
],
});scriptHash
Import from @k8ordo/framework/vite
Resolves to the SHA-256 of an inline script’s text as a CSP source, 'sha256-…'. Put it in static mode’s csp.
scriptHash(text: string): Promise<string>Parameters
textstring- The text the
<script>is given.
Returns
Promise<string> — A CSP source such as 'sha256-…', quotes included.
Caveats
- The text is hashed as it is. A single space more or less makes another script, and it is blocked.
- A line break counts as the browser parses the page, a CR LF as one LF, so a text read from a file with CR LF hashes as the script that runs.
vite.config.tsimport { framework, scriptHash } from '@k8ordo/framework/vite';
import { defineConfig } from 'vite';
import { densityScript } from './src/lib/density-script';
export default defineConfig({
plugins: [
framework({
mode: 'static',
site: 'https://example.com',
csp: {
'script-src': ["'self'", await scriptHash(densityScript)],
},
}),
],
});Route files
The files a directory under src/routes/ may hold, with what each exports and receives. Any other file lives outside src/routes/: components in src/components/, every other module in src/lib/.
page.tsx
- Default export: the page component for the directory’s URL. It receives
paramsandpathname. - In server mode, it also receives
request, aRouteRequest. A'use client'file does not. params: the declaration that reads the parameters, afield.object({ … }). Optional.search: server mode only. The declaration of the query the page reads, theurlof a@k8ordo/statedefinition. A page that exports it receives the values read assearch.
layout.tsx
- Default export: a component that wraps what renders below it, received as
children. It also receivesparamsandpathname.paramsstays typed as strings. - In server mode, it also receives
request, aRouteRequest. A'use client'file does not. params: read for every page below, before the page’s own declaration.
not-found.tsx
- Default export: the component rendered for a URL below the directory that no route matched. It receives
paramsandpathname. Theparamsare unvalidated strings. - In server mode, it also receives
request, aRouteRequest. A'use client'file does not. - In static mode, it is written as
404.html, so an application may hold only one. In server mode, it answers with a 404 status, and each directory may hold its own.
error.tsx
- A
'use client'file. - Default export: a component that receives
errorandreset. It renders in place of what is below it when that throws. It receives noparams.
loading.tsx
- Default export: a component with no props. It renders as the
<Suspense>fallback while the page below it loads.
fallback.tsx
- Default export: a component with no props. Under static mode it renders in place of the
page.tsxbeside it, for a URL whose value nopaths.tsreturned. - It sits beside a
page.tsxthat has a parameter. Its directory may not hold alayout.tsx. - Server mode does not use it:
page.tsxrenders every value per request.
paths.ts
- Default export: a function that returns the values of its directory’s parameter, as an array or a promise of one, typed as a link’s
href()takes them. It receives the parameters of the directories above it as theparamsexports of the layouts above its directory read them. Type it by the pattern down to its directory, as inPaths<'/:locale/posts/:id'>. - It sits in a parameter’s directory, such as
[id]. Its values apply to the page beside it and to every page,route.tsandredirect.tsbelow. - A static mode build and
vite devcall it. Server mode does not read it.
redirect.ts
- Default export: the target as a string, or
{ to, permanent }.tois a pattern of the route table. Its parameters are filled with the matched values. - In static mode, it is written as a page that sends the visitor to the target. In server mode, it answers
GETandHEADwith a307, or a308whenpermanent.
route.ts
- Exports named after methods, such as
GETandPOST: functions that receive{ request, params }and return aResponse. params: a declaration that reads the parameters, as a page’s does.- In static mode, only
GETmay be exported, and its response is written as the file at that URL. In server mode, any of the seven methods may be exported.
guard.ts
- Server mode only. Default export: a function of type
Guard. It runs before anything below the directory responds. Returning aResponseanswers the request there. Returning nothing goes on to the page or theroute.ts.
Application API
import {
bindParams,
href,
isNotFound,
matchPath,
normalizePathname,
notFound,
useMatch,
usePathname,
usePendingPathname,
withBase,
} from '@k8ordo/framework';
import type {
BoundParams,
ErrorProps,
LayoutProps,
MatchablePattern,
MatchOptions,
PageProps,
Paths,
RedirectTarget,
RegisteredNavigablePattern,
RegisteredPageParams,
RegisteredParams,
RegisteredPattern,
RouteContext,
} from '@k8ordo/framework';Most are exports of @k8ordo/router, re-exported by name. The framework’s own are Paths and RedirectTarget, the types of a paths.ts’s and a redirect.ts’s default export. useParams is not among them: it reads a <Router> the framework never renders. A page receives params as a prop instead. The router’s are described on the API page of @k8ordo/router.
RedirectTarget
Import from @k8ordo/framework
The type of what a redirect.ts default-exports: a path of the route table, or a URL on another origin. Write satisfies RedirectTarget, and a path the table lacks is a type error where it is written. A redirect.ts works in either mode, so its type is imported from @k8ordo/framework.
type RedirectTarget =
| RedirectTo
| { readonly to: RedirectTo; readonly permanent?: boolean };
type RedirectTo =
| PathFor<RegisteredNavigablePattern>
| `https://${string}`
| `http://${string}`;cookies
Import from @k8ordo/framework/server
Server mode only. Reads and writes the request’s cookies. Call it from a guard.ts, a route.ts or a Server Action.
cookies(): CookiesFields
get(name: string) => string | undefined- Reads a value by name.
undefinedwhen there is none. has(name: string) => boolean- Whether a cookie of that name exists.
set(name: string, value: string, options?: CookieOptions) => void- Writes a value. The third argument takes the attributes of
CookieOptions. delete(name: string, scope?: CookieScope) => void- Deletes a cookie. Pass the
pathanddomainit was written with.
Caveats
- A read reflects what was written earlier in the same request. A name not written yet returns the value the request carried.
- Every write goes to the browser as a
Set-Cookieon the response, whatever the response is. - Called while a page renders, it fails with
cookies() belongs to what answers the request. A page readsrequest.cookiesinstead.
cookies().get('session');
cookies().set('session', token, { maxAge: 60 * 60 * 24 });
cookies().delete('session');CookieOptions
Import from @k8ordo/framework/server
The cookie attributes passed to cookies().set() as its third argument. The defaults suit a session cookie.
type CookieOptions = {
readonly path?: string;
readonly domain?: string;
readonly maxAge?: number;
readonly expires?: Date;
readonly httpOnly?: boolean;
readonly secure?: boolean;
readonly sameSite?: 'strict' | 'lax' | 'none';
};Fields
pathstring/by default.domainstring- Not set by default.
maxAgenumber- The lifetime in seconds.
0expires it at once. expiresDate- The expiry, as a
Date. httpOnlybooleantrueby default, so a page’s script cannot read it.securebooleantrueby default, so it is sent over HTTPS only. For an HTTP request tolocalhost,127.0.0.1or[::1], the default isfalse.sameSite'strict' | 'lax' | 'none''lax'by default.'none'is alwayssecure, by default too. Passing it withsecure: falseis an error.
responseHeaders
Import from @k8ordo/framework/server
Server mode only. Returns the Headers the final response will carry. Call it from a guard.ts, a route.ts or a Server Action.
responseHeaders(): HeadersCaveats
- A header set here goes on the response, whether that is a page’s HTML, its RSC payload or a
Responseaguard.tsreturned. A header the response already carries is replaced. - Outside a request it fails with
responseHeaders() needs a request, and inside a page render withresponseHeaders() belongs to what answers the request.
requestHeaders
Import from @k8ordo/framework/server
Server mode only. Returns the headers the request arrived with. Call it from a guard.ts, a route.ts or a Server Action. It is how a Server Action, which receives only its arguments, reads the headers.
requestHeaders(): HeadersCaveats
- Outside a request it fails with
requestHeaders() needs a request, and inside a page render withrequestHeaders() belongs to what answers the request.
redirect
Import from @k8ordo/framework/server
Server mode only. Ends a Server Action, a guard.ts or a route.ts and redirects to another URL.
redirect(to: string): neverParameters
tostring- The target URL. It is sent as written, so build it with
href().
Returns
never — Never returns. The lines after it never run.
Caveats
- It ends by throwing, so do not call it inside a
try. - A form posted before JavaScript loaded gets a
303. A call from the client runtime gets a payload that tells the router where to go. - In a
guard.tsor aroute.ts, it answers with a303and alocation.
nonce
Import from @k8ordo/framework/server
Server mode only. Returns the nonce this response’s inline scripts carry. It is new for every request.
nonce(): stringCaveats
- While the request is being handled, the render included, it returns the same value.
- Outside a request it fails with
nonce() needs a request.
waitUntil
Import from @k8ordo/framework/server
Server mode only. Keeps the host from stopping the process or the function until the promise settles. Use it for work that goes on after the response is sent.
waitUntil(promise: Promise<unknown>): voidParameters
promisePromise<unknown>- The promise of work that has already started.
Caveats
- It can be called while the request is being handled, the render included.
- The handler logs the error of a promise that rejects, once. It does not become an unhandled rejection.
- Outside a request it fails with
waitUntil() needs a request. - On Vercel it needs
@vercel/functionsinstalled.
HandlerContext
Import from @k8ordo/framework/server
Server mode only. The type of the second argument the built handler takes. A runtime that calls the handler itself passes its own waitUntil.
type HandlerContext = {
readonly waitUntil?: (promise: Promise<unknown>) => void;
};Fields
waitUntil(promise: Promise<unknown>) => void- The handler calls it for every promise the application passed to
waitUntil(). The promise it receives never rejects.
Guard
Import from @k8ordo/framework/server
Server mode only. The type of what a guard.ts default-exports. Pass its directory’s pattern as the type argument.
type Guard<P extends string = string> = (
context: GuardContext<P>,
) => Response | void | Promise<Response | void>;Fields
requestRequest- The
Requestas it arrived. paramsParamsOf<P>- The parameters of the directory’s pattern, as the URL’s strings: a
guard.tsruns above every layout, where no declaration has typed them.
RouteRequest
Import from @k8ordo/framework/server
Server mode only. The type of the request a page, a layout and a not-found.tsx receive. Both fields are read-only.
type RouteRequest = {
readonly headers: Headers;
readonly cookies: ReadonlyMap<string, string>;
};Fields
headersHeaders- The request’s headers.
cookiesReadonlyMap<string, string>- The
Cookieheader parsed by name. When a name appears twice, the first value is used.
serve
Import from @k8ordo/framework/serve
Runs the build on a Node.js HTTP server. Once it is listening, it returns its URL and a function to stop it.
serve(options?: ServeOptions): Promise<Server>Parameters
options.diststring- The build output directory.
distby default, resolved from the working directory. options.portnumber- The port to listen on.
3000by default.0lets the system pick a free one. options.hoststring- The host to listen on. The default,
localhost, is reachable from the same machine only, so pass0.0.0.0inside a container.
Returns
Promise<Server> — The server, once it is listening.
Fields
portnumber- The port it actually listens on.
urlstring- Its URL, in the form
http://<host>:<port>. close() => Promise<void>- Stops the server. The promise resolves once it has stopped.
Caveats
- To call it from a server of your own, put
@k8ordo/frameworkindependenciesand install it for run time as well. Thedist/server.jsa server mode build writes has it bundled in. - For a server mode build, a
GETorHEADthat names a file indist/client/gets that file. Everything else goes to the handler. - When the handler throws, it responds with a
500and the bodyinternal errorand logs what was thrown. - For a static mode build, the handler is never called. It looks for the file, then a rewrite in
_redirects, then404.html, and answers any method butGETandHEADwith a405.
serve.jsimport { serve } from '@k8ordo/framework/serve';
const server = await serve({ port: 3000 });
console.log(server.url);vercel
Import from @k8ordo/framework/vercel
A plugin that has vite build also write .vercel/output/ in the shape of Vercel’s Build Output API (v3). Put it beside framework(), in either mode.
vercel(options?: VercelOptions): PluginParameters
options.beforeFilesreadonly VercelRoute[]- Static mode only. Routes of the application’s own, tried before any file: routes of Vercel’s Build Output API (
src,dest,status,headers,continue,check,caseSensitive,has), written as given. For headers on every page, and a redirect or a rewrite that has to win over a file. options.beforeNotFoundreadonly VercelRoute[]- Static mode only. Routes of the application’s own, tried once neither a file nor a shell answered, before
404.html, in the same shape asbeforeFiles. For a proxy to another origin, or a 404 of another body.
Returns
Plugin — A Vite plugin.
Caveats
- Under server mode the function is the handler, with every dependency bundled in. A dependency left out with
resolve.externalis not in the function, and does not work on Vercel. - Under static mode there is no function: only the written files, and routes that rewrite a value the build did not write to its shell. Vercel does not read
_redirects. A file aroute.tswrote is served with theContent-TypeitsGETreturned. - Only Vercel applies the routes in
beforeFilesandbeforeNotFound:vite dev,vite previewandserve()do not. Under server mode, the build refuses them. - Vercel matches
srcagainst the whole pathname:^/posts/does not match/posts/first, so write^/posts/.+$. - Nothing in the routes is checked, so a mistake shows once deployed.
vite.config.tsimport { framework } from '@k8ordo/framework/vite';
import { vercel } from '@k8ordo/framework/vercel';
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [framework({ mode: 'server' }), vercel()],
});