@ogygia/flags
Rollouts
Ship a feature dark behind a kill switch, release it to a sticky percentage of visitors, target it by role or request, and gate whole routes with when(), all with boolean flags decided on the server.
On this page
A rollout lets you merge a feature before everyone sees it. You ship it behind a boolean flag that starts off, turn it on for your team, then for a growing share of visitors, and delete the flag once it’s on for everyone. Each step is a change to the flag’s shape, a vendor’s dashboard or the URL, never to the code that reads it.
import { flag } from '@ogygia/flags';
export const newCheckout = flag('new-checkout', 5); // 5% of visitors, stickyimport { newCheckout } from '$lib/flags';
import type { PageServerLoad } from './$types';
export const load: PageServerLoad = (event) => ({
v2: newCheckout(event)
});Kill switches
flag(name) with no shape is off for every visitor. Something above the native rule has to turn it on:
- a decision source, such as a vendor’s dashboard, for some or all visitors;
- an override in the URL, for QA;
- the claims of an upstream shell, in federation.
The other way round, a source answering off beats any native rule, so a vendor can switch off a feature you rolled out in code without a deploy. A flag with a source behind it is a kill switch for the whole feature, including the routes it gates.
Without a vendor, a targeting function reads anything at request time, so an environment variable or a database row can be the switch. Return a boolean: undefined falls through to the next rule, and a targeting flag has none, so it means off.
import { env } from '$env/dynamic/private';
import { flag } from '@ogygia/flags';
// on unless EXPORTS_OFF=1 is set: turn the feature off without a deploy
export const exportsOn = flag(
'exports',
() => env.EXPORTS_OFF !== '1'
);Percentages
A number shape is the percentage of identified visitors who get the feature. It’s sticky: each visitor’s bucket is fixed, and the flag is on when the bucket falls under the percentage. Fractions work down to 0.01%.
| Shape | Who gets it |
|---|---|
0 or less | nobody |
0.5 | one identified visitor in 200 |
10 | one identified visitor in 10 |
100 or more | every identified visitor |
| any | never a visitor with no id: they get off (Visitor identity) |
Raising the percentage keeps everyone who had the feature: going from 10 to 25 adds visitors and removes none, because a bucket under 10 is also under 25. Lowering it takes the feature away from the most recently added visitors first. Renaming the flag reshuffles everyone, so raise the number and keep the name.
import { flag } from '@ogygia/flags';
// week 1: 5, week 2: 25, week 3: 100, then delete the flag and keep the new code
export const newCheckout = flag('new-checkout', 25);Targeting
A function shape decides per request: return true for on, false for off, or undefined to fall through to off. It gets the same context the read does, so it can read c.visitor’s claims, a header, a cookie or c.url. Under strict TypeScript, annotate its parameter as CtxLike: the function isn’t typed from the overloads yet (#2310).
A flag has one shape, so to target some visitors and roll out to a share of the rest, read a rollout flag from the targeting function. The inner read is an ordinary read, with its own exposure.
import { flag, type CtxLike } from '@ogygia/flags';
const searchRollout = flag('search-v2-rollout', 20);
export const searchV2 = flag('search-v2', (c: CtxLike) => {
const roles = c.visitor?.roles;
if (Array.isArray(roles) && roles.includes('staff'))
return true;
if (c.request.headers.get('x-region') === 'eu') return false;
return searchRollout(c);
});Route gates
A rollout of a whole page or endpoint is one wrapper on the server router: when(flag, entry). When the flag says no, the route doesn’t exist for that request. Without a 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 during the migration.
Routing and federation covers when the gate runs and how it combines with sources and federation.
import { routes, page, when } from '@ogygia/router';
import NewCheckout from '$lib/views/NewCheckout.svelte';
import { newCheckout } from '$lib/flags';
export const router = routes({
'/checkout': when(newCheckout, page(NewCheckout))
});Cleanup
A rollout ends with a human edit: delete the flag, keep the branch that won, and delete the old code. Until then, every page that reads it stays uncached by postrender. The flag manifest lists every flag in the build, so a CI check can flag the ones that outlived their date.