Skip to content

Commit

Permalink
Rework prefetch route handling (#10779)
Browse files Browse the repository at this point in the history
Follow-up to #10750 this removes the underscore prefetching from all prefetch outputs and instead only applies it to the index route itself as this causes issues with PPR making these outputs prerenders and being able to interpolate the route param values. 

There is the edge case of a user returning the literal value `index` from `generateStaticParams` but we can tolerate that more than ppr not working as expected.
  • Loading branch information
ijjk committed Oct 31, 2023
1 parent d9065c2 commit f5296c3
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 64 deletions.
5 changes: 5 additions & 0 deletions .changeset/tricky-rules-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@vercel/next": patch
---

Rework prefetch route handling
4 changes: 2 additions & 2 deletions packages/next/src/server-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1642,7 +1642,7 @@ export async function serverBuild({
dest: path.posix.join(
'/',
entryDirectory,
`/__$1${RSC_PREFETCH_SUFFIX}`
`/$1${RSC_PREFETCH_SUFFIX}`
),
headers: { vary: rscVaryHeader },
continue: true,
Expand Down Expand Up @@ -1737,7 +1737,7 @@ export async function serverBuild({
src: `^${path.posix.join(
'/',
entryDirectory,
`/__(.+?)${RSC_PREFETCH_SUFFIX}(?:/)?$`
`/(.+?)${RSC_PREFETCH_SUFFIX}(?:/)?$`
)}`,
dest: path.posix.join('/', entryDirectory, '/$1.rsc'),
has: [
Expand Down
8 changes: 6 additions & 2 deletions packages/next/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3127,8 +3127,12 @@ export function normalizePrefetches(prefetches: Record<string, FileFsRef>) {
const updatedPrefetches: Record<string, FileFsRef> = {};

for (const key in prefetches) {
const newKey = key.replace(/([^/]+\.prefetch\.rsc)$/, '__$1');
updatedPrefetches[newKey] = prefetches[key];
if (key === 'index.prefetch.rsc') {
const newKey = key.replace(/([^/]+\.prefetch\.rsc)$/, '__$1');
updatedPrefetches[newKey] = prefetches[key];
} else {
updatedPrefetches[key] = prefetches[key];
}
}

return updatedPrefetches;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ export function generateStaticParams() {
{
slug: [''],
},
{
slug: ['index'],
},
// this case is not supported
// {
// slug: ['index'],
// },
{
slug: ['first'],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ export function generateStaticParams() {
{
slug: [''],
},
{
slug: ['index'],
},
// not supported
// {
// slug: ['index'],
// },
{
slug: ['first'],
},
Expand Down
38 changes: 0 additions & 38 deletions packages/next/test/fixtures/00-app-dir-root-catch-all/vercel.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,6 @@
"mustNotContain": "<html",
"mustContain": "catch-all"
},
{
"path": "/index",
"status": 200,
"mustContain": "html"
},
{
"path": "/index",
"status": 200,
"mustContain": "catch-all"
},
{
"path": "/index",
"status": 200,
"headers": {
"RSC": 1
},
"mustNotContain": "<html",
"mustContain": "catch-all"
},
{
"path": "/nested",
"status": 200,
Expand All @@ -63,25 +44,6 @@
"mustNotContain": "<html",
"mustContain": "catch-all"
},
{
"path": "/nested/index",
"status": 200,
"mustContain": "html"
},
{
"path": "/nested/index",
"status": 200,
"mustContain": "catch-all"
},
{
"path": "/nested/index",
"status": 200,
"headers": {
"RSC": 1
},
"mustNotContain": "<html",
"mustContain": "catch-all"
},
{
"path": "/first",
"status": 200,
Expand Down
4 changes: 4 additions & 0 deletions packages/next/test/integration/integration-1.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,10 @@ it('Should throw when package.json or next.config.js is not the "src"', async ()
}
});

it('Should build the serverless-config-async example', async () => {
await runBuildLambda(path.join(__dirname, 'serverless-config-async'));
});

describe('Middleware simple project', () => {
const ctx = {};

Expand Down
12 changes: 0 additions & 12 deletions packages/next/test/integration/legacy/integration-legacy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,18 +247,6 @@ it('Should opt-out of shared lambdas when routes are detected', async () => {
expect(hasUnderScoreErrorStaticFile).toBeTruthy();
});

it('Should build the serverless-config-async example', async () => {
let error = null;

try {
await runBuildLambda(path.join(__dirname, '..', 'serverless-config-async'));
} catch (err) {
error = err;
}

expect(error).toBe(null);
});

it('Should provide lambda info when limit is hit (shared lambdas)', async () => {
let logs = '';

Expand Down
8 changes: 4 additions & 4 deletions packages/next/test/unit/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,10 +426,10 @@ describe('normalizePrefetches', () => {

expect(Object.keys(updatedPrefetches)).toEqual([
'__index.prefetch.rsc',
'index/__index.prefetch.rsc',
'__foo.prefetch.rsc',
'foo/__index.prefetch.rsc',
'foo/bar/__baz.prefetch.rsc',
'index/index.prefetch.rsc',
'foo.prefetch.rsc',
'foo/index.prefetch.rsc',
'foo/bar/baz.prefetch.rsc',
]);
});
});

0 comments on commit f5296c3

Please sign in to comment.