@ogygia/federation
Exposing routes
federate({ expose }) serves a server-router table to other apps as fragment documents. The same table keeps serving the app's own visitors, and each page reads the shell's visitor from the signed call.
An app that other teams mount or place as remote pages exposes a server router table. Pass it to federate({ expose }), with the base the table renders under:
posts stands for your own data module. user(c) is the visitor the shell signed into the call (Visitor identity).
import { routes, page } from '@ogygia/router';
import { user } from '@ogygia/federation';
import Home from '$lib/views/Home.svelte';
import Post from '$lib/views/Post.svelte';
import { posts } from '$lib/server/posts';
export const cmsRouter = routes(
{
'/': page(Home, {
load: async () => ({ posts: await posts.list() })
}),
'/posts/[id]': page(Post, {
load: async (c) => ({
post: await posts.get(c.params.id),
viewer: user(c) ?? null
})
})
},
{ base: '/cms' }
);import { federate } from '@ogygia/federation';
import { env } from '$env/dynamic/private';
import { cmsRouter } from '$lib/server/router';
export const { shell } = federate({
name: 'cms',
key: env.CMS_SIGNING_KEY,
audience: 'cms.internal',
peers: {
shell: {
origin: 'https://www.example.com',
key: env.SHELL_PUBLIC_KEY
}
},
expose: cmsRouter,
base: '/cms'
});Base
base in federate() must be the same as the table’s routes(table, { base }). A call for path=/posts/1 runs the table at /cms/posts/1, the address the page has on your own app. Your links, form actions and redirects are written in that address space, and a shell that mounts you under the same prefix keeps them working. Without base, the table is asked at the path as given.
Your own site
Exposing a table doesn’t take it off your own site. Serve it to your own visitors as usual, from the hook or a catch-all (Serving), and the same routes answer your visitors and your callers:
On your own door there’s no signed call, so user(c) is undefined and c.visitor is whatever your table’s visitor works out. Through a shell, both are the signed claims. A header can’t make one door look like the other (Threat model).
import { sequence } from '@sveltejs/kit/hooks';
import type { Handle } from '@sveltejs/kit';
import * as ogygia from 'ogygia/server';
import { cmsRouter } from '$lib/server/router';
import '$lib/server/peers';
const cms: Handle = ({ event, resolve }) =>
cmsRouter.handle({ event, resolve });
export const handle = sequence(ogygia.handle(), cms);Call handling
A shell’s call arrives at /og/fragment/page?path=<route>&search=<query>. Your app reads the body (up to maxBodyBytes), checks the signature, and then runs your table on a new request:
- at your own origin, with
baseand the path, so apaththat would leave the origin (//host,/\host) gets a 400; - with the caller’s method, headers and body, minus the raw
x-og-userheader; - with a
traceparentthat continues the shell’s trace; - with the verified claims attached in process, where
user(c)andc.visitorread them.
Your loads, actions, layouts and error boundaries run as for any request. Flags read through c stick to the shell’s visitor, and a decision the shell already made wins (Routing and federation).
Fragment document
Your app lifts the page’s HTML into a JSON document and answers it with a 200, whatever the page’s own status:
| Field | What it holds |
|---|---|
status | The page’s status: 200, the status of your own error page, or 404 when no route matched. |
location | For a redirect (301, 302, 303, 307, 308), its target, in your own address space. |
title | The page’s <title> as plain text. |
css | The <link rel="stylesheet"> and <style> tags of its head, asset URLs made absolute to your origin. |
head | Its SEO and social <meta> tags and canonical link. Not charset, viewport, http-equiv or ogygia’s own ogygia-* metas. |
body | The body’s HTML. Island code, chunks, images and region URLs point at your origin; links between your pages stay as you wrote them. |
runtime | The absolute URL of your ogygia runtime, which wakes the islands in the body on another app’s page. |
path | Your own path for the page, base plus the route, which shells file their stored copies under (Cross-app thaw). |
sources | The data-source receipts the render read, with any it took from its own peers. |
build | Your build id, Kit’s version.name. |
trace | The trace and span id of this render. server_ms is how long it took, also sent as Server-Timing: og-fragment;dur=. |
Limits
- Only the document crosses. Your response’s headers stay on your side,
set-cookieandcache-controlincluded. A cookie a load sets never reaches the visitor through a shell, so set cookies in the shell’s app. - Only the table is exposed. Kit’s file routes,
+server.tsendpoints and other handles of your app aren’t reachable through/og/fragment/page. - The page context stays yours. A page’s context reaches its own body only, never the shell’s chrome.
pageis the shell’s in the browser. Your islands render on the server with your page store, and read the shell’s once they hydrate there (Foreign islands).
Non-ogygia hosts lists every status /og/fragment/page answers with.