Skip to content

Commit 071d94e

Browse files
conico974Nicolas Dorseuil
andauthored
Fix for Next 15.4+ (#928)
* move to canary for test * allow to run some examples locally * fix page router * fix for external middleware and apply only for next 15.4+ * bump to 15.4.2 * linting * Create tricky-pans-cross.md * review --------- Co-authored-by: Nicolas Dorseuil <[email protected]>
1 parent 6cb7105 commit 071d94e

File tree

10 files changed

+333
-213
lines changed

10 files changed

+333
-213
lines changed

.changeset/tricky-pans-cross.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opennextjs/aws": patch
3+
---
4+
5+
Fix for Next 15.4+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import type { OpenNextConfig } from "@opennextjs/aws/types/open-next.js";
2+
3+
export default {
4+
default: {
5+
override: {
6+
wrapper: "express-dev",
7+
converter: "node",
8+
incrementalCache: "fs-dev",
9+
queue: "direct",
10+
tagCache: "dummy",
11+
},
12+
},
13+
14+
imageOptimization: {
15+
override: {
16+
wrapper: "dummy",
17+
converter: "dummy",
18+
},
19+
loader: "fs-dev",
20+
},
21+
22+
// You can override the build command here so that you don't have to rebuild next every time you make a change
23+
//buildCommand: "echo 'No build command'",
24+
} satisfies OpenNextConfig;

examples/app-router/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"version": "0.1.24",
44
"private": true,
55
"scripts": {
6-
"openbuild": "node ../../packages/open-next/dist/index.js build --streaming --build-command \"npx turbo build\"",
6+
"openbuild": "node ../../packages/open-next/dist/index.js build",
7+
"openbuild:local": "node ../../packages/open-next/dist/index.js build --config-path open-next.config.local.ts",
78
"dev": "next dev --turbopack --port 3001",
89
"build": "next build",
910
"start": "next start --port 3001",

examples/app-router/tsconfig.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,9 @@
2424
}
2525
},
2626
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
27-
"exclude": ["node_modules"]
27+
"exclude": [
28+
"node_modules",
29+
"open-next.config.ts",
30+
"open-next.config.local.ts"
31+
]
2832
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export default {
2+
default: {
3+
override: {
4+
wrapper: "express-dev",
5+
converter: "node",
6+
incrementalCache: "fs-dev",
7+
queue: "direct",
8+
tagCache: "dummy",
9+
},
10+
},
11+
12+
imageOptimization: {
13+
override: {
14+
wrapper: "dummy",
15+
converter: "dummy",
16+
},
17+
loader: "fs-dev",
18+
},
19+
20+
// You can override the build command here so that you don't have to rebuild next every time you make a change
21+
//buildCommand: "echo 'No build command'",
22+
};

examples/pages-router/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"private": true,
55
"scripts": {
66
"openbuild": "node ../../packages/open-next/dist/index.js build --build-command \"npx turbo build\"",
7+
"openbuild:local": "node ../../packages/open-next/dist/index.js build --config-path open-next.config.local.ts",
78
"dev": "next dev --turbopack --port 3002",
89
"build": "next build",
910
"start": "next start --port 3002",

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 isAfter154 = 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+
...(isAfter154 ? [] : ["setInitialURL"]),
263270
],
264271
}),
265272
openNextReplacementPlugin({

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
constructNextUrl,
1919
convertRes,
2020
convertToQuery,
21+
convertToQueryString,
2122
createServerResponse,
2223
} from "./routing/util";
2324
import routingHandler, {
@@ -238,7 +239,11 @@ async function processRequest(
238239
// Here we try to apply as much request metadata as possible
239240
// 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
240241
// and `router-server` https://github.com/vercel/next.js/blob/916f105b97211de50f8580f0b39c9e7c60de4886/packages/next/src/server/lib/router-server.ts
241-
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] ??
245+
routingResult.initialURL,
246+
);
242247
let invokeStatus: number | undefined;
243248
if (routingResult.internalEvent.rawPath === "/500") {
244249
invokeStatus = 500;
@@ -266,6 +271,14 @@ async function processRequest(
266271
//#endOverride
267272

268273
// Next Server
274+
// TODO: only enable this on Next 15.4+
275+
// We need to set the pathname to the data request path
276+
//#override setInitialURL
277+
req.url =
278+
initialURL.pathname +
279+
convertToQueryString(routingResult.internalEvent.query);
280+
//#endOverride
281+
269282
await requestHandler(requestMetadata)(req, res);
270283
} catch (e: any) {
271284
// This might fail when using bundled next, importing won't do the trick either

0 commit comments

Comments
 (0)