@ogygia/flags
Routing and federation
Flags on the server router pick a page component per variant, gate whole routes and endpoints with when(), and travel with the visitor across federation mounts, so one visitor sees one variant in every team's part of the page.
On this page
On the server router, a flag can do more than change a value in a load. It can choose the page component itself, decide whether a route exists for this visitor, and, through federation, carry its decision into another team’s app. Each of these reads the flag through the router’s c, so it sticks to c.visitor.
import { routes, page, when } from '@ogygia/router';
import Hero from '$lib/views/Hero.svelte';
import HeroBold from '$lib/views/HeroBold.svelte';
import NewCheckout from '$lib/views/NewCheckout.svelte';
import { hero, newCheckout } from '$lib/flags';
export const router = routes({
'/': page(hero.pick({ control: Hero, bold: HeroBold })),
'/checkout': when(newCheckout, page(NewCheckout))
});Page variants
flag.pick(map) without a context returns a page slot. Put it where page() takes a component, and the router renders this visitor’s variant on each request. The map must cover every variant.
The page’s types come from its load, not from the component, so App sees no difference and the arms can be anything that takes the same props. They don’t even have to be different files: a component’s schedule rides on its import attribute, so two imports of one file are two different pages. That lets you test whether an interactive version is worth its JavaScript:
import { routes, page } from '@ogygia/router';
import { flag } from '@ogygia/flags';
import Lab from '$lib/views/Lab.svelte'; // server HTML, no JS
import LabLive from '$lib/views/Lab.svelte' with {
wake: 'load'
}; // the same file as one live island
import { stats } from '$lib/server/stats';
export const csrMode = flag('csr-mode', {
static: 50,
hydrated: 50
});
export const router = routes({
'/lab': page(
csrMode.pick({ static: Lab, hydrated: LabLive }),
{
load: async () => ({ stats: await stats.latest() })
}
)
});Route gates
when(gate, entry) makes an entry exist only for requests the gate allows. A boolean flag is a gate as it is, so a staged rollout of a page, a beta route, an endpoint’s kill switch or a mounted app’s rollout is one wrapper. It works on pages, endpoints and mount().
When the flag answers off, the route doesn’t exist for that request. With no base, the request goes on to the rest of your app, so a Kit route at the same path can keep serving the old version. Under a base, the table answers as for any unmatched path.
The gate runs after the route matches and before any load. The flag is decided like any read: after the source primed, so a vendor’s kill switch removes the route, sticky per visitor, and carried across federation. App sees the entry as if it weren’t wrapped. router.entries() still lists a gated page for prerendering, whatever the gate would say (#2293).
import { routes, when } from '@ogygia/router';
import { exportsOn } from '$lib/flags';
import { orders } from '$lib/server/orders';
export const router = routes({
'/api/export': when(exportsOn, {
GET: async () =>
new Response(await orders.csv(), {
headers: { 'content-type': 'text/csv' }
})
})
});Shell flags
A flag a page reads is decided on that request and carried into every federation mount the page makes. Static and deferred remote regions don’t carry it (Flag decisions). A shell that only routes to other teams’ apps may read no flag itself, yet still want the visitor’s buckets to travel. List those flags in routes(table, { flags }): each is decided on every request the table matches, before any when() gate or load, so it’s carried even though nothing renders it.
import { routes, anonymousVisitor } from '@ogygia/router';
import { mount } from '@ogygia/federation';
import { cms } from '$lib/server/peers';
import { newNav, csrMode } from '$lib/flags';
export const router = routes(
{ '/cms/[...rest]': mount(cms) },
{
visitor: anonymousVisitor(),
flags: [newNav, csrMode]
}
);Federation
When a shell mounts another team’s app, the decisions the shell’s request has made so far travel in the mount’s signed claims, under experiments. Nobody lists anything by hand, so a forgotten entry can’t split a visitor between teams. On the other side, a carried decision comes second in the decision order, after an override and before the team’s own source and native rule, so one visitor never ends up in two variants of one flag. A decision already carried from further upstream wins over the shell’s own.
The claims are signed with the hop and set on the request in-process, so a visitor can’t forge them with a header (Visitor identity). The receiving app reads the claims’ sub or vid as its visitor, so its own flags stick to the same person.
A flag decided only after the mount rendered isn’t in its claims, so read the flags a mounted app depends on in the shell’s table flags, or before the mount in the page. A mount’s user option replaces the visitor claims, and the request’s decisions are still added. To canary a mounted app itself, pick the peer with a flag:
import { routes, when } from '@ogygia/router';
import { mount } from '@ogygia/federation';
import { cmsV1, cmsV2 } from '$lib/server/peers';
import { cmsV2On, cmsRollout } from '$lib/flags';
export const router = routes({
'/cms/[...rest]': mount(
cmsV2On.pick({ off: cmsV1, on: cmsV2 })
),
'/beta/[...rest]': when(cmsRollout, mount(cmsV2))
});