@ogygia/federation
Remote regions
peer.page() and peer.widget() turn another app's page or widget into a region you place with <Region>. A static one is baked into your server render; a deferred one is a hole the browser fetches through your own server, with the visitor's claims added there.
On this page
A peer handle from federate() turns any page of the peer’s exposed table or any widget of its catalog into a region. peer.page(path) is a remote route, peer.widget(name, props) a remote component. Both return a Promise<RegionValue>, the same held region a local region() gives you, so <Region of>, its placeholder snippet, travelling CSS and islands that wake inside all work as they do for a local one.
The handles hold your private key, so they’re server values. Build the region in a server load and render it from data:
A region crosses a Kit load through the transport hook in src/hooks.ts (export const transport = { ...ogygia.transport }), which ogygia init adds. A server router load passes it in process, with nothing to add.
import type { PageServerLoad } from './$types';
import { cms, dash } from '$lib/server/peers';
export const load: PageServerLoad = async () => ({
article: await cms.page('/solar'),
kpis: await dash.widget(
'kpis',
{ org: 'acme' },
{ render: 'deferred' }
)
});<script lang="ts">
import { Region } from 'ogygia';
import type { PageProps } from './$types';
let { data }: PageProps = $props();
</script>
<Region of={data.article} />
<Region of={data.kpis}>
{#snippet placeholder()}<p>Loading figures…</p>{/snippet}
</Region>Arguments
peer.page(path, dials?) and peer.widget(name, props?, dials?) take these, with the dials as their last argument:
pathis the route in the peer’s own table, without itsbase:cms.page('/posts/1')for a page the cms serves at/cms/posts/1. Start it with one/: a path without it is still served, but filed under the wrong thaw tag (#2311, item 12).search(page()only) is the query string the page sees, such as'page=2'.propsbecome the widget call’s query string, so every value is a string. The widget receives them as they are (Props).renderis'static'(the default) or'deferred', below.claimsare the visitor claims signed into a static call. A deferred region ignores them.
Static regions
A static remote region is one signed call during this render. Its HTML and its CSS links are baked into your page, the CSS is hoisted to <head>, and the islands inside wake by themselves once the page loads. No extra request is made for the HTML; the island code, CSS and images load from the peer’s asset origin. Await it in the load, as above.
A page region also carries the peer page’s head tags, its description, og:* metas and canonical link, and they land in your body, in the region’s place, not in your <head> (#2311, item 17). Browsers ignore them there, but a canonical link to the peer inside your page can still confuse a crawler. Use a mount when you want the peer’s head.
A static call carries only the claims you pass. Without them it’s anonymous: it doesn’t run federate({ visitor }) and it doesn’t carry your flag decisions. That’s on purpose, since an anonymous region can be part of a page postrender stores once for everyone. To show the signed-in visitor something personal, pass claims, or use a deferred region:
sessions.claims() stands for your own code that turns a session cookie into { sub, … }, or undefined for an anonymous visitor. The fragment’s data-source receipts join this render’s, under tags named after the peer. A page postrender stores is filed under them, so a publish or a deploy on the peer clears it (Cross-app thaw).
import type { PageServerLoad } from './$types';
import { dash } from '$lib/server/peers';
import { sessions } from '$lib/server/sessions';
export const load: PageServerLoad = async ({ cookies }) => ({
usage: await dash.widget(
'usage',
{},
{
claims: sessions.claims(
cookies.get('session')
)
}
)
});Deferred regions
render: 'deferred' makes a hole, the same as a local server island. The page renders the placeholder, and the browser fetches the HTML when the page loads, from your app’s /og/frag endpoint, never from the peer. Your server then:
- checks the hole’s URL, which your app signed when it rendered the page;
- runs
federate({ visitor })with the hole’s request, its cookies andlocals; - calls the peer, signed, with those claims, and answers with the fragment’s HTML.
So the hole’s own fetch goes to your origin, with your cookies, and the claims always come from the visitor’s own session at the moment the hole loads. The claims dial is ignored here. The fragment’s island code, CSS and images still load straight from the peer’s asset origin, as for a static region (Asset origin).
The browser can’t choose anything about the call. The hole’s URL names the peer, the page or widget and its props, and it’s signed with your app’s OGYGIA_SECRET, so there’s no endpoint that turns browser input into a signed call, and nothing to allowlist. The props are readable in the URL, though, so pass ids, not secrets.
sessions.claims() stands for your own code: return a plain object, such as { sub, roles }, or undefined for an anonymous visitor. A hole’s answer is sent with cache-control: no-store.
import { federate } from '@ogygia/federation';
import { env } from '$env/dynamic/private';
import { sessions } from '$lib/server/sessions';
export const { dash } = federate({
name: 'shell',
key: env.SHELL_SIGNING_KEY,
visitor: ({ cookies }) =>
sessions.claims(cookies?.get('session')) ,
peers: {
dash: {
origin: 'https://dash.internal',
key: env.DASH_PUBLIC_KEY
}
}
});Stored pages
A deferred remote region’s URL is one of your app’s own signed hole URLs, so it lives exactly as long as a local hole’s on the same page: 10 years on a page postrender stores, the cache lifetime on a page a load marks shared-cacheable, and the app’s ttl anywhere else. A copy that outlived its URL renews it the way a local hole does (Server islands). Keep OGYGIA_SECRET stable across deploys, or stored copies carry URLs the new build refuses.
So a page with deferred remote regions can be stored like any other. It’s stored once, and each visitor’s holes still load fresh, with claims from that visitor’s own session. A static remote region is the other choice on a stored page: baked into the stored bytes, and cleared by the peer’s thaw notices.
Nested server islands
A fragment can hold the peer’s own server islands and lakes. Their URLs point at the peer, so before the HTML reaches the page, your app rewrites each one on the peer’s origins into a hole of its own. The browser asks your app, and your app asks the peer’s URL, as the browser would have, with no claims added: the peer signed what the hole renders into its own URL. An expired URL is renewed by the peer once. A URL on any other origin is left as it came, and the runtime refuses it. That includes the peer’s public address when you call it internally and it isn’t one of the peer’s origin entries (Asset origin).
Failures
A peer that’s down, slow past its timeout, or answers with something that isn’t its document fails the call. What the visitor sees depends on the kind of region:
| Region | What happens |
|---|---|
| static widget | The widget’s place shows a card, “This section is temporarily unavailable.” (data-og-fragment-failed), and the rest of the page renders. |
| static page | The load throws the router’s HTTP error: 504 for no answer, the peer’s own 503 or 504, else 502. A router page shows its error boundary with that status. Kit doesn’t know the router’s error, so a Kit load answers 500 (#2311, item 6). Catch it to render the page without the region. |
| deferred region | The hole answers with that error status, the placeholder stays, and the runtime asks again later. |
Each failure is logged once with its code (Failures). A static page bakes the body the peer sends whatever its status, so a page the peer doesn’t have shows the peer’s own not-found page, and a page that redirects bakes nothing. A deferred page whose own status is 400 or above, such as a page the peer doesn’t have or its 500 page, keeps its placeholder, and one that redirects shows a “This section moved.” link to the new address.
import type { PageServerLoad } from './$types';
import { isHttpError } from '@ogygia/router';
import { cms } from '$lib/server/peers';
export const load: PageServerLoad = async () => {
try {
return { article: await cms.page('/solar') };
} catch (e) {
if (isHttpError(e)) return { article: null }; // the page renders without it
throw e;
}
};Peer handles
federate() returns one handle per peer, typed by the names in peers. Besides page() and widget(), a handle has:
name,originandlabel: the peer’s name, its first origin, and itsServer-Timinglabel.doc(path, search, claims?, traceparent?): the peer’s page document itself, through the cache and shared fetches.mount()reads pages with it.postDoc(path, search, body, contentType, claims?, traceparent?): a form post to the peer’s page, which skips and then clears the cache.widgetDoc(name, props?, { claims?, traceparent? }): a widget’s raw document,{ html, origin, … }.drop(tags): drops cached documents that carry any oftags, or every one with'all'. A thaw notice calls it for you.
Use doc() when you need the fragment document’s fields, such as its title, rather than a region. Unlike a region, it doesn’t file the render under the peer’s thaw tags, so a page built from it and stored by postrender isn’t cleared by the peer’s publish or deploy (#2311, item 11).