mirror of
https://github.com/vercel/flags.git
synced 2026-09-19 03:49:23 +08:00
422435c0eb
* @vercel-flags » flags * update lockfile * fix name
49 lines
1.8 KiB
TypeScript
49 lines
1.8 KiB
TypeScript
import { precompute } from 'flags/next';
|
|
import { type NextRequest, NextResponse } from 'next/server';
|
|
import { precomputedFlags } from './flags';
|
|
|
|
export const config = {
|
|
matcher: ['/', '/pages-router', '/app-router-static', '/pages-router-static'],
|
|
};
|
|
|
|
export default async function middleware(request: NextRequest) {
|
|
if (
|
|
request.nextUrl.pathname === '/' ||
|
|
request.nextUrl.pathname === '/pages-router'
|
|
) {
|
|
// pretend the request had a cookie so we can reliably test it without having
|
|
// to actually set a cookie in the browser. this way it works during local dev easily
|
|
const requestClone = request.clone();
|
|
requestClone.headers.set('cookie', 'example-cookie=example-cookie-value');
|
|
return NextResponse.next({ request: requestClone });
|
|
}
|
|
|
|
if (request.nextUrl.pathname === '/app-router-static') {
|
|
// pretend the request had a cookie so we can reliably test it without having
|
|
// to actually set a cookie in the browser. this way it works during local dev easily
|
|
request.cookies.set('example-cookie', 'example-cookie-value');
|
|
const code = await precompute(precomputedFlags);
|
|
const nextUrl = new URL(
|
|
`/app-router-precomputed/${code}${request.nextUrl.search}`,
|
|
request.url,
|
|
);
|
|
|
|
return NextResponse.rewrite(nextUrl.toString());
|
|
}
|
|
|
|
if (request.nextUrl.pathname === '/pages-router-static') {
|
|
// pretend the request had a cookie so we can reliably test it without having
|
|
// to actually set a cookie in the browser. this way it works during local dev easily
|
|
request.cookies.set('example-cookie', 'example-cookie-value');
|
|
const code = await precompute(precomputedFlags);
|
|
const nextUrl = new URL(
|
|
`/pages-router-precomputed/${code}${request.nextUrl.search}`,
|
|
request.url,
|
|
);
|
|
|
|
return NextResponse.rewrite(nextUrl.toString(), { request });
|
|
}
|
|
|
|
return NextResponse.next();
|
|
}
|