Skip to content

@ogygia/core · API

@ogygia/core/plugin

A plugin's server half. defineServer and its hooks, the app and request contexts, request facts, shared hooks, region values, late regions and route config for routers.

On this page

This page is generated from the package’s own .d.ts files on every build, so the signatures, descriptions and examples below always match the source. ogygia/plugin re-exports the same module. The plugin API is internal and can change in any release (Core). Server hooks, Contexts and facts and Helpers explain it.

Exports deferredRegion · defineServer · late_chunk · late_slot · og_html_region · open_late_scope · region · register_route_config · take_late_regions · LATE_BOOT_SCRIPT · STORED_DOCUMENT_META · HandleContext · HtmlPass · LateRegion · PluginCapabilities · Reads · RequestContext · RequestFacts · ResolveOptions · ServerApp · ServerHooks · CapabilityOpened · DevtoolsEventInput · Handle · RouteConfig · RouteConfigMatcher · ServerPlugin

import { … } from '@ogygia/core/plugin';

deferredRegion

function

A deferred region value whose HTML the browser fetches from url (a capability the plugin minted, '' for an inert one). HTML only: whatever islands the answer carries wake themselves.

function deferredRegion(id: string, url: string): DeferredRegion;

defineServer

function

Typed identity: a plugin's server half.

function defineServer<Options = undefined, State = undefined>(plugin: ServerPlugin<Options, State>): ServerPlugin<Options, State>;

late_chunk

function

A late chunk: inert template + nothing else — the boot script (or the runtime, post-SPA-swap) performs the swap. Multiple chunks for one slot apply in order (progressive states).

function late_chunk(id: string, html: string): string;

late_slot

function

Wrap html as the swappable slot id (display:contents — layout-invisible).

function late_slot(id: string, html: string): string;

og_html_region

function

A pure-HTML region — what import.meta.og.code() / .md() inline. The html is baked at build (Shiki / markdown), so this is an INLINE region whose component ({@link RawHtml }) simply renders the html in the page's own SSR pass. Static by construction: it ships as HTML and no client JS of its own. Rendered like any region: <Region of={…} />.

function og_html_region(html: string): InlineRegion;

open_late_scope

function

