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

Function proxy

flags-sdk/flagsmith/proxy.ts:11–38  ·  view source on GitHub ↗
(request: NextRequest)

Source from the content-addressed store, hash-verified

9}
10
11export async function proxy(request: NextRequest) {
12 const stableId = await getStableId()
13 const cartId = await getCartId()
14
15 const code = await precompute(productFlags)
16
17 // rewrites the request to the variant for this flag combination
18 const nextUrl = new URL(
19 `/${code}${request.nextUrl.pathname}${request.nextUrl.search}`,
20 request.url
21 )
22
23 // Add a header to the request to indicate that the stable id is generated,
24 // as it will not be present on the cookie request header on the first-ever request.
25 if (cartId.isFresh) {
26 request.headers.set('x-generated-cart-id', cartId.value)
27 }
28
29 if (stableId.isFresh) {
30 request.headers.set('x-generated-stable-id', stableId.value)
31 }
32
33 // response headers
34 const headers = new Headers()
35 headers.append('set-cookie', `stable-id=${stableId.value}`)
36 headers.append('set-cookie', `cart-id=${cartId.value}`)
37 return NextResponse.rewrite(nextUrl, { request, headers })
38}

Callers

nothing calls this directly

Calls 1

setMethod · 0.80

Tested by

no test coverage detected