@@ -130,21 +130,34 @@ globalThis.Buffer = Buffer;
130130
131131import {AsyncLocalStorage} from "node:async_hooks";
132132globalThis.AsyncLocalStorage = AsyncLocalStorage;
133+
134+ ${
135+ ""
136+ /**
137+ * Next.js sets this `__import_unsupported` on `globalThis` (with `configurable: false`):
138+ * https://github.com/vercel/next.js/blob/5b7833e3/packages/next/src/server/web/globals.ts#L94-L98
139+ *
140+ * It does so in both the middleware and the main server, so if the middleware runs in the same place
141+ * as the main handler this code gets run twice triggering a runtime error.
142+ *
143+ * For this reason we need to patch `Object.defineProperty` to avoid this issue.
144+ */
145+ }
146+ const defaultDefineProperty = Object.defineProperty;
147+ Object.defineProperty = function(o, p, a) {
148+ if(p=== '__import_unsupported' && Boolean(globalThis.__import_unsupported)) {
149+ return;
150+ }
151+ return defaultDefineProperty(o, p, a);
152+ };
153+
133154 ${
134155 isInCloudfare
135156 ? ""
136157 : `
137158 const require = (await import("node:module")).createRequire(import.meta.url);
138159 const __filename = (await import("node:url")).fileURLToPath(import.meta.url);
139160 const __dirname = (await import("node:path")).dirname(__filename);
140-
141- const defaultDefineProperty = Object.defineProperty;
142- Object.defineProperty = function(o, p, a) {
143- if(p=== '__import_unsupported' && Boolean(globalThis.__import_unsupported)) {
144- return;
145- }
146- return defaultDefineProperty(o, p, a);
147- };
148161 `
149162 }
150163 ${ additionalInject ?? "" }
0 commit comments