@ogygia/flags
Feature flags
@ogygia/flags gives kill switches, rollouts, targeting rules and A/B tests one primitive, flag(), decided per visitor on the server during SSR. The chosen variant is what renders, so there is no flicker, no client SDK and no JavaScript for the decision.
A kill switch, a staged rollout, a targeting rule and an A/B/n test all come down to the same thing: a name with variants, decided per visitor. @ogygia/flags gives that decision one primitive, flag(). The decision happens on the server while the page renders, so the chosen variant is simply what the browser gets. There’s no flicker while a client SDK loads, nothing to hydrate for the decision, and a csr = false page can run an A/B test without shipping any JavaScript for it.
import { flag } from '@ogygia/flags';
export const checkout = flag('new-checkout'); // kill switch, off until decided on
export const promo = flag('summer-promo', 10); // sticky rollout to 10%
export const hero = flag('hero', { control: 80, bold: 20 }); // weighted variantsInstallation
Install the package and list its flags() plugin beside ogygia(), before sveltekit().
A flag decides from its own rule even without the plugin, but keep it listed in every app that reads flags. It does four things:
- Marks each read. Calling a flag (or its
pickorstamp) tells the request that its render belongs to one visitor, so postrender never stores one variant of an A/B test for everyone, and a cached server island is never shared across variants..value(c)doesn’t mark the render yet (Vendor values). - Records each decision. The request’s decisions are what a federation mount carries to other teams.
- Runs the source and the sink. It asks a decision source once at the start of each request and sends queued exposures at its end.
- Writes the manifest. A build writes every flag in the app to the flag manifest.
Without the plugin, the server says flags_plugin_not_running the first time it sees a flag read, flags_source_unprimed when a source is set but never runs, and the build says plugin_not_listed once for a module that imports @ogygia/flags.
pnpm add @ogygia/flagsimport { sveltekit } from '@sveltejs/kit/vite';
import { ogygia } from 'ogygia/vite';
import { flags } from '@ogygia/flags/vite';
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [ogygia(), flags(), sveltekit()]
});First flag
You read a flag by calling it with the request. A SvelteKit RequestEvent is such a request context, so you pass the event straight to the flag in a server load, then render what the page data says:
Stickiness needs to know who the visitor is. Until you give flags an identity, every rollout and every split answers its control for everyone. Visitor identity shows how, on the server router or on plain SvelteKit.
import { hero } from '$lib/flags';
import type { PageServerLoad } from './$types';
export const load: PageServerLoad = (event) => ({
variant: hero(event) // 'control' | 'bold', sticky per visitor
});<script lang="ts">
import type { PageProps } from './$types';
let { data }: PageProps = $props();
</script>
{#if data.variant === 'bold'}
<h1 class="bold">Everything you need, today</h1>
{:else}
<h1>Everything you need</h1>
{/if}Decision order
Each read resolves in this order, and the first answer wins:
- Override:
?og-exp=name:variantin the URL, on the dev server unless you set your own gate, or where that gate allows it (QA overrides). - Carried claims: a decision an upstream shell already made and signed into a federation hop.
- Source: a vendor or your own function, set with
decide({ source })(Decision sources). - Layer: for a flag in a layer, the control unless this flag owns the visitor’s lane.
- Native rule: the flag’s targeting function, its weights or its rollout (Declaring flags).
- Control: the first variant, or
off.
A flag decides once per request: the first read computes the answer, and every later read in the same request returns it.
Regions
A component’s schedule rides on its import attribute, so two imports of one file are two different things to test. This experiment asks whether an interactive version of a section is worth its JavaScript:
The flag, flag('pricing-live', { static: 50, live: 50 }), is read in the page’s server load as in First flag. Islands get the decision as a prop, never by reading the flag in the browser (Islands).
<script lang="ts">
import type { PageProps } from './$types';
import Pricing from '$lib/Pricing.svelte'; // server HTML, no JS
import PricingLive from '$lib/Pricing.svelte' with {
wake: 'visible'
}; // the same file, as an island
let { data }: PageProps = $props();
</script>
{#if data.variant === 'live'}<PricingLive />{:else}<Pricing
/>{/if}Package contents
| Page | What it covers |
|---|---|
| Declaring flags | flag(), the four shapes, names and variants, the options, and registration. |
| Reading flags | Calling a flag, pick, value(), stamp(), where flags can be read, islands, and unit tests. |
| Visitor identity | Sticky bucketing, c.visitor, the og-vid cookie, anonymous visitors, and identity on plain SvelteKit. |
| Rollouts | Kill switches, percentage rollouts, targeting, raising a rollout safely, and gating whole routes. |
| Experiments | Weighted variants, layers for mutual exclusion, exposures, batching and data-og-exp stamps. |
| Decision sources | decide(), the source contract, priming, the deadline, failures, and a custom source. |
| OpenFeature | openfeature() and ofrep(), their options, and how each treats a flag the vendor doesn’t know. |
| QA overrides | ?og-exp, its syntax, and allowing it outside dev. |
| Routing and federation | when() gates, pick as a page, the router’s flags option, and decisions carried across teams. |
| Caching | Postrender, server island caches, prerendered pages and CDNs. |
| Flag manifest | The build’s inventory of every flag() call, and catching flags that outlive their purpose. |
| API | The generated reference for @ogygia/flags, /openfeature and /vite. |
Integrations
- @ogygia/router gives every flag the visitor declared once on the table, picks a page per variant with
hero.pick({ control: Hero, bold: HeroBold }), and gates a whole route withwhen(checkout, page(NewCheckout)). - @ogygia/postrender never stores a page whose render read a flag, so one visitor’s variant is never served to everyone.
- @ogygia/federation carries every decision across a mount, so a visitor sees the same variant in each team’s part of the page.
Moved sections
These sections used to be on the single Flags & experiments page. Each now lives on its own page:
- Installation, in Feature flags
- Declaring flags, in Declaring flags
- Reading flags, in Reading flags
- Branching with pick, in Reading flags
- Visitor identity, in Visitor identity
- Plain SvelteKit, in Visitor identity
- Vendor values, in Reading flags
- OpenFeature, in OpenFeature
- Sources reference, in Decision sources
- openfeature(client, options?), in OpenFeature
- ofrep(options), in OpenFeature
- A custom source, in Decision sources
- prime_flags() and flush_exposures(), in Decision sources
- QA override, in QA overrides
- Mutual exclusion, in Experiments
- Measurement, in Experiments
- Cross-team flags, in Routing and federation
- Flag manifest, in Flag manifest