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 #
-->