-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[next]: fix issues with rewrite normalization of index routes (#11707)
The Next.js builder currently normalizes user rewrites to ensure the internal `.rsc` / `.action` rewrites are still handled even after following the rewrite. However we have special rewrites: `/index.rsc` and `/index.action`. When those routes pass through the normalization logic, a request routed to a catch-all route (e.g. `[[...param]]`) will go from `/` -> `/index.rsc` -> `<someRewriteString>?param=index`. This is inconsistent `next start` or other spots where the param should be an empty string. Similarly, if a user rewrites their entire app to a subdirectory (eg `/:path*` -> `/base/:path*`, a route will go from `/` -> `/index.rsc` -> `/base/index.rsc` which won't exist, causing a 404. The solution here is to return the `index.rsc` / `index.action` routes back to `/` so they can be handled by user rewrites, if necessary. This also disables the `hasActionOutputSupport` flag if `routesManifest.i18n` is detected as they are not compatible.
- Loading branch information
Showing
18 changed files
with
273 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@vercel/next': patch | ||
--- | ||
|
||
prevent /index from being incorrectly normalized in rewrites |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
...ext/test/fixtures/00-app-dir-actions-experimental-streaming-index-handling/app/actions.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
'use server'; | ||
|
||
export async function increment(value) { | ||
await new Promise(resolve => setTimeout(resolve, 500)); | ||
return value + 1; | ||
} |
10 changes: 10 additions & 0 deletions
10
...next/test/fixtures/00-app-dir-actions-experimental-streaming-index-handling/app/layout.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export default function Root({ children }) { | ||
return ( | ||
<html> | ||
<head> | ||
<title>Hello World</title> | ||
</head> | ||
<body>{children}</body> | ||
</html> | ||
); | ||
} |
25 changes: 25 additions & 0 deletions
25
...test/fixtures/00-app-dir-actions-experimental-streaming-index-handling/app/static/page.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
"use client"; | ||
|
||
import { useState } from "react"; | ||
import { increment } from "../actions"; | ||
|
||
export default function Home() { | ||
const [count, setCount] = useState(0); | ||
|
||
return ( | ||
<div> | ||
{count} | ||
<button | ||
onClick={async () => { | ||
const actionResult = await increment(count); | ||
// @ts-ignore | ||
setCount(actionResult); | ||
console.log(actionResult); | ||
}} | ||
> | ||
Trigger | ||
</button> | ||
Static | ||
</div> | ||
); | ||
} |
55 changes: 55 additions & 0 deletions
55
...next/test/fixtures/00-app-dir-actions-experimental-streaming-index-handling/index.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* eslint-env jest */ | ||
const path = require('path'); | ||
const { deployAndTest } = require('../../utils'); | ||
const fetch = require('../../../../../test/lib/deployment/fetch-retry'); | ||
|
||
const ctx = {}; | ||
|
||
function findActionId(page, runtime) { | ||
page = `app${page}/page`; // add /app prefix and /page suffix | ||
|
||
for (const [actionId, details] of Object.entries( | ||
ctx.actionManifest[runtime] | ||
)) { | ||
if (details.workers[page]) { | ||
return actionId; | ||
} | ||
} | ||
|
||
throw new Error("Couldn't find action ID"); | ||
} | ||
|
||
describe(`${__dirname.split(path.sep).pop()}`, () => { | ||
beforeAll(async () => { | ||
const info = await deployAndTest(__dirname); | ||
|
||
const actionManifest = await fetch( | ||
`${info.deploymentUrl}/server-reference-manifest.json` | ||
).then(res => res.json()); | ||
|
||
ctx.actionManifest = actionManifest; | ||
|
||
Object.assign(ctx, info); | ||
}); | ||
|
||
it('should work when there is a rewrite targeting the root page', async () => { | ||
const actionId = findActionId('/static', 'node'); | ||
|
||
const res = await fetch(ctx.deploymentUrl, { | ||
method: 'POST', | ||
body: JSON.stringify([1337]), | ||
headers: { | ||
'Content-Type': 'text/plain;charset=UTF-8', | ||
'Next-Action': actionId, | ||
}, | ||
}); | ||
|
||
expect(res.status).toEqual(200); | ||
expect(res.headers.get('x-matched-path')).toBe('/static/'); | ||
expect(res.headers.get('x-vercel-cache')).toBe('BYPASS'); | ||
|
||
const body = await res.text(); | ||
// The action incremented the provided count by 1 | ||
expect(body).toContain('1338'); | ||
}); | ||
}); |
10 changes: 10 additions & 0 deletions
10
...ext/test/fixtures/00-app-dir-actions-experimental-streaming-index-handling/next.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module.exports = { | ||
rewrites() { | ||
return [ | ||
{ | ||
source: '/:path*', | ||
destination: '/static/:path*', | ||
}, | ||
]; | ||
}, | ||
}; |
9 changes: 9 additions & 0 deletions
9
.../next/test/fixtures/00-app-dir-actions-experimental-streaming-index-handling/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"dependencies": { | ||
"next": "canary" | ||
}, | ||
"scripts": { | ||
"build": "next build && cp .next/server/server-reference-manifest.json public/" | ||
}, | ||
"ignoreNextjsUpdates": true | ||
} |
Empty file.
14 changes: 14 additions & 0 deletions
14
...s/next/test/fixtures/00-app-dir-actions-experimental-streaming-index-handling/vercel.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"builds": [ | ||
{ | ||
"src": "package.json", | ||
"use": "@vercel/next" | ||
} | ||
], | ||
"build": { | ||
"env": { | ||
"NEXT_EXPERIMENTAL_STREAMING_ACTIONS": "1" | ||
} | ||
}, | ||
"probes": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
packages/next/test/fixtures/00-app-dir-catchall-index-handling/app/[[...slug]]/page.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import Link from 'next/link'; | ||
|
||
const Page = ({ params }) => { | ||
return ( | ||
<div> | ||
<div id="page-param">page-param-{params.slug?.[0] ?? ''}</div> | ||
<Link href="/">Home</Link> | ||
<Link href="/foo">Foo</Link> | ||
<Link href="/bar">Bar</Link> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Page; |
10 changes: 10 additions & 0 deletions
10
packages/next/test/fixtures/00-app-dir-catchall-index-handling/app/layout.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export default function Root({ children }) { | ||
return ( | ||
<html> | ||
<head> | ||
<title>Hello World</title> | ||
</head> | ||
<body>{children}</body> | ||
</html> | ||
); | ||
} |
12 changes: 12 additions & 0 deletions
12
packages/next/test/fixtures/00-app-dir-catchall-index-handling/index.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* eslint-env jest */ | ||
const path = require('path'); | ||
const { deployAndTest } = require('../../utils'); | ||
|
||
const ctx = {}; | ||
|
||
describe(`${__dirname.split(path.sep).pop()}`, () => { | ||
it('should deploy and pass probe checks', async () => { | ||
const info = await deployAndTest(__dirname); | ||
Object.assign(ctx, info); | ||
}); | ||
}); |
1 change: 1 addition & 0 deletions
1
packages/next/test/fixtures/00-app-dir-catchall-index-handling/next.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = {}; |
8 changes: 8 additions & 0 deletions
8
packages/next/test/fixtures/00-app-dir-catchall-index-handling/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"dependencies": { | ||
"next": "canary", | ||
"react": "19.0.0-rc-f994737d14-20240522", | ||
"react-dom": "19.0.0-rc-f994737d14-20240522" | ||
}, | ||
"ignoreNextjsUpdates": true | ||
} |
27 changes: 27 additions & 0 deletions
27
packages/next/test/fixtures/00-app-dir-catchall-index-handling/vercel.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"builds": [ | ||
{ | ||
"src": "package.json", | ||
"use": "@vercel/next" | ||
} | ||
], | ||
"probes": [ | ||
{ | ||
"path": "/", | ||
"status": 200, | ||
"mustContain": "\"page-param-\",\"\"", | ||
"mustNotContain": "\"page-param-\",\"index\"", | ||
"headers": { | ||
"RSC": "1" | ||
} | ||
}, | ||
{ | ||
"path": "/foo", | ||
"status": 200, | ||
"mustContain": "\"page-param-\",\"foo\"", | ||
"headers": { | ||
"RSC": "1" | ||
} | ||
} | ||
] | ||
} |