Skip to content

@ogygia/federation

Federation

@ogygia/federation renders another team's SvelteKit app into your pages over HTTP, at render time. A peer's routes and widgets are regions you place with <Region>, a whole app mounts under your layout, every hop is signed, and a publish on one team clears the stored pages of every team that used its fragments.

On this page

Experimental

The API can still change. The mechanics are covered by end-to-end tests, and examples/mfe in the repository is a working three-team demo.

When three teams with three repositories and three deploy pipelines all build parts of one page, something has to put their work together. @ogygia/federation does it over HTTP, while the page renders. The page’s app asks another team’s server for a fragment, gets back HTML, and puts it in its own server render. The islands inside wake in the browser with their code loaded from the team that built them.

Each app calls federate() once to say who it is, which apps it talks to and what it serves. It gets back a handle for each of those apps, its peers. A peer’s page or widget is a held region, the same value region() returns, so you place it with <Region>. A whole peer app can also take over a route of your server router with mount().

The browser loads the cms team’s island code and CSS from that team’s own server, so call each peer at an address visitors can reach, or give the peer a public ORIGIN (Asset origin).

import { federate } from '@ogygia/federation';
import { env } from '$env/dynamic/private';

export const { cms } = federate({
  name: 'shell',
  key: env.SHELL_SIGNING_KEY,
  peers: {
    cms: {
      origin: 'https://cms.example.com',
      key: env.CMS_PUBLIC_KEY
    }
  }
});

Installation

Install the package in every app that federates: the app whose pages show the fragments, and every app that serves them. It’s built on the server router, a peer dependency you install beside it. List federation() from @ogygia/federation/vite beside ogygia(), before sveltekit(). List router() too when the app has route tables, so their pages link their CSS.

The plugin serves the federation endpoints from ogygia.handle(), so you write no route files. It also keeps the exports that let another app’s page wake this app’s islands. Each app then needs a key pair, made with npx ogygia keys, and one federate() call that hooks.server.ts imports (Setup).

pnpm add @ogygia/federation @ogygia/router
import { sveltekit } from '@sveltejs/kit/vite';
import { ogygia } from 'ogygia/vite';
import { federation } from '@ogygia/federation/vite';
import { router } from '@ogygia/router/vite';
import { defineConfig } from 'vite';

export default defineConfig({
  plugins: [ogygia(), federation(), router(), sveltekit()]
});

First shell

The app whose page shows the fragments is the shell. Its federate() lives in a server module, here src/lib/server/peers.ts from the top of this page. hooks.server.ts imports it, so it runs when the server starts:

A load then asks the peer for one of its pages and returns it as a region. The page renders it with <Region>. The cms team’s HTML is in the shell’s first paint, and its islands wake on the shell’s page:

The cms app needs its own federate() that exposes its route table and lists the shell’s public key (Exposing routes). A region crosses Kit’s load with the transport hook that ogygia init adds to src/hooks.ts.

import * as ogygia from 'ogygia/server';
import '$lib/server/peers';

export const handle = ogygia.handle();
import type { PageServerLoad } from './$types';
import { cms } from '$lib/server/peers';

export const load: PageServerLoad = async () => ({
  article: await cms.page('/solar')
});
<script lang="ts">
  import { Region } from 'ogygia';
  import type { PageProps } from './$types';

  let { data }: PageProps = $props();
</script>

<Region of={data.article} />

Module federation

Module federation shares JavaScript modules between builds at runtime, which ties the teams’ bundlers, versions and upgrade dates together. Fragment federation shares finished HTML and self-contained islands instead. Each team ships its own build with its own copy of Svelte, about 12 kB gzipped for each extra build on a page, and a foreign island hydrates through a function that the build that made it exports, closed over its own Svelte. So the teams can run different Svelte versions, and one team’s broken dependency can’t break another team’s code.

Package contents

PageWhat it covers
Setupfederate(), the peers map, shells and producers, the endpoints it serves, and every option with its default.
Keys and signingnpx ogygia keys, what a signature covers, audience, replay and clock checks, key rotation and open endpoints.
Remote regionspeer.page() and peer.widget(), static and deferred, stored pages, server islands inside a fragment, failures.
Mounting appsmount() in a route table, status, head, redirects and forms, streamed mounts, canaries, and mount.kit().
Exposing routesfederate({ expose, base }), what the exposed table gets, the fragment document and its limits.
Widgets and catalogfederate({ widgets }), props, the catalog, and typed stubs with npx ogygia fragments.
Visitor identityWhich claims each kind of hop carries, user(c), c.visitor on the far side, flag decisions and chains.
Cross-app thawHow a publish or a deploy on one team clears other teams’ stored pages: tags, notices, retries and chains.
Caching and failoverThe document cache, shared fetches, replicas, timeouts, what each failure shows, and tracing.
Foreign islandsHow a peer’s islands wake on your page, code origins, CORS, and what crosses between builds.
Shared stateSharedState, one value that islands from separate builds share, seeded by any server.
Non-ogygia hostsThe wire: every endpoint, header and answer, signing a call by hand, sign_headers() and verify_signed_request().
Threat modelWhat the signatures protect against, what they don’t, and what’s left to you.
@ogygia/federationThe generated API reference, with /shared and /vite.

Integrations

  • @ogygia/router is what mount(peer) plugs into: one table entry serves another team’s app under your layouts, and the table’s c.visitor is the identity it signs into each hop.
  • @ogygia/postrender stores pages that baked a static fragment like any other page. A publish or a deploy on the team that made the fragment clears them (Cross-app thaw); mounted pages aren’t cleared this way yet (Stored pages). A deferred remote region keeps a stored page personal (Stored pages).
  • @ogygia/flags decisions ride a mount’s signed claims, so the visitor gets the same variant in every team’s part of the page (Routing and federation).
  • @ogygia/cli makes key pairs with ogygia keys and typed widget stubs with ogygia fragments. Its MCP server’s ogygia_fragment tool checks a peer’s origin.

Moved sections

These sections used to be on the single Fragment federation page (/docs/federation/guide). Each now lives on its own page: