Skip to content

Commit d4c0dc0

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

File tree

2 files changed

+35
-29
lines changed

2 files changed

+35
-29
lines changed

plugin/src/middleware/request.ts

+10-17
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable no-underscore-dangle */
21
import { NextURL } from 'next/dist/server/web/next-url'
32
import { NextResponse } from 'next/server'
43
import type { NextRequest } from 'next/server'
@@ -10,14 +9,6 @@ type Context = {
109
next: () => Promise<Response>
1110
}
1211

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-
2112
/**
2213
* Supercharge your Next middleware with Netlify Edge Functions
2314
*/
@@ -28,14 +19,18 @@ export class MiddlewareRequest extends Request {
2819
constructor(private nextRequest: NextRequest) {
2920
super(nextRequest)
3021
if (!('Deno' in globalThis)) {
31-
throw new Error('NetlifyMiddleware only works in a Netlify Edge Function environment')
22+
throw new Error('MiddlewareRequest only works in a Netlify Edge Function environment')
3223
}
33-
const geo = nextRequest.geo as AugmentedGeo
34-
if (!geo) {
35-
throw new Error('NetlifyMiddleware must be instantiated with a NextRequest object')
24+
const requestId = nextRequest.headers.get('x-nf-request-id')
25+
if (!requestId) {
26+
throw new Error('Missing x-nf-request-id header')
3627
}
37-
this.context = geo.__nf_context
38-
this.originalRequest = geo.__nf_request
28+
const requestContext = globalThis.NFRequestContextMap.get(requestId)
29+
if (!requestContext) {
30+
throw new Error(`Could not find request context for request id ${requestId}`)
31+
}
32+
this.context = requestContext.context
33+
this.originalRequest = requestContext.request
3934
}
4035

4136
// Add the headers to the original request, which will be passed to the origin
@@ -83,5 +78,3 @@ export class MiddlewareRequest extends Request {
8378
return this.nextRequest.url.toString()
8479
}
8580
}
86-
87-
/* eslint-enable no-underscore-dangle */

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)