Skip to content

API

ogygia

The runtime surface, covering Region, region(), script, preference, transport and context.

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.

Exports createContext · getContext · hydratedBy · isOgygiaPage · isRegion · keepFallback · og_derived · preference · preload · region · requestEvent · script · setContext · OgygiaBoundary · Provide · Region · Preference · TransportCodec · AwaitableRegion · DeferredRegion · DualRegion · Fallback · InlineRegion · PreferenceSpec · RegionData · RegionOptions · RegionSchedule · RegionValue

import { … } from 'ogygia';

createContext

function

A typed context, in Svelte's createContext shape: [get, set, has]. Its key is Symbol.for(name), so it crosses into regions (each wakes as its own root in the browser) and is the same key as a plain setContext(Symbol.for(name), …). The setter is setContext from ogygia, so a value set in a csr=false layout reaches every region on the page. get throws when nothing above set it, like Svelte's; has asks first.

A value with methods crosses as a wired class (static wire = import.meta.og.wire(…)): every region gets the one live instance.

import { createContext } from 'ogygia';
export const [getRuntime, setRuntime] = createContext<AppRuntime>('app-runtime');
// in a layout:  setRuntime(runtime);
// in a region:  const runtime = getRuntime();
function createContext<T>(name: string): Context<T>;

getContext

function

Retrieves the context set with the specified key in the current component or any of its ancestors. If multiple components set the same key, the value from the closest one is returned. A setContext call in the current component is only visible to getContext calls that run after it. Must be called during component initialisation.

createContext is a type-safe alternative.

function getContext<T>(key: any): T;

hydratedBy

function

function hydratedBy(): string | null;

isOgygiaPage

function

PUBLIC: is the current page an ogygia page (csr=false), as opposed to a csr=true page Kit hydrates whole? The inverse of documentIsCsrTrue, so it answers on BOTH legs with no requestEvent handling on the caller's side — server reads the request's route against the build-time csr set, client reads Kit's bootstrap. Meant for shared code (a store, a helper) that must branch on which world it runs in. Off-request on the server (a module init, a prerender helper with no page) there is no document to speak of, so it returns false.

function isOgygiaPage(): boolean;

isRegion

function

True for any value produced by region (or decoded from the wire).

function isRegion(value: unknown): value is RegionValue;

keepFallback

function

End this server island's render: the page's fallback is the right content for this visitor. Called from a server island rendering INLINE (nested in an island — the deferred mark is ignored there), the signal would escape to Kit's error page: fail with the reason instead.

function keepFallback(): never;

og_derived

function

A derived that CROSSES boundaries ALIVE — resumability for the reactive graph.

Svelte's derived is a live computation: sources + a formula. The value can serialize; the computation can't — so a plain derived crosses as a FROZEN seed (the classifier warns). This primitive remembers its RECIPE, and the recipe is made of exactly the two things the seam already carries: sources are STORES (they reunify to one live instance per island page) and the formula is a FUNCTION (mark it with import.meta.og.$ so it travels as a fn ref).

Locally it IS svelte/store's derived — same laziness, same semantics. At a boundary the derived kind ships { sources, formula, seed }; the island decodes by re-deriving against the REUNIFIED sources with the REBOUND formula: bump the source in any island and every island's derived follows. The seed rides along only for exactness of the first paint.

An UNMARKED formula still works same-tree, but the derived then can't cross (the fn ref can't be built) — the boundary explains this at the seam rather than freezing silently.

const doubled = og_derived(tally, import.meta.og.$((n) => n * 2));
function og_derived<T>(sources: AnyStore | AnyStore[], fn: (...vals: never[]) => T, initial?: T): {
  subscribe: (run: (v: T) => void) => () => void;
};

preference

function

const preference: typeof preference_impl & {
  switch: typeof preference_switch;
}

preload

function

function preload(region: unknown): 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;
}

requestEvent

function

function requestEvent<E = unknown>(): E | null;

script

function

An inline <script> tag. Pass a function and its args (shortcut for { run: fn, args }) or one options object; see the module doc. Throws on an object with no payload or more than one, and on an attribute name that is not one.

