@ogygia/core
Browser hooks
A plugin's browser half is defineClient({ boot, hooks }). It boots once with the page's runtime and subscribes to the runtime's moments: a region woke, hydrated, failed or left, a server island answered, a navigation finished. A hook no plugin subscribes to is removed from the runtime.
On this page
A plugin’s browser half is the module its client location names. Its default export is defineClient({ … }) from @ogygia/core/plugin/client. The build imports it into the runtime’s boot, so it ships to every page the runtime boots on, and it subscribes to the moments of the runtime: a region woke, hydrated or failed, a server island answered, a navigation finished.
The browser half is in the boot’s static graph, so every byte of it ships to every page. Keep it small, and load anything heavy on demand with a dynamic import() of its own.
import { defineClient } from '@ogygia/core/plugin/client';
export default defineClient({
boot({ kitPage }) {
if (!kitPage) document.documentElement.dataset.stamped = '';
},
hooks: {
regionHydrated({ el, t0, tDone }) {
console.info(
el.tagName,
`hydrated in ${(tDone - t0).toFixed(1)} ms`
);
},
regionFailed({ el, message }) {
console.warn(el, message);
}
}
});Boot
boot({ kitPage }) runs once, as the runtime boots: after core’s own features, before the first region wakes. Plugins boot in plugin order.
kitPage is true on a page Kit hydrates (csr = true). The runtime doesn’t boot there and no runtime hook fires. A browser half boots on such a page only on the dev server.
Runtime hooks
hooks maps hook names to functions. Each gets one event object. Times are page times in milliseconds, as performance.now() gives them.
| Hook | When it runs | Event |
|---|---|---|
regionWoke | A region’s schedule fired (load, idle, visible, interaction, media): it starts to wake, or a server island starts to fetch. | el, when |
regionHydrate | Svelte is about to hydrate a component over a region’s server markup. | el. May return a function, which runs right after the hydrate. |
regionMount | A region’s component started in the browser, hydrated or mounted. | el, entry (its component’s stable module URL), urls (the files it runs from) |
regionHydrated | A region hydrated. | el, t0 (it woke), tLoaded (its modules arrived), tTurn (its turn began), tDone (data-hydrated set), ssrHtml (the server markup) |
regionFailed | A region failed to hydrate. | el, message, since, turn |
regionMismatched | A region’s markup changed between the server and its hydration. | el, kind (recovered: Svelte rendered again; healed: the runtime put the server markup back; conflict: a web component’s own children were removed), reason |
regionUnmount | A region’s component left the page. | el, entry |
answerParsed | A region answer (a server island, a lake remount, a live region’s update) was parsed, before anything reads it. | fragment, an inert DocumentFragment. May return a promise: nothing reads the fragment until it settles. |
answerPlaced | A parsed answer went into the page. | el, the region it went into |
holeAnswered | A server island’s first answer replaced its fallback. | el, start, and batch (left, at, size) when it came in a batch |
holeFailed | A server island’s answer never came, and no retry follows. | el, id, reason (redirected, document, forbidden or error), finalPath, message, attempts, t |
holeBatchMissed | A batch request carried fewer server islands than it was sent for. | sent, delivered, status, refused, finalUrl, endpoints |
entryFallback | A region’s file failed to load, and its stable module URL was loaded fresh instead. | entry, src (what failed), recovered |
reloadNeeded | A chunk can load only after a reload: a download it imports failed, and the browser keeps that failure. Told once per page. | url, error, regions |
navStarted | An in-app navigation started, before its page is fetched. | from, to, type |
navigated | An in-app navigation finished. | from, to, type, t, fetched, styled, swapped, href, reconciled (the regions were kept in place) |
criticalSettled | The wait for the page’s critical resources ended, so the region downloads it held back may start. | outcome (arrived, input, cap or none), resources, waitedMs |
loadStarted | A download ogygia started on its own left the load scheduler’s queue. | class (user, visible, ahead, speculative), kind (code or content), label, waitedMs |
warning | Dev only: Svelte warned about hydration. | code, message, file, fp, t |
@ogygia/core/plugin/client exports a type for each event (RegionHydratedEvent, HoleFailedEvent and so on) and RuntimeHooks, the whole map.
Hook rules
- Subscribers run in plugin order, the same order as the server hooks.
- A hook is told. What it returns is ignored, with two exceptions.
regionHydratemay return a function, and every subscriber’s runs right after the hydrate, in plugin order.answerParsedmay return a promise, and the runtime waits for every subscriber’s before it reads the fragment. - A hook that throws, or whose promise rejects, is reported once per plugin and hook with
client_hook_failed, and stops nothing else: the other subscribers run, and the page goes on. A rejectedanswerParsedpromise is waited past. Abootthat throws is reported the same way, and the other plugins still boot. - Every subscriber gets the same event object. On the dev server it’s frozen, so a hook that writes to it fails, and the next subscriber sees it untouched.
- Hooks are called as plain functions, without
this.
Folding
The build reads the keys of hooks from the file, without running it. A hook no plugin subscribes to is removed from the runtime, with every call to it, so an app pays only for the moments its plugins listen to.
So write hooks as an object literal of plain keys inside defineClient({ … }). The object, or the whole argument, may sit in a module-level const. A spread or a computed key fails the build with plugin_client_hooks_unread, and a key that isn’t a runtime hook with plugin_client_hook_unknown, since a misspelled hook would never be called.
A plugin that places regions the build never compiled, such as another app’s, also calls b.foreignHydrate(), so the runtime ships what those regions need. A browser half alone doesn’t.
import {
defineClient,
type RuntimeHooks
} from '@ogygia/core/plugin/client';
const hooks: Partial<RuntimeHooks> = {
navigated({ to, reconciled }) {
console.info(
`to ${to}`,
reconciled ? 'in place' : 'swapped'
);
}
};
export default defineClient({ hooks });