Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions packages/open-next/src/build/edge/createEdgeBundle.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { mkdirSync } from "node:fs";

Check failure on line 1 in packages/open-next/src/build/edge/createEdgeBundle.ts

View workflow job for this annotation

GitHub Actions / validate

format

File content differs from formatting output

import fs from "node:fs";
import path from "node:path";
Expand Down Expand Up @@ -130,21 +130,34 @@

import {AsyncLocalStorage} from "node:async_hooks";
globalThis.AsyncLocalStorage = AsyncLocalStorage;

${
''
/**
* Next.js sets this `__import_unsupported` on `globalThis` (with `configurable: false`):
* https://github.com/vercel/next.js/blob/5b7833e3/packages/next/src/server/web/globals.ts#L94-L98
*
* It does so in both the middleware and the main server, so if the middleware runs in the same place
* as the main handler this code gets run twice triggering a runtime error.
*
* For this reason we need to patch `Object.defineProperty` to avoid this issue.
*/
}
const defaultDefineProperty = Object.defineProperty;
Object.defineProperty = function(o, p, a) {
if(p=== '__import_unsupported' && Boolean(globalThis.__import_unsupported)) {
return;
}
return defaultDefineProperty(o, p, a);
};

${
isInCloudfare
? ""
: `
const require = (await import("node:module")).createRequire(import.meta.url);
const __filename = (await import("node:url")).fileURLToPath(import.meta.url);
const __dirname = (await import("node:path")).dirname(__filename);

const defaultDefineProperty = Object.defineProperty;
Object.defineProperty = function(o, p, a) {
if(p=== '__import_unsupported' && Boolean(globalThis.__import_unsupported)) {
return;
}
return defaultDefineProperty(o, p, a);
};
`
}
${additionalInject ?? ""}
Expand Down
Loading