function script<A extends unknown[]>(run: (...args: A) => unknown, ...args: ScriptArgs<A>): string;
function script<A extends unknown[]>(options: ClassicScript<A> | ModuleScript<A>): string;
function script<I extends Record<string, string>, A extends unknown[] = []>(options: ModuleImportsScript<I, A>): string;
function script(options: JsonScript | LdScript | ImportMapScript | SpeculationScript): string;

setContext

function

Drop-in setContext — swap import { setContext } from 'svelte' for from 'ogygia' and an existing csr=false layout's context reaches child islands with NO other change.

It does exactly what Svelte's does (sets context for same-root descendants + the SSR-nested tree), AND on the server records the value so the handle can emit one page-level marker every island seeds from — closing the gap where a plain setContext shows up on the server but is gone on the client (child islands are separate hydration roots). A string key and a shared Symbol.for(name) key bridge; a one-off Symbol() key has no twin in the browser and stays same-root.

GRANULARITY: the optional third argument is the explicit marker — { islands: false } keeps a key host-native (same-root context works, nothing serializes). The default bridges: a missing key in an island is a broken app, extra bytes are not. Ogygia never infers readership from getContext call sites (aliased imports and wrapper modules would make any scan miss reads — the fatal direction).

This is the FLAT page root: every island on the page inherits it. For scoped/shadowed context use <Provide>, which wraps its subtree and beats the root on the same key.

function setContext<T>(key: unknown, value: T, opts?: SetContextOptions): T;

OgygiaBoundary

component

Optional public annotation wrapper around a region usage.

Renders children only: no extra DOM, no nested-island context, and no hydrate / render attributes. Use it to mark an island in source for humans. It is not <svelte:boundary>.

import OgygiaBoundary from 'ogygia';

Provide

component

Cross-island context provider for plain getContext(key): no createContext handle needed, and no change to the consumer islands. It writes values into the page once, so islands below (which hydrate as separate roots on a csr=false page) get them in their own context at hydrate, and it sets Svelte's own context, so the server-rendered tree reads the same values. Values must be serializable: plain data, [ogygia.wire] live values, region snippets; never a function or a live store. A provider's values are read once, when it renders.

import Provide from 'ogygia';

Region

component

A region: one place on a page whose HTML the server renders and whose code, if any, wakes on its own schedule. <Region of={value} /> renders a held region value from region(), or a promise of one (a remote call); placeholder shows while it is not ready yet. Placed regions (wake, render, 'none' on an import) render through it too, through the props the compiler writes.

import Region from 'ogygia';

Preference

interface

A live preference handle — SSR head(), client get()/set(), and the attr CSS authors target.

Preference.name

Preference.values

Preference.default

Preference.attr

The attribute set on <html>: data-pref-<name>. CSS targets :root[<attr>="<value>"].

Preference.head

No-flash inline <script> string — reads localStorage and applies the attr before paint. {@html} it once in <svelte:head>. Idempotent, so emitting it more than once is harmless.

Preference.set