ONLY A RENDERER THAT DELIVERS LATE CHUNKS ARMS THEM. The server router's streamed document opens this request's late scope before it renders (and drains it after); anywhere else — a Kit page render — a promise of registers nothing, because nothing would ever fill its slot (a Kit page's header sat in an empty slot forever).

function open_late_scope(): void;

region

function

Make a held region with type-checked props: a value you can pass around and render with <Region of={f} />.

  • A plain component import → an inline region (renders in this pass; can't cross the wire).
  • A component imported with { region: 'raw' } → a dual region: inline where it's made, a signed ticket where it travels (the transport drops the component and signs it, so it arrives as a deferred region). Minting the marked form is server-only (the signer lives on the SSR leg) — call it in a load / remote / render context; on the client it throws. A component marked with wake: is a binding too, and on the client it renders inline.

A dual region is awaitable. await region(Card, props) renders the component to HTML on the server (snippets in the props in their portable form) and bakes it into the ticket, so the client swaps it in with no extra request. The awaited value is a plain copy, not a thenable. In an async generator (query.live) or an async remote, yield region(…) / return region(…) is awaited by the language, so the HTML travels automatically — LiveView over the channel you already have. A held region you don't await renders inline where it lands (first paint, same SSR pass). A dual region with no server renderer is not awaitable; an inline one awaited on the client settles to a plain copy.

region.snippet() is the snippet form of a region.

const region: typeof make_region & {
  snippet: typeof region_snippet;
}

register_route_config

function

A routes() table joins (keyed: dev re-evaluates hooks.server.ts and calls routes() again; a matcher with the same key REPLACES its stale twin instead of shadowing it).

function register_route_config(key: string, matcher: RouteConfigMatcher): void;

take_late_regions

function

The router drains THIS request's registered late regions after the document render (through this seam, never by importing hooks — its virtual: imports don't resolve outside Vite). null when nothing registered / nothing armed. Draining clears the request's list.

function take_late_regions(): LateRegion[] | null;

LATE_BOOT_SCRIPT

const

The classic inline boot: observes streamed-in <template data-og-late> nodes DURING parse and swaps each into its slot immediately (stylesheet links hoist to head first so the swap never paints unstyled). Kept dependency-free and tiny — it must run before any module script can.

const LATE_BOOT_SCRIPT: string

STORED_DOCUMENT_META

const

Stamped into a document served FROM a store (not the render that just happened): the browser runtime then treats render: 'live' lakes as stale on first mount and revalidates them.

const STORED_DOCUMENT_META = "<meta name=\"ogygia-stored\" content=\"hit\">"

HandleContext

interface

A request as an AROUND hook sees it: before anything is decided about it.

HandleContext.event

Kit's own event, untouched.

HandleContext.options

HandleContext.app

interface HandleContext<Options = undefined> {/*…*/}
readonly event: RequestEvent;
readonly options: Options;
readonly app: ServerApp<Options>;

HtmlPass

interface

An app's own pass over the HTML ogygia sends (ServerPlugin.pass).

HtmlPass.document

A document, one chunk at a time as Kit hands them out, in order. csr: Kit hydrates the page; answer: the router fetched it, so it is rendered as an answer; head / body: the chunk carries </head> / </body>; done: the last chunk. Out: the chunk's HTML, what goes before </head> (read when head), and what goes first before </body> (read when body), in the document's one assembly.

HtmlPass.region

A region answer (a hole, a batched hole, a lake remount), fully assembled.

interface HtmlPass {/*…*/}
document(html: string, page: {
    csr: boolean;
    answer: boolean;
    head: boolean;
    body: boolean;
    done: boolean;
  }): Promise<{
    html: string;
    head: string;
    body: string;
  }>;
region(html: string): Promise<string>;

LateRegion

interface

LateRegion.id

LateRegion.promise

interface LateRegion {/*…*/}
id: string;
promise: Promise<unknown>;

PluginCapabilities

interface

A capability URL for an endpoint the plugin serves (a hole the browser fetches later), held to the rules of the app's own region holes: the same lifetime, expiry in unix seconds, and the same answer once one has expired, so the runtime renews it the way it renews a region hole. The URL carries the plugin's own fields first, then exp and sig.

PluginCapabilities.mint

Sign a capability minted now: its exp (long on a page a plugin may store or a shared cache keeps, else the app's ttl), the sig over message(exp), and k, the signing key's fingerprint. The URL carries all three as params of those names: another deployment then refuses it as foreign-key, not as a bad signature.

PluginCapabilities.open

Check a request for a capability mint made, by its URL's exp and sig (with renew=1, the runtime's one retry of an expired one), charging the visitor's region budgets as a region hole does (the probe before the signature, the render after). Refused: the response to answer with, the 403 or 429 a region request gets. Accepted: headers to send with the answer (a renewal's carry the fresh URL).

interface PluginCapabilities {/*…*/}
mint(message: (exp: string) => string): {
    exp: string;
    sig: string;
    k: string;
  };
open(event: RequestEvent, message: (exp: string) => string): CapabilityOpened;

Reads

interface

One render's read set: what it read that is not the same for every visitor, or for every page. A cache keeps a render only for the callers it is the same for (server/render-cache.ts, a plugin's page store). Every way ogygia learns of such a read notes it in one:

  • a flag decided for the visitor (@ogygia/flags), a streamed load, a plugin's ctx.perVisitor;
  • a cookie, personal header, locals value, client address, platform value or credentialed fetch the render saw (observe_reads);
  • the page.data or page.url a hole's tree read (server/render-page.ts). The page is the render's identity, not the visitor's: ogygia's own look-up of it is no read at all.

