MCPcopy Create free account
hub / github.com/vercel/examples / proxy

Function proxy

flags-sdk/vercel/proxy.ts:10–46  ·  view source on GitHub ↗
(request: NextRequest)

Source from the content-addressed store, hash-verified

8}
9
10export async function proxy(request: NextRequest) {
11 const hasFlagsSecret = Boolean(process.env.FLAGS_SECRET)
12 if (!hasFlagsSecret) {
13 return NextResponse.rewrite(new URL('/setup', request.url))
14 }
15
16 // Demo-only: lazily import flags after env validation to avoid loading
17 // the provider setup before users complete /setup.
18 const { productFlags } = await import('@/flags')
19
20 const stableId = await getStableId()
21 const cartId = await getCartId()
22
23 const code = await precompute(productFlags)
24
25 // rewrites the request to the variant for this flag combination
26 const nextUrl = new URL(
27 `/${code}${request.nextUrl.pathname}${request.nextUrl.search}`,
28 request.url
29 )
30
31 // Add a header to the request to indicate that the stable id is generated,
32 // as it will not be present on the cookie request header on the first-ever request.
33 if (cartId.isFresh) {
34 request.headers.set('x-generated-cart-id', cartId.value)
35 }
36
37 if (stableId.isFresh) {
38 request.headers.set('x-generated-stable-id', stableId.value)
39 }
40
41 // response headers
42 const headers = new Headers()
43 headers.append('set-cookie', `stable-id=${stableId.value}`)
44 headers.append('set-cookie', `cart-id=${cartId.value}`)
45 return NextResponse.rewrite(nextUrl, { request, headers })
46}

Callers

nothing calls this directly

Calls 1

setMethod · 0.80

Tested by

no test coverage detected