CLIENT: persist + apply a new value (wire a control's handler to this). No-op on the server.

Preference.get

CLIENT: the current value (from the applied attr), or the default. Returns the default on the server.

interface Preference {/*…*/}
readonly name: string;
readonly values: readonly string[];
readonly default: string;
readonly attr: string;
head(): string;
set(value: string): void;
get(): string;

TransportCodec

interface

The codec import.meta.og.wire({ … }) carries (or a static method returning it).

TransportCodec.encode

Sending side: turn the live instance into devalue-safe data.

TransportCodec.decode

Receiving side: rebuild a live instance from that data.

TransportCodec.id

CONTINUITY — a stable session name. Naming the codec promotes the instance from page lifetime to SESSION lifetime: it becomes a singleton in this browser tab, and a navigation reunites the next page's decode with the SAME live instance instead of rebuilding it. Tab- scoped only — the server stays per-request (never remembers), a reload starts fresh.

TransportCodec.merge

Reconcile a navigation: the tab already holds the live named instance and the new page's server snapshot just arrived (decoded as fresh). Apply whatever should carry over INTO live — its identity is preserved; fresh is discarded afterwards. Default: do nothing (live wins — continuity is the point; a cart mid-edit beats a server re-read). Override to pull server truth in (prices, stock, auth state).

interface TransportCodec<T = unknown, D = unknown> {/*…*/}
encode: (value: T) => D;
decode: (data: D) => T;
id?: string;
merge?: (live: T, fresh: T) => void;

AwaitableRegion

type

What region returns: a RegionValue you can render right now with <Region of={…} />, AND a PromiseLike<Region> you can await to bake its server-rendered HTML into the ticket. In an async generator (query.live) or async remote, yield / return awaits it for you.

type AwaitableRegion = RegionValue & PromiseLike<RegionValue>;

DeferredRegion

type

A held region that arrived over the wire: a signed capability, no component.

type DeferredRegion = {
  readonly [REGION_BRAND]: true;
  readonly kind: "deferred";
  readonly id: string;
  readonly props: Record<string, unknown>;
  readonly url: string;
  readonly module: string;
  readonly hydrate?: string;
  readonly hydrateMargin?: string;
  readonly html?: string;
  readonly version?: number;
};

DualRegion

type

A marked component: renders inline here, or becomes a signed ticket when it crosses the wire.

type DualRegion = {
  readonly [REGION_BRAND]: true;
  readonly kind: "dual";
  readonly component: AnyComponent;
  readonly props: Record<string, unknown>;
  readonly id: string;
  readonly module: string;
  readonly hydrate?: string;
  readonly hydrateMargin?: string;
  readonly sign: (id: string, props: Record<string, unknown>, wakes?: boolean) => string;
  readonly renderHtml?: (props: Record<string, unknown>, wakes?: boolean) => string | Promise<string>;
  readonly html?: string;
  readonly version?: number;
};

Fallback

type

type Fallback<P = unknown> = P & {
  ogygiaFallback?: import("svelte").Snippet;
};

InlineRegion

type

A plain component. Renders in the current server pass; awaiting it bakes its SSR HTML so it can cross the wire as an HTML-only ticket (no chunk, no signer — nothing to fetch).

type InlineRegion = {
  readonly [REGION_BRAND]: true;
  readonly kind: "inline";
  readonly component: AnyComponent;
  readonly props: Record<string, unknown>;
  readonly html?: string;
  readonly content_id?: string;
};

PreferenceSpec

type

A preference declaration: its name, allowed values, and default (which must be one of the values).

type PreferenceSpec = {
  name: string;
  values: readonly string[];
  default: string;
};

RegionData

type

The region() data argument: region(Card, props, { version: row.updatedAt }). version is how new the data behind this answer is: a number, a Date, or a date string. When two answers for the same region reach the browser (two commands that both return it), the one with the newer version stays on screen, whichever arrived last. Equal versions, or an answer without one, fall back to arrival order. Only a held region has an address in the browser to order, so an inline region's version has nothing to compare against.

type RegionData = {
  version?: number | Date | string;
};

RegionOptions

type

Schedule options for a held region. wake = when its JS runs; margin = IntersectionObserver rootMargin for wake: 'visible'. Merged OVER the binding's baked schedule (from a wake: mark) — anything set here WINS; a region: 'raw' binding bakes nothing, so this is the whole schedule.

type RegionOptions = {
  wake?: string;
  margin?: string;
};

RegionSchedule

type

The region() schedule argument: a function handed the component's own props (its "generated data") that returns the schedule. Always a function, never a bare object — the object case is the baked wake: mark on the import. The function is for a registry of region: 'raw' components that decides each one's timing from its data — e.g. region(block, data, (d) => ({ wake: d.interactive ? 'load' : undefined })).

type RegionSchedule<P = Record<string, unknown>> = (data: P) => RegionOptions;

RegionValue

type

type RegionValue = InlineRegion | DualRegion | DeferredRegion;