@@ -130,21 +130,34 @@ globalThis.Buffer = Buffer;
130
130
131
131
import {AsyncLocalStorage} from "node:async_hooks";
132
132
globalThis.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
+
133
154
${
134
155
isInCloudfare
135
156
? ""
136
157
: `
137
158
const require = (await import("node:module")).createRequire(import.meta.url);
138
159
const __filename = (await import("node:url")).fileURLToPath(import.meta.url);
139
160
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
- };
148
161
`
149
162
}
150
163
${ additionalInject ?? "" }
0 commit comments