@ogygia/flags
Visitor identity
Flags stick to the visitor with a hash of the flag's name and the visitor's id, with nothing stored. Declare who the visitor is once, on the server router or in a small helper on plain SvelteKit, and give logged-out visitors a cookie.
A rollout or an A/B test has to give the same visitor the same answer on every request. ogygia does that without storing anything: a flag’s bucket is a hash of the flag’s name and the visitor’s id. The same visitor lands in the same bucket on every request and every server, with no cookie written per flag and nothing to sync. What you provide is the id.
import { routes, page } from '@ogygia/router';
import Home from '$lib/views/Home.svelte';
import { sessions } from '$lib/server/sessions';
export const router = routes(
{ '/': page(Home) },
{
visitor: (c) => {
const s = sessions.read(c.cookies?.get('session'));
return s ? { sub: s.userId, roles: s.roles } : undefined;
}
}
);Bucketing
The id is sub for a signed-in visitor, else vid for a logged-out one. A rollout or a weighted flag hashes name:id into one of 10,000 buckets and decides from the bucket. Because the flag’s name is in the hash, a visitor’s bucket in one flag says nothing about another flag, so two 10% rollouts pick their 10% independently, and about one visitor in a hundred lands in both.
A few things follow:
- No id, no split. A visitor with no id gets the control of every weighted flag and
offfrom every rollout, even a rollout at 100. Targeting functions, overrides, a source and carried claims still apply, since they don’t need the hash, except for a layer member: all visitors with no id share one lane, so only one member’s targeting runs for them and the others answer their control (#2310). - Signing in can move a visitor. Their id changes from
vidtosub, so their bucket is worked out afresh. If an experiment must hold across sign-in, key the visitor on one id throughout, such as avidyou keep after sign-in, or let a source that knows both decide. - The hash is for spreading, not secrecy. It’s uniform and fast, not cryptographic, so don’t use a bucket to protect anything.
Where the id comes from
A read looks for the visitor in this order, and uses the first that has a sub or a vid:
c.visitoron the context you passed: the server router’sc.visitor, or avisitoryou put on the context yourself;- the claims of a signed federation hop, when a shell called this app;
- the
og-vidcookie, read as{ vid, anonymous: true }; - nobody.
A small helper puts your own identity on a Kit event. It lives outside $lib/server, so a server island’s component can import it too (Where to read):
import type { RequestEvent } from '@sveltejs/kit';
import type { CtxLike } from '@ogygia/flags';
export function flagContext(event: RequestEvent): CtxLike {
const user = event.locals.user; // whatever your auth sets, typed in App.Locals
return {
request: event.request,
url: event.url,
cookies: event.cookies,
visitor: user ? { sub: user.id } : undefined
};
}Server router
On the server router, say once on the table how to find the visitor with routes(table, { visitor }), as in the example at the top. That becomes c.visitor in every load, handler and when() gate, and every flag read through c sticks to it. The router works it out once per request, only when something reads it.
For logged-out traffic, anonymousVisitor() makes a random vid on a visitor’s first request, sets it in the og-vid cookie and returns { vid, anonymous: true }. It sticks only when the table is served from a catch-all route; served from the hook, the cookie is dropped and every request gets a new id (#2292). Set the cookie in a handle of your own, as below, in that case.
import { routes, page, anonymousVisitor } from '@ogygia/router';
import Home from '$lib/views/Home.svelte';
import { sessions } from '$lib/server/sessions';
const anon = anonymousVisitor(); // { cookie: 'og-vid', days: 365 } by default
export const router = routes(
{ '/': page(Home) },
{
visitor: (c) => {
const s = sessions.read(c.cookies?.get('session'));
return s ? { sub: s.userId } : anon(c);
}
}
);Plain SvelteKit
Flags don’t need the server router. A Kit RequestEvent has request, url and cookies, so you can pass it straight to a flag, but it has no visitor. A read then falls back to the og-vid cookie, and with no cookie the visitor is nobody. So until you provide an identity, every weighted flag and every rollout answers its control for everyone.
For signed-in visitors, pass a context with a visitor. The flagContext helper above keeps that in one place:
For logged-out visitors, set the og-vid cookie in a handle, which is what anonymousVisitor() does on the router. Kit’s cookies.get returns a cookie set earlier in the same request, so the very first request already sticks. The handle also imports your flags module, so every flag is registered before the first request.
import { hero } from '$lib/flags';
import { flagContext } from '$lib/flag-context';
import type { PageServerLoad } from './$types';
export const load: PageServerLoad = (event) => ({
variant: hero(flagContext(event))
});import type { Handle } from '@sveltejs/kit';
import { sequence } from '@sveltejs/kit/hooks';
import * as ogygia from 'ogygia/server';
import '$lib/flags';
const visitorCookie: Handle = ({ event, resolve }) => {
if (!event.cookies.get('og-vid')) {
event.cookies.set('og-vid', crypto.randomUUID(), {
path: '/',
maxAge: 365 * 86_400,
httpOnly: true,
sameSite: 'lax'
});
}
return resolve(event);
};
export const handle = sequence(visitorCookie, ogygia.handle());Sources and exposures
A decision source runs at the start of the request, before the router or your load, so it sees only the identity it can find on its own: the claims of a federation hop, else the og-vid cookie. It never sees a signed-in user that the router’s visitor or your flagContext works out later. When a vendor should target signed-in users, give the source a context that reads the session from the request (OpenFeature).
Exposure events carry sub from the visitor on the context you read the flag with. A read keyed only on a vid, from the cookie or anonymousVisitor(), is sticky but its event has no sub and no vid. Pass a signed-in visitor if your analytics needs to tie events to a person.