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. The field a params and src/env.ts are written with comes from @k8ordo/field.
  • @k8ordo/framework/vite: framework(), the plugin for vite.config.ts, and scriptHash(), 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 but AsyncLocalStorage, so it runs wherever the handler does.
  • @k8ordo/framework/env: either mode. env, the server’s environment as the server declaration in src/env.ts read 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’s dist/server.js calls 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, VercelOptions and VercelRoute.
  • @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.

ts
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’s compiler). The application needs oxc-transform-react installed. Server Components, and the HTML a client component renders on the server, are left as written. false by default.
options.headersReadonly<Record<string, string>>
Either mode. Headers every response carries, whether a page, a file of the build, a 404 or a redirect. vite dev, vite preview and serve send them, the handler adds them to its own answers under server mode, and a static build writes them into _headers (or vercel()’s routes). A header a guard.ts or a route.ts set 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 a route.ts is under it, so it is the origin of the request.url a route.ts receives. The build also writes sitemap.xml, listing the pages it wrote at this origin; a route.ts that writes sitemap.xml is 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/vite returns the hash of a script the application writes, and colorSchemeScriptHash() that of the @k8ordo/color-scheme script.

Returns

PluginOption[] — An array of Vite plugins.

Caveats

  • site or csp written beside mode: '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: true and no oxc-transform-react installed, vite dev and vite build stop as soon as they read the config, with React’s plugin’s error React Compiler requires the optional …. The react({ compiler: true }) it names is the option framework() passes for you.
  • When csp holds a directive that works only from a header, or 'strict-dynamic', framework() fails as soon as it is called, with the error the "csp" option cannot go into a page's <meta> as it is.
vite.config.ts
import { 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.

ts
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.ts
import { 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 params and pathname.
  • In server mode, it also receives request, a RouteRequest. A 'use client' file does not.
  • params: the declaration that reads the parameters, a field.object({ … }). Optional.
  • search: server mode only. The declaration of the query the page reads, the url of a @k8ordo/state definition. A page that exports it receives the values read as search.

layout.tsx

  • Default export: a component that wraps what renders below it, received as children. It also receives params and pathname. params stays typed as strings.
  • In server mode, it also receives request, a RouteRequest. 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 params and pathname. The params are unvalidated strings.
  • In server mode, it also receives request, a RouteRequest. 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 error and reset. It renders in place of what is below it when that throws. It receives no params.

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.tsx beside it, for a URL whose value no paths.ts returned.
  • It sits beside a page.tsx that has a parameter. Its directory may not hold a layout.tsx.
  • Server mode does not use it: page.tsx renders 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 the params exports of the layouts above its directory read them. Type it by the pattern down to its directory, as in Paths<'/: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.ts and redirect.ts below.
  • A static mode build and vite dev call it. Server mode does not read it.

redirect.ts

  • Default export: the target as a string, or { to, permanent }. to is 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 GET and HEAD with a 307, or a 308 when permanent.

route.ts

  • Exports named after methods, such as GET and POST: functions that receive { request, params } and return a Response.
  • params: a declaration that reads the parameters, as a page’s does.
  • In static mode, only GET may 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 a Response answers the request there. Returning nothing goes on to the page or the route.ts.

Application API

ts
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.

ts
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.

ts
cookies(): Cookies

Fields

get(name: string) => string | undefined
Reads a value by name. undefined when 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 path and domain it 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-Cookie on the response, whatever the response is.
  • Called while a page renders, it fails with cookies() belongs to what answers the request. A page reads request.cookies instead.
ts
cookies().get('session');
cookies().set('session', token, { maxAge: 60 * 60 * 24 });
cookies().delete('session');

Import from @k8ordo/framework/server

The cookie attributes passed to cookies().set() as its third argument. The defaults suit a session cookie.

ts
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. 0 expires it at once.
expiresDate
The expiry, as a Date.
httpOnlyboolean
true by default, so a page’s script cannot read it.
secureboolean
true by default, so it is sent over HTTPS only. For an HTTP request to localhost, 127.0.0.1 or [::1], the default is false.
sameSite'strict' | 'lax' | 'none'
'lax' by default. 'none' is always secure, by default too. Passing it with secure: false is 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.

ts
responseHeaders(): Headers

Caveats

  • A header set here goes on the response, whether that is a page’s HTML, its RSC payload or a Response a guard.ts returned. A header the response already carries is replaced.
  • Outside a request it fails with responseHeaders() needs a request, and inside a page render with responseHeaders() 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.

ts
requestHeaders(): Headers

Caveats

  • Outside a request it fails with requestHeaders() needs a request, and inside a page render with requestHeaders() 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.

ts
redirect(to: string): never

Parameters

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.ts or a route.ts, it answers with a 303 and a location.

nonce

Import from @k8ordo/framework/server

Server mode only. Returns the nonce this response’s inline scripts carry. It is new for every request.

ts
nonce(): string

Caveats

  • 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.

ts
waitUntil(promise: Promise<unknown>): void

Parameters

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/functions installed.

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.

ts
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.

ts
type Guard<P extends string = string> = (
  context: GuardContext<P>,
) => Response | void | Promise<Response | void>;

Fields

requestRequest
The Request as it arrived.
paramsParamsOf<P>
The parameters of the directory’s pattern, as the URL’s strings: a guard.ts runs 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.

ts
type RouteRequest = {
  readonly headers: Headers;
  readonly cookies: ReadonlyMap<string, string>;
};

Fields

headersHeaders
The request’s headers.
cookiesReadonlyMap<string, string>
The Cookie header 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.

ts
serve(options?: ServeOptions): Promise<Server>

Parameters

options.diststring
The build output directory. dist by default, resolved from the working directory.
options.portnumber
The port to listen on. 3000 by default. 0 lets the system pick a free one.
options.hoststring
The host to listen on. The default, localhost, is reachable from the same machine only, so pass 0.0.0.0 inside 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/framework in dependencies and install it for run time as well. The dist/server.js a server mode build writes has it bundled in.
  • For a server mode build, a GET or HEAD that names a file in dist/client/ gets that file. Everything else goes to the handler.
  • When the handler throws, it responds with a 500 and the body internal error and logs what was thrown.
  • For a static mode build, the handler is never called. It looks for the file, then a rewrite in _redirects, then 404.html, and answers any method but GET and HEAD with a 405.
serve.js
import { 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.

ts
vercel(options?: VercelOptions): Plugin

Parameters

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 as beforeFiles. 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.external is 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 a route.ts wrote is served with the Content-Type its GET returned.
  • Only Vercel applies the routes in beforeFiles and beforeNotFound: vite dev, vite preview and serve() do not. Under server mode, the build refuses them.
  • Vercel matches src against 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.ts
import { framework } from '@k8ordo/framework/vite';
import { vercel } from '@k8ordo/framework/vercel';
import { defineConfig } from 'vite';

export default defineConfig({
  plugins: [framework({ mode: 'server' }), vercel()],
});
k8ordo

React libraries that use Baseline features without holding back.