Skip to content

@ogygia/core

Helpers

What a plugin's server code uses besides the hooks: region values it can hand to <Region>, late regions that arrive after the document, route config for a programmatic router, the CSS of components it renders, and the pure helpers in plugin/utils.

On this page

The hooks are when a plugin runs. This page is what it works with: values, wires and small helpers from @ogygia/core/plugin, /plugin/css and /plugin/utils. Each also comes from ogygia/plugin… under the same subpath.

import { defineServer, htmlRegion } from '@ogygia/core/plugin';
import { html } from '@ogygia/core/plugin/utils';

export const banner = (text: string) =>
  htmlRegion(String(html`<p class="banner">${text}</p>`));

export default defineServer({});

Region values

A plugin’s server code can make the same values an app places with <Region>, so a load can return them like any held region:

FunctionWhat it makes
region(Component, props)A region of a component, the same as region from ogygia, without loading the whole front door.
htmlRegion(html)A region holding finished HTML, placed as it is, with no island. Escape what goes in it: html from plugin/utils does.
deferredRegion(id, url)A region whose HTML the browser fetches from url, a capability your plugin minted ('' for one that never fetches). It’s HTML only: islands in the answer wake by themselves.

Federation makes another app’s page into a region this way.

Late regions

A late region is one whose HTML arrives after the document has flushed, down the same response, the way a streamed page fills its slots. A plugin that renders a page itself, outside Kit’s routes, drives it in three steps:

  1. openLateScope(), before the render, arms the request’s capture: a <Region of={promise}> in that render then registers its promise, and its placeholder is wrapped in a slot.
  2. takeLateRegions(), after the render, returns { id, promise } for each slot, or null when none registered, and clears the list.
  3. For each one, as it settles, write lateChunk(id, html) down the response. lateSlot(id, html) makes a slot by hand, and LATE_BOOT_SCRIPT is the inline script for the <head> that moves each chunk into its slot as the document parses.

The server router is built on these.

Route config for routers

ctx.routeConfig(key) reads a key from the page’s config. For Kit routes the build reads it (Route config). A programmatic router, whose pages are values rather than files, answers for its own paths with registerRouteConfig(key, matcher):

The matcher returns undefined for a path that isn’t its own, so the next matcher, then the Kit page, answers. For a path it claims, it returns the route’s merged config, and a key that config lacks answers undefined without asking Kit. Registering the same key again replaces the old matcher, which matters in dev, where hooks.server.ts runs again on an edit.

import {
  registerRouteConfig,
  type RouteConfig
} from '@ogygia/core/plugin';

const tables = new Map<string, RouteConfig>([
  ['/pricing', { stamp: false }]
]);

registerRouteConfig('my-router', (pathname) =>
  tables.get(pathname)
);

Component CSS

A page rendered outside Kit’s routes has no file-derived link to its components’ styles. componentCssHead(components) from @ogygia/core/plugin/css is async. It resolves to the <link> and <style> tags for the components one render placed (a layout chain and its page), each component’s whole tree, without the ones this request already linked. Put the result in the document’s head.

It needs the components’ modules named as page modules. On the dev server, a render with none named warns once with router_css_inactive. Outside the app’s Vite pipeline, such as in a unit test, it resolves to an empty string.

import { componentCssHead } from '@ogygia/core/plugin/css';
import { escapeText } from '@ogygia/core/plugin/utils';

export async function documentHead(
  components: readonly unknown[],
  title: string
): Promise<string> {
  const css =
    await componentCssHead(components);
  return `<title>${escapeText(title)}</title>${css}`;
}

Stored documents

STORED_DOCUMENT_META is the <meta name="ogygia-stored" content="hit"> tag a plugin puts in a document it serves from a store rather than from the render that just happened. With it, the runtime treats render: 'live' lakes as stale on their first mount and fetches them fresh. Postrender stamps it on every hit.

Utils

@ogygia/core/plugin/utils holds pure helpers with no context and no state, safe in any bundle, a browser’s included:

HelperWhat it does
html, attr, trustedHtml, HtmlBuild HTML with each value escaped for where it sits: text, a quoted attribute, or a URL attribute, where a javascript: URL becomes harmless. attr(name, value) is an optional attribute. html.svelte also escapes { and }, for markup written into a Svelte template.
escapeText, escapeAttrEscape &, < and > for text, or & and " for an attribute value.
decodeTextTurn HTML text back into plain text, decoding its character references.
closeSafeMake code safe to write inline in a <script>: no </script and no <!-- inside.
isScriptUrlWhether a browser would run a URL as script (javascript:, vbscript:, data:), its scheme read as a URL parser reads it.
srcsetCandidatesSplit a srcset value as the HTML spec does.
isSpaceWhether a character code is whitespace, as \s matches it.
fnv1a32A fast 32-bit hash, stable across runs. Not for anything secret.
SizedLruA cache map bounded by bytes and entries: new SizedLru(maxBytes, maxEntries), then set(key, value, bytes, expires) and get(key, now).
sourceTagThe receipt tag one call to an import.meta.og.source function files, or null for any other function.
dedentStrip the common indent of a block of text, as a function or a template tag.
contentCssKeyThe stable key of a markup module’s own scoped CSS.
StandardSchemaV1, StandardResult, StandardIssueThe Standard Schema types, to validate a value with any schema library.