File tree Expand file tree Collapse file tree 1 file changed +13
-0
lines changed
packages/cloudflare/src/api Expand file tree Collapse file tree 1 file changed +13
-0
lines changed Original file line number Diff line number Diff line change @@ -112,6 +112,19 @@ type RuntimeContext = Record<string, unknown> & {
112112 * Note: this function should only be called inside the Next.js config file
113113 */
114114export async function enableEdgeDevGetCloudflareContext ( ) {
115+ await monkeyPatchVmModuleEdgeContext ( ) ;
116+ }
117+
118+ /**
119+ * Next.js uses the Node.js vm module's `runInContext()` function to evaluate the edge functions
120+ * in a runtime context that tries to simulate as accurately as possible the actual production runtime
121+ * behavior, see: https://github.com/vercel/next.js/blob/9a1cd3/packages/next/src/server/web/sandbox/context.ts#L525-L527
122+ *
123+ * This function monkey-patches the Node.js `vm` module to override the `runInContext()` function so that the
124+ * cloudflare context (which is created using the platform proxy obtained via wrangler's `getPlatformProxy` utility)
125+ * can be added to the runtime context's `process.env` before the actual edge functions are evaluated.
126+ */
127+ async function monkeyPatchVmModuleEdgeContext ( ) {
115128 const require = (
116129 await import ( /* webpackIgnore: true */ `${ "__module" . replaceAll ( "_" , "" ) } ` )
117130 ) . default . createRequire ( import . meta. url ) ;
You can’t perform that action at this time.
0 commit comments