Skip to content

@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.

On this page

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 variants

Installation

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 pick or stamp) 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/flags
import { 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:

  1. Override: ?og-exp=name:variant in the URL, on the dev server unless you set your own gate, or where that gate allows it (QA overrides).
  2. Carried claims: a decision an upstream shell already made and signed into a federation hop.
  3. Source: a vendor or your own function, set with decide({ source }) (Decision sources).
  4. Layer: for a flag in a layer, the control unless this flag owns the visitor’s lane.
  5. Native rule: the flag’s targeting function, its weights or its rollout (Declaring flags).
  6. 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

PageWhat it covers
Declaring flagsflag(), the four shapes, names and variants, the options, and registration.
Reading flagsCalling a flag, pick, value(), stamp(), where flags can be read, islands, and unit tests.
Visitor identitySticky bucketing, c.visitor, the og-vid cookie, anonymous visitors, and identity on plain SvelteKit.
RolloutsKill switches, percentage rollouts, targeting, raising a rollout safely, and gating whole routes.
ExperimentsWeighted variants, layers for mutual exclusion, exposures, batching and data-og-exp stamps.
Decision sourcesdecide(), the source contract, priming, the deadline, failures, and a custom source.
OpenFeatureopenfeature() 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 federationwhen() gates, pick as a page, the router’s flags option, and decisions carried across teams.
CachingPostrender, server island caches, prerendered pages and CDNs.
Flag manifestThe build’s inventory of every flag() call, and catching flags that outlive their purpose.
APIThe 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 with when(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: