@ogygia/federation
Shared state
SharedState from @ogygia/federation/shared is one page-wide value that islands from separate builds read and write, such as a cart that one team's button fills and another team's header counts. Values cross as plain snapshots, and any server can seed the first value.
Islands from two teams’ builds on one page can’t share a Svelte store or a wired object: each build has its own Svelte runtime. SharedState gives them one value by name instead. Each build compiles its own copy of the class, and every copy meets at one store on the page, under the same name. Reads are reactive wherever Svelte is watching, and a write re-renders every island that reads it, whichever team built it.
Put the shared values in a small contracts package that every team depends on:
AddToCart can be a dash island and CartCount a shell island: both import the contract, and the count follows the button.
import { SharedState } from '@ogygia/federation/shared';
export const cart = new SharedState('corp.cart', {
items: [] as string[]
});<script lang="ts">
import { cart } from '@corp/contracts';
let { sku }: { sku: string } = $props();
</script>
<button onclick={() => /*MK<*/ cart.current.items.push(sku)}
>Add to cart</button
><script lang="ts">
import { cart } from '@corp/contracts';
</script>
<span>{cart.current.items.length} in cart</span>Reading and writing
.current reads the value. Inside a component, an effect or a $derived, the read subscribes, so the island updates on every write. Outside Svelte it’s a plain read.
You write either way:
- Replace it:
cart.current = { items: [] }. - Change it in place:
cart.current.items.push('sku-1'). Any nested set or delete is noticed, and a burst of changes, such as apush, sends one update per microtask. AMap,Set,DateorRegExpinside the value sends an update on every method call, reads included. See Plain data for what you can put in.
The name is the whole identity: two builds that use the same name share the value, whatever their initial values say. Prefix names with your organization or domain, as corp.cart does, and treat a change of shape as a versioned change of the contracts package. The first build on the page to create a name sets its first value.
Plain data
Only plain data enters the store: what devalue can write, which is JSON plus Date, Map, Set, BigInt, RegExp and the like. A write that replaces the value, cart.current = … or the plain-script set(), goes through a copy, so the store keeps a snapshot, not a live object, and a reactive proxy becomes plain data. A function, a class instance or a DOM node in it throws shared_state_not_plain at the write, with the path devalue reports.
An in-place write, such as cart.current.items.push(x) or cart.current.total = x, isn’t copied or checked today: the store keeps x as given, a class instance or a function included, and another build’s island then reads a live object from a Svelte runtime that isn’t its own (#2311, item 10). Push and set only plain data, or replace the whole value when you’re not sure. Share facts, and keep live objects on one side.
Seeding
Any server can set the opening value by printing a JSON script tag with the name. When the first SharedState of that name is created in the browser, the tag’s value wins over the contract’s initial value:
The tag holds JSON, not devalue, and isn’t checked by the copy rule, so write plain JSON. Escape < as above, so the text can’t close its script. Any server that can print HTML can seed it, an ogygia app or not. A tag that doesn’t parse reads as no seed.
<script lang="ts">
import type { PageProps } from './$types';
let { data }: PageProps = $props();
const seed = $derived(
JSON.stringify({ items: data.cartItems }).replaceAll(
'<',
'\\u003c'
)
);
</script>
<svelte:head>
{@html `<script type="application/json" data-og-shared="corp.cart">${seed}</` +
`script>`}
</svelte:head>Plain scripts
Code that isn’t Svelte joins through globalThis.ogygia.shared(name), which returns { get, set, subscribe }. set goes through the same copy rule, and subscribe(fn) calls fn with the new value after every write and returns a function that stops it:
The function exists once a module that imports @ogygia/federation/shared has loaded on the page, so call it after an island that uses the contract has loaded.
const cart = globalThis.ogygia.shared('corp.cart');
const stop = cart.subscribe((value) => {
document.querySelector('#legacy-count').textContent = String(
value.items.length
);
});
cart.set({ items: [...cart.get().items, 'sku-9'] });Server rendering
The store is page-wide in the browser. On the server there’s no page to be wide for: the same store is shared by the whole server process, across every request. During a server render, a SharedState reads its initial value, and a write there would reach every visitor’s render that follows. Nothing refuses such a write today (#2311, item 2). Write it only in the browser, from event handlers and effects, and seed per-visitor values with the script tag.