mirror of
https://github.com/vercel/flags.git
synced 2026-09-19 03:49:23 +08:00
ba4eec7b23
* remove @pyra/eslint-config * wip * wip * convert shirt-shop-api * convert shirt-shop * convert snippets * convert next-13 * convert next-14 * convert next-15 * eslint configs * upgrade prettier * use eslint v9 * run prettier * resolve type-check * adjust eslint * drop Next.js v13 * fix next-14 Avoid accidentally using implicitly installed eslint v8 * update lockfile * rm @eslint/eslintrc * add @next/eslint-plugin-next * biome * biome fixes * wip * apply formatting * tabs * rm prettier * spaces * fix svelte * update lockfile * single quotes * format all * rm eslint-disable comments * upgrade playwright * upgrade publint * upgrade playwright ci workflow * try chromium headless * try adding executablePath
69 lines
2.1 KiB
TypeScript
69 lines
2.1 KiB
TypeScript
import { normalizeUrl } from '@sveltejs/kit';
|
|
import { rewrite } from '@vercel/edge';
|
|
import { parse } from 'cookie';
|
|
import { marketingABTestManualApproach } from './src/lib/flags';
|
|
import {
|
|
computeInternalRoute,
|
|
createVisitorId,
|
|
} from './src/lib/precomputed-flags';
|
|
|
|
export const config = {
|
|
// Either run middleware on all but the internal asset paths ...
|
|
// matcher: '/((?!_app/|favicon.ico|favicon.png).*)'
|
|
// ... or only run it where you actually need it (more performant).
|
|
matcher: [
|
|
'/examples/marketing-pages-manual-approach',
|
|
'/examples/marketing-pages',
|
|
// add more paths here if you want to run A/B tests on other pages, e.g.
|
|
// '/something-else'
|
|
],
|
|
};
|
|
|
|
export default async function middleware(request: Request) {
|
|
const { url, denormalize } = normalizeUrl(request.url);
|
|
|
|
if (url.pathname === '/examples/marketing-pages-manual-approach') {
|
|
// Retrieve cookies which contain the feature flags.
|
|
let flag = parse(request.headers.get('cookie') ?? '').marketingManual || '';
|
|
|
|
if (!flag) {
|
|
flag = String(Math.random() < 0.5);
|
|
request.headers.set('x-marketingManual', flag); // cookie is not available on the initial request
|
|
}
|
|
|
|
return rewrite(
|
|
// Get destination URL based on the feature flag
|
|
denormalize(
|
|
(await marketingABTestManualApproach(request))
|
|
? '/examples/marketing-pages-variant-a'
|
|
: '/examples/marketing-pages-variant-b',
|
|
),
|
|
{
|
|
headers: {
|
|
'Set-Cookie': `marketingManual=${flag}; Path=/`,
|
|
},
|
|
},
|
|
);
|
|
}
|
|
|
|
if (url.pathname === '/examples/marketing-pages') {
|
|
// Retrieve cookies which contain the feature flags.
|
|
let visitorId = parse(request.headers.get('cookie') ?? '').visitorId || '';
|
|
|
|
if (!visitorId) {
|
|
visitorId = createVisitorId();
|
|
request.headers.set('x-visitorId', visitorId); // cookie is not available on the initial request
|
|
}
|
|
|
|
return rewrite(
|
|
// Get destination URL based on the feature flag
|
|
denormalize(await computeInternalRoute(url.pathname, request)),
|
|
{
|
|
headers: {
|
|
'Set-Cookie': `visitorId=${visitorId}; Path=/`,
|
|
},
|
|
},
|
|
);
|
|
}
|
|
}
|