@@ -32,6 +32,18 @@ export interface RequestData {
32
32
body ?: ReadableStream < Uint8Array >
33
33
}
34
34
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
+
35
47
const handler = async ( req : Request , context : Context ) => {
36
48
const url = new URL ( req . url )
37
49
if ( url . pathname . startsWith ( '/_next/static/' ) ) {
@@ -44,18 +56,15 @@ const handler = async (req: Request, context: Context) => {
44
56
city : context . geo . city ,
45
57
}
46
58
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
+ }
59
68
60
69
const request : RequestData = {
61
70
headers : Object . fromEntries ( req . headers . entries ( ) ) ,
@@ -72,6 +81,10 @@ const handler = async (req: Request, context: Context) => {
72
81
} catch ( error ) {
73
82
console . error ( error )
74
83
return new Response ( error . message , { status : 500 } )
84
+ } finally {
85
+ if ( requestId ) {
86
+ globalThis . NFRequestContextMap . delete ( requestId )
87
+ }
75
88
}
76
89
}
77
90
0 commit comments