Skip to content

Commit b82d367

Browse files
committed
chore: switch from hidden fields to global map
1 parent 468a0d3 commit b82d367

File tree

2 files changed

+35
-26
lines changed

2 files changed

+35
-26
lines changed

plugin/src/middleware/request.ts

+10-14
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,6 @@ type Context = {
1010
next: () => Promise<Response>
1111
}
1212

13-
// We sneak our own request and context into the middleware using the geo object
14-
type AugmentedGeo = NextRequest['geo'] & {
15-
// eslint-disable-next-line camelcase
16-
__nf_context: Context
17-
// eslint-disable-next-line camelcase
18-
__nf_request: Request
19-
}
20-
2113
/**
2214
* Supercharge your Next middleware with Netlify Edge Functions
2315
*/
@@ -28,14 +20,18 @@ export class MiddlewareRequest extends Request {
2820
constructor(private nextRequest: NextRequest) {
2921
super(nextRequest)
3022
if (!('Deno' in globalThis)) {
31-
throw new Error('NetlifyMiddleware only works in a Netlify Edge Function environment')
23+
throw new Error('MiddlewareRequest only works in a Netlify Edge Function environment')
24+
}
25+
const requestId = nextRequest.headers.get('x-nf-request-id')
26+
if (!requestId) {
27+
throw new Error('Missing x-nf-request-id header')
3228
}
33-
const geo = nextRequest.geo as AugmentedGeo
34-
if (!geo) {
35-
throw new Error('NetlifyMiddleware must be instantiated with a NextRequest object')
29+
const requestContext = globalThis.NFRequestContextMap.get(requestId)
30+
if (!requestContext) {
31+
throw new Error(`Could not find request context for request id ${requestId}`)
3632
}
37-
this.context = geo.__nf_context
38-
this.originalRequest = geo.__nf_request
33+
this.context = requestContext.context
34+
this.originalRequest = requestContext.request
3935
}
4036

4137
// Add the headers to the original request, which will be passed to the origin

plugin/src/templates/edge/runtime.ts

+25-12
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ export interface RequestData {
3232
body?: ReadableStream<Uint8Array>
3333
}
3434

35+
export interface RequestContext {
36+
request: Request
37+
context: Context
38+
}
39+
40+
declare global {
41+
// deno-lint-ignore no-var
42+
var NFRequestContextMap: Map<string, RequestContext>
43+
}
44+
45+
globalThis.NFRequestContextMap = new Map()
46+
3547
const handler = async (req: Request, context: Context) => {
3648
const url = new URL(req.url)
3749
if (url.pathname.startsWith('/_next/static/')) {
@@ -44,18 +56,15 @@ const handler = async (req: Request, context: Context) => {
4456
city: context.geo.city,
4557
}
4658

47-
// The geo object is passed through to the middleware unchanged
48-
// so we're smuggling the Request and Netlify context object inside it
49-
50-
Object.defineProperty(geo, '__nf_context', {
51-
value: context,
52-
enumerable: false,
53-
})
54-
55-
Object.defineProperty(geo, '__nf_request', {
56-
value: req,
57-
enumerable: false,
58-
})
59+
const requestId = req.headers.get('x-nf-request-id')
60+
if (!requestId) {
61+
console.error('Missing x-nf-request-id header')
62+
} else {
63+
globalThis.NFRequestContextMap.set(requestId, {
64+
request: req,
65+
context,
66+
})
67+
}
5968

6069
const request: RequestData = {
6170
headers: Object.fromEntries(req.headers.entries()),
@@ -72,6 +81,10 @@ const handler = async (req: Request, context: Context) => {
7281
} catch (error) {
7382
console.error(error)
7483
return new Response(error.message, { status: 500 })
84+
} finally {
85+
if (requestId) {
86+
globalThis.NFRequestContextMap.delete(requestId)
87+
}
7588
}
7689
}
7790

0 commit comments

Comments
 (0)