@ogygia/federation
Foreign islands
A peer's islands wake on your page with their code loaded from the peer, closed over the peer's own Svelte. Your page loads code only from the origins you listed as peers, the peer serves its assets with CORS, and only plain data crosses between the two builds.
A fragment’s HTML holds the peer’s islands, written by the peer’s build. When the page loads, your runtime imports each one from the peer’s origin and hydrates it through a function the peer’s build exports, which runs with the peer’s own copy of Svelte. Your page can carry islands from three builds on three Svelte versions, and each wakes on its own team’s schedule. The federation() plugin keeps those exports in the peer’s build, so every app that serves fragments lists it.
Asset origin
The browser loads a fragment’s island code, CSS, images and the peer’s runtime straight from the peer, not through your app. Only the server-to-server call and the fetch of a deferred region go through your server. When the peer answers, it makes every asset URL in the fragment absolute against the origin it answers on:
- the peer’s
ORIGINenvironment variable, on adapter-node; - without one, the host your app called, from the request’s
Hostheader. Adapter-node then also assumeshttps.
So if your app calls the peer at an internal address, such as https://cms.internal, and the peer sets no ORIGIN, the fragment tells the browser to load https://cms.internal/_app/immutable/…, which it can’t reach, and every island and stylesheet from that peer fails. A run on examples/mfe showed it: with the shell calling the cms under a host the browser couldn’t reach, the cms island logged island_load_failed and stayed as server HTML.
Pick one of these:
- Call the peer at its public address.
peers.cms.originishttps://cms.example.com, the address the browser can load. Everything else stays at its defaults. - Call it internally, and give it a public
ORIGIN. The peer runs withORIGIN=https://cms.example.com, so its asset URLs name that host, whilepeers.cms.originstayshttps://cms.internalfor the server-to-server call. The peer’s default audience is now the public host, so list both hosts in itsaudience. Also list the public address as a secondoriginon your side,origin: ['https://cms.internal', 'https://cms.example.com']: the peer’s nested server islands then point at that address, and your app forwards only URLs on one of the peer’s origins. Mounted form posts then fail: a mount sends the peer’s firstoriginas the post’sOrigin, and the peer’s Kit refuses one that isn’t its ownORIGINwith a 403 (#2312). If you mount the peer, add the internal origin to its Kitcsrf.trustedOrigins, or list the public address first. - Serve the built files from a public CDN. With Kit’s
paths.assetsset to the CDN, island code, CSS and the runtime are already absolute and stay on the CDN, with CORS. URLs the peer’s server answers itself, such as image-endpoint files and server islands, still use the answering origin. Your page learns the CDN as a code origin only from static page regions and mounts, so for widgets and deferred regions also list the CDN, last, as one of the peer’soriginentries (Code origins).
import { federate } from '@ogygia/federation';
import { env } from '$env/dynamic/private';
export const { cms } = federate({
name: 'shell',
key: env.SHELL_SIGNING_KEY,
peers: {
// the cms runs with ORIGIN=https://cms.example.com and audience: ['cms.example.com', 'cms.internal']
cms: {
origin: [
'https://cms.internal',
'https://cms.example.com'
],
key: env.CMS_PUBLIC_KEY
}
}
});Code origins
Your runtime imports island code only from your page’s own origin, Kit’s paths.assets, and the origins your server writes on the runtime’s script. Federation adds two kinds: every origin of every peer in your federate({ peers }), replicas included, and the runtime origin a page fragment names, taken only from an answer that came from that peer’s own origins and only for a static peer.page() or a mount rendered with this page. Island code from anywhere else is refused with island_code_refused, and the island stays as its server HTML.
A peer whose built files live on its own origin needs nothing more. When they live on another host, such as a CDN set as its Kit paths.assets, only static page regions and mounts are covered, since their fragments name a runtime on that host. A widget’s answer names no runtime, and a deferred region’s HTML arrives after the page was written, so their islands on the CDN are refused with island_code_refused (#2312). List the CDN as the last of the peer’s origin entries to cover them. Every extra entry is also a replica, though: a read that fails on the peer, with no answer or a 5xx, is tried against the CDN too, signed for its host and with its own timeout, and fails there with a 502 and federation_peer_answer_invalid naming the CDN instead of the peer’s own status (#2312). A public address listed as a second origin, above, is the peer itself, so a read that fails over to it is answered properly. A region whose code points anywhere else came from markup that isn’t the peer’s render, and is refused on purpose.
CORS
A peer’s island code is loaded by a page on another origin, through a dynamic import(), which makes it a CORS request. The peer must answer its built files under /_app/immutable/ (with Kit’s default appDir) with Access-Control-Allow-Origin, usually *, since they’re public, content-hashed files. So must its /_app/ogygia.json, the map your runtime reads when one of the peer’s islands arrives without its file named. Without the header, each foreign island’s import fails (Chromium says “Failed to fetch dynamically imported module”; Firefox and WebKit word it differently), and the island stays as server HTML.
Put the header on the peer’s CDN or reverse proxy. A plain adapter-node server doesn’t set it, and Kit’s hooks don’t run for its static files. Wrapping the built handler works:
Checks that only read HTML can’t catch a missing header: keep a real-browser test that loads a page with a foreign island and waits for it to wake.
import { createServer } from 'node:http';
import { handler } from './build/handler.js';
createServer((req, res) => {
if (
req.url?.startsWith('/_app/immutable/') ||
req.url === '/_app/ogygia.json'
) {
res.setHeader('access-control-allow-origin', '*');
}
handler(req, res, () => {
res.statusCode = 404;
res.end('Not found');
});
}).listen(Number(process.env.PORT ?? 3000));What crosses
An island’s props cross from the peer’s server render to its hydration in your page as data, under the same rules as any island prop. Between two builds, a few things can’t cross:
- Snippets and children. A fragment is finished HTML, so you can’t pass your own markup into a peer’s island.
- Wired classes. A wired object from another build can’t revive in another Svelte runtime, so it throws
wired_class_foreign. Pass its data. - Kit’s public env. A peer island that imports
$app/env/publicreads an empty env on your page, never yours, and sayskit_env_missing(#2221). Pass the value as a prop.
State that two teams’ islands share on one page goes through SharedState, which holds plain data by name, page-wide.
Page store
On the peer’s server, page from $app/state is the peer’s page. In your browser, a peer island hydrates inside your document, and the same read returns your page: your route, params and page.data. So an island that reads page.data paints the peer’s value on the server and repaints with yours after hydration. The URL matches only for a mount under the peer’s base: a peer.page('/solar') placed on your home page rendered with page.url at /cms/solar on the peer, and reads / once it hydrates.
When the peer’s island comes from a dev build, the peer’s own vite dev, ogygia warns once per island with foreign_page_read, naming the island and its origin, when it reads page.params, page.route, page.data, page.form or page.error. It doesn’t warn for page.url, page.status and page.state, which match on a mount, but not on a page placed as a region. Pass the value the island needs as a prop instead.
Context
A context a peer’s page or layout sets reaches that page’s islands on your page too, as it does on the peer’s own door. It stops at the fragment’s edge: it never reaches your chrome, and your context never reaches the peer’s islands.