@ogygia/federation
Cross-app thaw
A page that baked another team's fragment is stored like any other page. When that team publishes or deploys, its app sends a signed notice to every peer, and each one clears the stored pages and cached documents that used the fragment.
A static fragment baked into a shell’s page is stored with that page by postrender, like any other page. The question a store always asks is when the stored page stops being true. For a fragment, the answer lives on another team’s server: the page is stale once that team publishes the content or deploys a new build. Federation answers it without wiring on either side. The team that made the fragment, the producer, sends each peer a signed thaw notice, and each peer clears what used it.
That runs on the cms app, which exposes its table under /cms. posts stands for your own data module. Every shell that baked /cms/posts/<id> clears its stored pages that hold it.
import type { Actions } from './$types';
import { postrender } from '@ogygia/postrender';
import { posts } from '$lib/server/posts';
export const actions: Actions = {
default: async ({ params, request }) => {
await posts.save(params.id, await request.formData());
// clears this app's stored page, then tells every peer
await postrender.invalidate(
`/cms/posts/${params.id}`
);
}
};Tags
When a shell uses a fragment, it files the render, and so the page postrender stores, under tags named after the peer. The name is the one the shell gave the peer in its own peers:
| Tag | Added for | Cleared by, on the producer |
|---|---|---|
a:<peer> | every page and widget from the peer | a deploy, or postrender.invalidateWhere({ prefix }) |
p:<peer>:<path> | a page of the exposed table, at its own path (base included) | postrender.invalidate('<path>') with that exact path |
r:<peer>:<receipt> | each data source the fragment’s render read | postrender.invalidate(source, args) |
Only static regions, peer.page() and peer.widget(), file the render under these tags. A page served by mount() or mount.kit(), or built from peer.doc(), isn’t filed under any of them today, so the peer’s notices never clear a stored copy of it (#2311, item 11). Keep such pages out of the store, or clear them yourself.
A fragment carries its receipts in the answer’s sources, and the shell prefixes each one. A producer that never declared a source still gets a:<peer>, so deploys reach every page that used it. The peer’s cached documents carry the same tags, so a notice drops them too.
Publishing
Every clear on the producer is also sent to its peers, after its own store and edges are cleared:
- By path,
postrender.invalidate('/cms/posts/1')sendsp:/cms/posts/1. Write the path the way the fragment’spathfield has it:baseplus the route, as the page is addressed on the producer, so the cms home is/cms/. - By source,
postrender.invalidate(loadPost, ['1'])sends that source’s receipt, which clears every page that baked a fragment whose render read it. - By prefix,
postrender.invalidateWhere({ prefix })has no receipts to name, so it tells each peer to clear everything it baked from this app, whatever the prefix.
The producer needs postrender for this: its clears are what send notices. A notice carries only tags, never data, so the worst a notice can cause is that some pages render again.
Receiving
A notice is a signed POST /og/thaw with { id, hop, tags }, where tags is a list or 'all' (the wire). The receiving app:
- checks the signature against each peer’s keys, and takes the sender to be the peer whose key passed. It never trusts a name in the body, so one peer can’t clear another peer’s pages;
- puts each tag under that peer’s name (
p:/xbecomesp:cms:/x, a receipt becomesr:cms:<receipt>,'all'becomesa:cms); - drops its cached documents from that peer, clears its stored pages with those tags at its store and every edge, and answers 200;
- sends the notice on to every one of its own peers, as the next hop, under the same
id. That includes the peer it came from, and peers the producer already reached directly.
A peer accepts notices only from peers whose public key it lists, so a shell lists the key of every producer it wants to hear from. An app without postrender still accepts a notice and drops its cached documents; it has no stored pages to clear, and turning postrender on later needs no change. A notice is applied once: a copy with the same id within 10 minutes is answered { ok: true, deduped: true }, whichever peer sent it. Since the tags a notice clears are filed under its sender, that can skip a clear when apps list each other in a triangle: if the dash gets the shell’s forwarded copy of a cms notice before the cms’s own copy, it clears the pages filed under shell, then skips the cms’s copy, so its pages filed under cms stay (#2312). Postrender’s TTL is the backstop. Any clear on the cms sent again later goes out under a new id; a deploy’s thaw-all that was skipped this way isn’t sent again, since its retries reuse the id and the skipped copy counted as taken. When the store fails to clear, the app answers 503 and keeps nothing, so the sender’s retry is applied.
Delivery
A notice goes to every peer in the producer’s peers, each through its own queue, and never holds up or fails the publish. A peer that doesn’t take it is tried up to 4 times, with 200 ms, 1 s and 5 s between tries (20% jitter), each try waiting up to 5 seconds for an answer, all within 30 seconds. After that the producer logs federation_thaw_notice_failed, and the peer serves its stored copy until postrender’s TTL clears it. On platforms with waitUntil, the sends are kept alive past the response.
Notices are signed for the host of the peer’s first origin, even when the peer has an audience option, so the receiving app’s own audience must include that host (#2311, item 3).
Deploys
A new producer build should clear every shell page that baked its fragments, or those pages keep pointing at asset files of the old build. With deploy: 'auto', the default, the producer checks on the first fragment or widget request it answers after it starts: it asks postrender which build it saw last, Kit’s version.name, and if that’s another build, it sends every peer 'all'. Each peer clears every page tagged a:<producer>.
- Only producers send it. An app with no
exposeand nowidgetsnever does. - The producer needs postrender with a shared store. Postrender keeps the last build in its store. Without postrender nothing remembers a build, and the memory store starts empty on every start, so it never sees an earlier one. Use
ValkeyStore,UpstashStoreorCloudflareKvStore(Shared stores). - One instance sends it. The first instance to see the new build moves the record, and only it owes the notice, so ten instances don’t send it ten times.
A failed store check, or peers that didn’t take the notice, are tried again by the next fragment or widget request the producer answers (a catalog fetch counts, and so does a call that fails its signature check) at least 30 seconds after the last try, with no timer of its own. Requests from the producer’s own visitors don’t start it, so a producer nobody calls doesn’t retry. The notice goes again, under the same id, only to the peers that missed it, for 10 rounds. Then the producer logs federation_deploy_thaw_undelivered, naming them.
deploy: 'manual' turns the check off. To clear by hand after a deploy, call postrender.invalidateWhere({ prefix: '/' }) on the producer, which also sends every peer 'all'.
Chains
A notice travels on: a shell that receives one sends it to its own peers with the tags it just applied, one hop further, up to 4 hops. So a deploy or a prefix clear on a team at the end of a chain clears every page that used its work, however it got there. A source clear does too, since each app in the chain passes the receipts up as r: tags.
A path clear reaches only the apps that baked the page directly from the producer. Two hops away, the page is filed as r:<middle>:p:<producer>:<path>, and the forwarded tag is p:<middle>:<producer>:<path>, which doesn’t match (#2311, item 4). Clear by source or by prefix when a chain bakes your pages.