Skip to content

Commit 836e157

Browse files
fixup! enable getCloudflareContext to work in middlewares via a new enableEdgeDevGetCloudflareContext utility
create new `monkeyPatchVmModuleEdgeContext` commented function
1 parent 2754445 commit 836e157

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

packages/cloudflare/src/api/cloudflare-context.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff 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
*/
114114
export 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);

0 commit comments

Comments
 (0)