A request's facts hold its read set (facts.reads). Work that may be kept (a cached hole render) runs in its own set under it, its parent (server/read-trace.ts): a read lands in the innermost set and in every set around it, so a hole rendered into a page makes that page per-visitor too. A plain shape, not a class: core's own types and its built types (and a plugin's copy of this module) must agree on it. ReadSet is the one that keeps the state.

Reads.perVisitor

The first per-visitor input read (flag:<name>, cookie:<name>, header:<name>, locals.<key>, a plugin's reason), or null.

Reads.pageData

The render read its page (page.data, or the url of the page it renders for): its output is one page's.

Reads.visitor

A per-visitor input was read (the first one names the set).

Reads.page

The render's page was read.

interface Reads {/*…*/}
readonly perVisitor: string | null;
readonly pageData: boolean;
visitor(input: string): void;
page(): void;

RequestContext

interface

One request, as one plugin sees it.

RequestContext.event

Kit's own event, untouched.

RequestContext.path

The decoded pathname.

RequestContext.forPage

This request renders on behalf of a page it is not (a remote function call, the islands endpoint): its URL may be that page's, but it is not a request for the page.

RequestContext.state

This plugin's per-request state, from its state().

RequestContext.options

RequestContext.app

RequestContext.facts

The request's shared facts.

RequestContext.routeConfig

This request's value for a key this plugin claimed inside Kit's config page option (b.routeConfig(key): export const config = { <key>: … } on a Kit page or layout, merged the way Kit merges it, or a programmatic router's config), or undefined when nothing declares it.

RequestContext.renderHole

Render one signed hole in place, with this request's event (its cookies ride in): the hole's HTML, or null when it cannot render now (the stored fallback stays, the browser fetches).

RequestContext.perVisitor

Name the first reason this render is per-visitor (later reasons do not replace it).

interface RequestContext<Options = undefined, State = undefined> {/*…*/}
readonly event: RequestEvent;
readonly path: string;
readonly forPage: boolean;
readonly state: State;
readonly options: Options;
readonly app: ServerApp<Options>;
readonly facts: RequestFacts;
routeConfig(key: string): unknown;
renderHole(endpoint: string): Promise<string | null>;
perVisitor(reason: string): void;

RequestFacts

interface

RequestFacts.storable

This render MAY be stored and served to others later (a plugin decided so before the render): the holes it mints get prerender-grade capabilities, and data-source receipts are kept.

RequestFacts.reads

What this request's render read that is not the same for every visitor (a flag, a streamed promise, a personal cookie or header): reads.perVisitor names the first, or is null.

RequestFacts.streamed

The page streams load promises after the document.

RequestFacts.private

The response belongs to one browser (devtools switched on for it): never store or share it.

RequestFacts.answerForm

An app transform renders this router-fetched page as an ANSWER, a form a store keyed by path does not hold.

RequestFacts.sources

Data-source receipts (s:<id>:<fp>) the render filed — collected only once a plugin asks for them by setting a list here (a storable render's reverse index, a fragment's provenance).

RequestFacts.assignments

Every flag decided for this request (name → variant): federation carries them as claims.

RequestFacts.codeOrigins

Origins island code on this page may load from, learned by the render (a peer fragment's own runtime origin): written on the runtime's script with the plugins' (codeOrigins).

interface RequestFacts {/*…*/}
storable: boolean;
readonly reads: Reads;
streamed: boolean;
private: boolean;
answerForm: boolean;
sources: string[] | null;
assignments: Record<string, string>;
codeOrigins?: string[];

ResolveOptions

interface

Kit's resolve() options.

ResolveOptions.transformPageChunk

ResolveOptions.filterSerializedResponseHeaders

ResolveOptions.preload

interface ResolveOptions {/*…*/}
transformPageChunk?(input: {
    html: string;
    done: boolean;
  }): MaybePromise<string | undefined>;
filterSerializedResponseHeaders?(name: string, value: string): boolean;
preload?(input: {
    type: "font" | "css" | "js" | "asset";
    path: string;
  }): boolean;

ServerApp

interface

The server process, as one plugin sees it (handed to setup, and on every ctx.app).

ServerApp.name

The plugin's name (its package name).

ServerApp.options

The options the app gave the plugin in vite.config (JSON).

ServerApp.notify

Call a shared hook on every plugin that has it.

ServerApp.ask

Ask a shared hook: the first answer that is not undefined, in plugin order.

ServerApp.facts

A request's facts (made on first touch) — for the plugin's own code outside a hook.

ServerApp.current

The facts of the request the running code belongs to (a load, a render), or null outside one.

ServerApp.perVisitor

A per-visitor read by request's code (a flag decided for its visitor): it lands in the read set of the render running now and in the request's (facts.reads), so no cache keeps that render for another visitor. The first reason names it.

ServerApp.observeReads

Note event's personal reads from now on (a cookie, a personal header or the headers whole, a locals value that is not its default, the client address, per-request platform data, a fetch that carries the visitor's cookies) the same way. Once per request.

ServerApp.sign

Sign message with the app's region key (HMAC) in this plugin's own domain: a capability only this app can mint, which never checks for another plugin or as a region capability.

ServerApp.verify

Check a signature sign made (constant-time).

ServerApp.deny

The refusal ogygia's own endpoints answer a bad signature with: a 403 no cache keeps, saying why (x-ogygia-deny) and naming the key it checked with (x-ogygia-key, its fingerprint).

ServerApp.capability

Signed capabilities for an endpoint the plugin serves, held to the rules of region holes.

ServerApp.devtools

Record a server-realm event into the current request's devtools stream (a no-op unless devtools is on for that browser).

ServerApp.keepAlive

Keeps work alive past the response of the request the running code belongs to: it is handed to the platform's waitUntil when there is one. Outside a request, or on a platform without one, it runs on in the process. Never throws. An outbox's waitUntil.

ServerApp.warn

Warn once per message (of the last 1024 said), prefixed with the plugin's name.

interface ServerApp<Options = undefined> {/*…*/}
readonly name: string;
readonly options: Options;
readonly notify: Notify;
readonly ask: Ask;
facts(request: Request): RequestFacts;
current(): RequestFacts | null;
perVisitor(request: Request, reason: string): void;
observeReads(event: RequestEvent): void;
sign(message: string): string;
verify(message: string, signature: string): boolean;
deny(why: CapabilityDeny): Response;
readonly capability: PluginCapabilities;
devtools(event: DevtoolsEventInput): void;
keepAlive(work: Promise<unknown>): void;
warn(message: string): void;

ServerHooks

interface

The shared hooks. Each is optional on a plugin; the signature is the contract.

ServerHooks.invalidate

Drop every stored page carrying any of tags (a peer said they are stale). Never re-announced.

ServerHooks.invalidated

Stored pages were dropped: tags names what (p:<path>, s:<source>:<fp>), or 'all'.

ServerHooks.lastBuild

Record version as the build the shared store last saw; answer the one before (or undefined).

ServerHooks.measures

Is this request's visitor one the plugin measures for, proved by its own credentials (the profiler: its logged-in user)? Such a visitor's region answers say how loaded the server is (server-timing). A plugin that cannot tell answers undefined.

ServerHooks.codeOrigins

The origins island code on this app's pages may load from besides its own (a federation shell's peers). Sync, and every implementation's answers count: read through code_origins, written on the runtime's script (runtime/code-origins.ts).

interface ServerHooks {/*…*/}
invalidate(tags: string[]): void | Promise<void>;
invalidated(tags: string[] | "all"): void | Promise<void>;
lastBuild(version: string): string | undefined | Promise<string | undefined>;
measures(event: RequestEvent): boolean | undefined | Promise<boolean | undefined>;
codeOrigins(): readonly string[];

CapabilityOpened

type

What capability.open decided.

type CapabilityOpened = {
  ok: true;
  headers: Record<string, string>;
} | {
  ok: false;
  response: Response;
};

DevtoolsEventInput

type

What a call site passes to emit — the envelope is added by the bus.

type DevtoolsEventInput = CompileEventInput | ServerEventInput | WireEventInput | RuntimeEventInput | HubEventInput | NavEventInput;

Handle

type

Kit's Handle hook.

type Handle = (input: {
  event: RequestEvent;
  resolve: (event: RequestEvent, opts?: ResolveOptions) => MaybePromise<Response>;
}) => MaybePromise<Response>;

RouteConfig

type

A route's merged config, as far as the claimed keys go.

type RouteConfig = Readonly<Record<string, unknown>>;

RouteConfigMatcher

type

Per matcher: undefined = not mine (ask the next one, then the file world) · an object = mine, with this merged config (a claimed key it lacks answers undefined, never the file world).

type RouteConfigMatcher = (pathname: string) => RouteConfig | undefined;

ServerPlugin

type

A plugin's server half: lifecycle hooks plus any shared hooks it implements.

type ServerPlugin<Options = undefined, State = undefined> = Partial<ServerHooks> & {
  setup?(app: ServerApp<Options>): void;
  state?(): State;
  handle?(ctx: HandleContext<Options>, next: (options?: ResolveOptions) => Promise<Response>): MaybePromise<Response>;
  request?(ctx: RequestContext<Options, State>): MaybePromise<void | Response>;
  page?(ctx: RequestContext<Options, State>): MaybePromise<void | Response>;
  response?(ctx: RequestContext<Options, State>, response: Response): MaybePromise<void | Response>;
  done?(ctx: RequestContext<Options, State>): void | Promise<void>;
  pass?(event: RequestEvent): HtmlPass | undefined;
};