Unverified Commit 9f4a7f03 authored by Andrew Clark's avatar Andrew Clark Committed by GitHub

[Fix] Inverse prefetch segment for Pages routes (#78932)

Another follow-up to https://github.com/vercel/next.js/pull/78387 and
https://github.com/vercel/next.js/pull/78568. Refer to those PRs for
full details.

Fixes a routing conflict related to per-segment prefetches in sites that
use both App Router and Pages Router.

The fix is to expand the behavior added in the previous PRs — we output
an "inverse" tree prefetch route for every route in the route manifest
that is not PPR-enabled, including ones defined by Pages Router.

---------
Co-authored-by: 's avatarWyatt Johnson <accounts+github@wyattjoh.ca>
parent 884f5860
...@@ -3557,18 +3557,11 @@ export default async function build( ...@@ -3557,18 +3557,11 @@ export default async function build(
...routesManifest.staticRoutes, ...routesManifest.staticRoutes,
...routesManifest.dynamicRoutes, ...routesManifest.dynamicRoutes,
]) { ]) {
// We only want to handle pages that are using the app router. We // If the segment paths aren't defined, we need to insert a
// need this path in order to determine if it's an app route or an // reverse routing rule so that there isn't any conflicts
// app page. // with other dynamic routes for the prefetch segment
const originalAppPath = pageInfos.get(route.page)?.originalAppPath // routes. This is true for any route that is not PPR-enabled,
if (!originalAppPath) { // including all routes defined by Pages Router.
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 // 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 // added due to a page that was already generated. This would have
...@@ -3577,11 +3570,6 @@ export default async function build( ...@@ -3577,11 +3570,6 @@ export default async function build(
continue 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.
route.prefetchSegmentDataRoutes = [ route.prefetchSegmentDataRoutes = [
buildInversePrefetchSegmentDataRoute( buildInversePrefetchSegmentDataRoute(
route.page, route.page,
......
export default function RootLayout({ children }) {
return (
<html>
<body>{children}</body>
</html>
)
}
export default function Page() {
return '/new/[teamSlug]/page.tsx'
}
...@@ -32,12 +32,6 @@ describe('conflicting routes', () => { ...@@ -32,12 +32,6 @@ describe('conflicting routes', () => {
}) })
it('matches the right route when the original route has no dynamic params, is dynamic, and PPR is disabled', async () => { it('matches the right route when the original route has no dynamic params, is dynamic, and PPR is disabled', async () => {
if (isNextDeploy) {
// TODO: Temporarily disabled until corresponding fix in Vercel builder
// (https://github.com/vercel/vercel/pull/13275) is released.
return
}
const res = await next.fetch('/prefetch-tests/dynamic', { const res = await next.fetch('/prefetch-tests/dynamic', {
headers: { headers: {
RSC: '1', RSC: '1',
...@@ -54,4 +48,31 @@ describe('conflicting routes', () => { ...@@ -54,4 +48,31 @@ describe('conflicting routes', () => {
) )
} }
}) })
it('handles conflict between App Router and Pages Router routes', async () => {
const res = await next.fetch('/new/templates', {
headers: {
RSC: '1',
'Next-Router-Prefetch': '1',
'Next-Router-Segment-Prefetch': '/_tree',
},
})
// Should match the route defined at pages/new/templates/[[...slug]].js,
// not the one at app/new/[teamSlug]/page.tsx
if (isNextDeploy) {
// In a deployed environment the builder routes this to the .prefetch.rsc
// route, which doesn't exist, so it returns a 404.
// TODO: It'd probably be more correct if it didn't re-route to
// .prefetch.rsc and just routed to the normal Pages route. This would
// match the behavior in server mode. This only happens when the
// prefetch header is present, though, so the only observable effect right
// now is that it shows up as a 404 in the network panel. Either way, the
// page can't be prefetched by App Router because it's a Pages route.
expect(res.status).toBe(404)
} else {
expect(res.status).toBe(200)
expect(await res.text()).toContain('/new/templates/[[...slug]].js')
}
})
}) })
export default function App({ Component, pageProps }) {
return <Component {...pageProps} />
}
import { Html, Head, Main, NextScript } from 'next/document'
export default function Document() {
return (
<Html lang="en">
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
export default function Page() {
return '/new/templates/[[...slug]].js'
}
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