Unverified Commit b8675611 authored by Wyatt Johnson's avatar Wyatt Johnson Committed by GitHub

fix: handle incremental PPR with client segment cache (#78387)

This pull request introduces changes to enhance the handling of prefetch
segment data routes in the Next.js build process and adds new test cases
to validate these updates. Key changes include the addition of a new
`buildInversePrefetchSegmentDataRoute` utility, modifications to the
route generation logic, and the introduction of experimental features
like `clientSegmentCache`. This addresses a bug where conflicting routes
while incremental PPR was enabled that matched routes incorrectly, for
example:

```
/~/monitoring <- PPR disabled
/[teamSlug]/monitoring <- PPR enabled
```

Would previously cause client segment requests for `/~/monitoring` to
match `/[teamSlug]/monitoring`. This addresses it by adding early
rewrites to handle this case. This will be removed in future versions
when incremental PPR is deprecated.

### Enhancements to prefetch segment data routes:

* Added a new utility, `buildInversePrefetchSegmentDataRoute`, to
support inverted prefetch segment data routes, which rewrite from
segment paths back to prefetch RSC routes. This is particularly useful
for handling dynamic routes without conflicts when partial prerendering
is disabled
(`packages/next/src/server/lib/router-utils/build-prefetch-segment-data-route.ts`).
* Updated the `PrefetchSegmentDataRoute` type to include `routeKeys` for
better route matching and added logic for generating these keys
(`packages/next/src/server/lib/router-utils/build-prefetch-segment-data-route.ts`).
[[1]](diffhunk://#diff-35b900720f7fd8bc8995f5dad9880b2ffafba0638ec6c949efc9ba34b0acc9cbR15)
[[2]](diffhunk://#diff-35b900720f7fd8bc8995f5dad9880b2ffafba0638ec6c949efc9ba34b0acc9cbL27-R29)
* Modified the build process to utilize
`buildInversePrefetchSegmentDataRoute` for dynamic routes with undefined
segment paths when `clientSegmentCache` is enabled
(`packages/next/src/build/index.ts`).

### Codebase improvements:

* Added `originalAppPath` to the `PageInfo` interface to track the
original path of app pages and routes, improving route handling logic
(`packages/next/src/build/utils.ts`).
* Simplified the logic for appending prefetch segment data routes by
directly pushing the result of `buildPrefetchSegmentDataRoute` instead
of using intermediate variables (`packages/next/src/build/index.ts`).

### Testing and experimental features:

* Added comprehensive unit tests for
`buildInversePrefetchSegmentDataRoute` to ensure correct behavior and
compatibility with existing route utilities
(`packages/next/src/server/lib/router-utils/build-prefetch-segment-data-route.test.ts`).
* Introduced a new E2E test suite for validating conflicting routes and
prefetching behavior under the experimental `clientSegmentCache` feature
(`test/e2e/app-dir/segment-cache/conflicting-routes`).
* Enabled experimental features like `clientSegmentCache` and `ppr`
(partial prerendering) in the test configuration to validate their
integration
(`test/e2e/app-dir/segment-cache/conflicting-routes/next.config.js`).

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as s
moothly as possible we request that you follow the checklist sections
below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the
PR.
- Read the Docs Contribution Guide to ensure your contribution follows
the docs guidelines:
https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc
https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See
https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature
request has been accepted for implementation before opening a PR. (A
discussion must be opened, see
https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added
(https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to
understand the PR)
- When linking to a Slack thread, you might want to share details of the
conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic
behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->
parent 8ed3b934
...@@ -202,6 +202,7 @@ import { InvariantError } from '../shared/lib/invariant-error' ...@@ -202,6 +202,7 @@ import { InvariantError } from '../shared/lib/invariant-error'
import { HTML_LIMITED_BOT_UA_RE_STRING } from '../shared/lib/router/utils/is-bot' import { HTML_LIMITED_BOT_UA_RE_STRING } from '../shared/lib/router/utils/is-bot'
import type { UseCacheTrackerKey } from './webpack/plugins/telemetry-plugin/use-cache-tracker-utils' import type { UseCacheTrackerKey } from './webpack/plugins/telemetry-plugin/use-cache-tracker-utils'
import { import {
buildInversePrefetchSegmentDataRoute,
buildPrefetchSegmentDataRoute, buildPrefetchSegmentDataRoute,
type PrefetchSegmentDataRoute, type PrefetchSegmentDataRoute,
} from '../server/lib/router-utils/build-prefetch-segment-data-route' } from '../server/lib/router-utils/build-prefetch-segment-data-route'
...@@ -2175,6 +2176,7 @@ export default async function build( ...@@ -2175,6 +2176,7 @@ export default async function build(
} }
pageInfos.set(page, { pageInfos.set(page, {
originalAppPath,
size, size,
totalSize, totalSize,
isStatic, isStatic,
...@@ -3074,11 +3076,9 @@ export default async function build( ...@@ -3074,11 +3076,9 @@ export default async function build(
dynamicRoute.prefetchSegmentDataRoutes ??= [] dynamicRoute.prefetchSegmentDataRoutes ??= []
for (const segmentPath of metadata.segmentPaths) { for (const segmentPath of metadata.segmentPaths) {
const result = buildPrefetchSegmentDataRoute( dynamicRoute.prefetchSegmentDataRoutes.push(
route.pathname, buildPrefetchSegmentDataRoute(route.pathname, segmentPath)
segmentPath
) )
dynamicRoute.prefetchSegmentDataRoutes.push(result)
} }
} }
...@@ -3497,6 +3497,47 @@ export default async function build( ...@@ -3497,6 +3497,47 @@ export default async function build(
// remove temporary export folder // remove temporary export folder
await fs.rm(outdir, { recursive: true, force: true }) await fs.rm(outdir, { recursive: true, force: true })
await writeManifest(pagesManifestPath, pagesManifest) await writeManifest(pagesManifestPath, pagesManifest)
if (config.experimental.clientSegmentCache) {
for (const dynamicRoute of routesManifest.dynamicRoutes) {
// We only want to handle pages that are using the app router. We
// need this path in order to determine if it's an app route or an
// app page.
const originalAppPath = pageInfos.get(
dynamicRoute.page
)?.originalAppPath
if (!originalAppPath) {
continue
}
// We only want to handle app pages, not app routes.
if (isAppRouteRoute(originalAppPath)) {
continue
}
// We don't need to add the prefetch segment data routes if it was
// added due to a page that was already generated. This would have
// happened if the page was static or partially static.
if (dynamicRoute.prefetchSegmentDataRoutes) {
continue
}
// If the segment paths aren't defined, we need to insert a
// reverse routing rule so that there isn't any conflicts
// with other dynamic routes for the prefetch segment
// routes. This is only an issue for pages that do not have
// partial prerendering enabled.
dynamicRoute.prefetchSegmentDataRoutes = [
buildInversePrefetchSegmentDataRoute(
dynamicRoute.page,
// We use the special segment path of `/_tree` because it's
// the first one sent by the client router so it's the only
// one we need to rewrite to the regular prefetch RSC route.
'/_tree'
),
]
}
}
}) })
// We need to write the manifest with rewrites after build as it might // We need to write the manifest with rewrites after build as it might
......
...@@ -338,6 +338,7 @@ const filterAndSortList = ( ...@@ -338,6 +338,7 @@ const filterAndSortList = (
} }
export interface PageInfo { export interface PageInfo {
originalAppPath: string | undefined
isHybridAmp?: boolean isHybridAmp?: boolean
size: number size: number
totalSize: number totalSize: number
......
import { buildPrefetchSegmentDataRoute } from './build-prefetch-segment-data-route' import {
buildInversePrefetchSegmentDataRoute,
buildPrefetchSegmentDataRoute,
} from './build-prefetch-segment-data-route'
describe('buildPrefetchSegmentDataRoute', () => { describe('buildPrefetchSegmentDataRoute', () => {
it('should build a prefetch segment data route', () => { it('should build a prefetch segment data route', () => {
...@@ -10,8 +13,30 @@ describe('buildPrefetchSegmentDataRoute', () => { ...@@ -10,8 +13,30 @@ describe('buildPrefetchSegmentDataRoute', () => {
expect(route).toMatchInlineSnapshot(` expect(route).toMatchInlineSnapshot(`
{ {
"destination": "/blog/[...slug].segments/$c$slug$[slug]/__PAGE__.segment.rsc", "destination": "/blog/[...slug].segments/$c$slug$[slug]/__PAGE__.segment.rsc",
"routeKeys": {
"nxtPslug": "nxtPslug",
},
"source": "^/blog/(?<nxtPslug>.+?)\\.segments/\\$c\\$slug\\$\\k<nxtPslug>/__PAGE__\\.segment\\.rsc$", "source": "^/blog/(?<nxtPslug>.+?)\\.segments/\\$c\\$slug\\$\\k<nxtPslug>/__PAGE__\\.segment\\.rsc$",
} }
`) `)
}) })
}) })
describe('buildInversePrefetchSegmentDataRoute', () => {
it('should build an inverted prefetch segment data route', () => {
const route = buildInversePrefetchSegmentDataRoute(
'/blog/[...slug]',
'/_tree'
)
expect(route).toMatchInlineSnapshot(`
{
"destination": "/blog/[...slug].prefetch.rsc",
"routeKeys": {
"nxtPslug": "nxtPslug",
},
"source": "^/blog/(?<nxtPslug>.+?)\\.segments/_tree\\.segment\\.rsc$",
}
`)
})
})
...@@ -2,6 +2,7 @@ import path from '../../../shared/lib/isomorphic/path' ...@@ -2,6 +2,7 @@ import path from '../../../shared/lib/isomorphic/path'
import { normalizePagePath } from '../../../shared/lib/page-path/normalize-page-path' import { normalizePagePath } from '../../../shared/lib/page-path/normalize-page-path'
import { getNamedRouteRegex } from '../../../shared/lib/router/utils/route-regex' import { getNamedRouteRegex } from '../../../shared/lib/router/utils/route-regex'
import { import {
RSC_PREFETCH_SUFFIX,
RSC_SEGMENT_SUFFIX, RSC_SEGMENT_SUFFIX,
RSC_SEGMENTS_DIR_SUFFIX, RSC_SEGMENTS_DIR_SUFFIX,
} from '../../../lib/constants' } from '../../../lib/constants'
...@@ -11,6 +12,7 @@ export const SEGMENT_PATH_KEY = 'nextSegmentPath' ...@@ -11,6 +12,7 @@ export const SEGMENT_PATH_KEY = 'nextSegmentPath'
export type PrefetchSegmentDataRoute = { export type PrefetchSegmentDataRoute = {
source: string source: string
destination: string destination: string
routeKeys: { [key: string]: string }
} }
export function buildPrefetchSegmentDataRoute( export function buildPrefetchSegmentDataRoute(
...@@ -24,7 +26,7 @@ export function buildPrefetchSegmentDataRoute( ...@@ -24,7 +26,7 @@ export function buildPrefetchSegmentDataRoute(
`${segmentPath}${RSC_SEGMENT_SUFFIX}` `${segmentPath}${RSC_SEGMENT_SUFFIX}`
) )
const { namedRegex } = getNamedRouteRegex(destination, { const { namedRegex, routeKeys } = getNamedRouteRegex(destination, {
prefixRouteKeys: true, prefixRouteKeys: true,
includePrefix: true, includePrefix: true,
includeSuffix: true, includeSuffix: true,
...@@ -35,5 +37,43 @@ export function buildPrefetchSegmentDataRoute( ...@@ -35,5 +37,43 @@ export function buildPrefetchSegmentDataRoute(
return { return {
destination, destination,
source: namedRegex, source: namedRegex,
routeKeys,
}
}
/**
* Builds a prefetch segment data route that is inverted. This means that it's
* supposed to rewrite from the previous segment paths route back to the
* prefetch RSC route.
*
* @param page - The page to build the route for.
* @param segmentPath - The segment path to build the route for.
* @returns The prefetch segment data route.
*/
export function buildInversePrefetchSegmentDataRoute(
page: string,
segmentPath: string
): PrefetchSegmentDataRoute {
const pagePath = normalizePagePath(page)
const source = path.posix.join(
`${pagePath}${RSC_SEGMENTS_DIR_SUFFIX}`,
`${segmentPath}${RSC_SEGMENT_SUFFIX}`
)
const { namedRegex, routeKeys } = getNamedRouteRegex(source, {
prefixRouteKeys: true,
includePrefix: true,
includeSuffix: true,
excludeOptionalTrailingSlash: true,
backreferenceDuplicateKeys: true,
})
const destination = path.posix.join(`${pagePath}${RSC_PREFETCH_SUFFIX}`)
return {
source: namedRegex,
destination,
routeKeys,
} }
} }
import { connection } from 'next/server'
export default async function Page() {
await connection()
return <div>/[lang]/[teamSlug]/[projectSlug]/monitoring</div>
}
export const experimental_ppr = true
import { connection } from 'next/server'
export default async function Page() {
await connection()
return <div>/[lang]/[teamSlug]/~/monitoring</div>
}
export default function RootLayout({ children, params }) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
export function generateStaticParams() {
return ['en', 'fr'].map((lang) => ({ lang }))
}
export default function Loading() {
return <div>Loading...</div>
}
import Link from 'next/link'
export default function Page() {
return (
<div>
<Link href="/es/~/monitoring">Monitoring</Link>
</div>
)
}
import { nextTestSetup } from 'e2e-utils'
describe('conflicting routes', () => {
const { next, isNextDev, isNextDeploy } = nextTestSetup({
files: __dirname,
})
if (isNextDev) {
test('prefetching is disabled', () => {})
return
}
it.each([
'/en/vercel/~/monitoring',
'/fr/vercel/~/monitoring',
'/es/vercel/~/monitoring',
])('%s matches the right route', async (path) => {
const res = await next.fetch(path, {
headers: {
RSC: '1',
'Next-Router-Prefetch': '1',
'Next-Router-Segment-Prefetch': '/_tree',
},
})
expect(res.status).toBe(200)
if (isNextDeploy) {
expect(res.headers.get('x-matched-path')).toBe(
'/[lang]/[teamSlug]/~/monitoring.prefetch.rsc'
)
}
})
})
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
experimental: {
ppr: 'incremental',
clientSegmentCache: true,
},
}
module.exports = nextConfig
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment