Skip to content

Commit 39dd3a3

Browse files
committed
fix for external middleware and apply only for next 15.4+
1 parent 1f00712 commit 39dd3a3

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

packages/open-next/src/build/createServerBundle.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,12 @@ async function generateBundle(
243243
"15.2.0",
244244
);
245245

246+
const isBefore154 = buildHelper.compareSemver(
247+
options.nextVersion,
248+
"<",
249+
"15.4.0",
250+
);
251+
246252
const disableRouting = isBefore13413 || config.middleware?.external;
247253

248254
const updater = new ContentUpdater(options);
@@ -260,6 +266,7 @@ async function generateBundle(
260266
...(disableRouting ? ["withRouting"] : []),
261267
...(isAfter142 ? ["patchAsyncStorage"] : []),
262268
...(isAfter141 ? ["appendPrefetch"] : []),
269+
...(isBefore154 ? ["setInitialURL"] : []),
263270
],
264271
}),
265272
openNextReplacementPlugin({

packages/open-next/src/core/requestHandler.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,10 @@ async function processRequest(
239239
// Here we try to apply as much request metadata as possible
240240
// We apply every metadata from `resolve-routes` https://github.com/vercel/next.js/blob/916f105b97211de50f8580f0b39c9e7c60de4886/packages/next/src/server/lib/router-utils/resolve-routes.ts
241241
// and `router-server` https://github.com/vercel/next.js/blob/916f105b97211de50f8580f0b39c9e7c60de4886/packages/next/src/server/lib/router-server.ts
242-
const initialURL = new URL(routingResult.initialURL);
242+
const initialURL = new URL(
243+
// We always assume that only the routing layer can set this header.
244+
routingResult.internalEvent.headers[INTERNAL_HEADER_INITIAL_URL] ?? routingResult.initialURL
245+
);
243246
let invokeStatus: number | undefined;
244247
if (routingResult.internalEvent.rawPath === "/500") {
245248
invokeStatus = 500;
@@ -268,10 +271,10 @@ async function processRequest(
268271

269272
// Next Server
270273
// TODO: only enable this on Next 15.4+
271-
const reqUrl = new URL(routingResult.initialURL);
272274
// We need to set the pathname to the data request path
273-
req.url = reqUrl.pathname + convertToQueryString(routingResult.internalEvent.query);
274-
// We need to set the headers to the request metadata
275+
//#override setInitialURL
276+
req.url = initialURL.pathname + convertToQueryString(routingResult.internalEvent.query);
277+
//#endOverride
275278

276279
await requestHandler(requestMetadata)(req, res);
277280
} catch (e: any) {

0 commit comments

Comments
 (0)