Unverified Commit 2682d817 authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

Allow intercepting dynamic routes to be partially prerendered (#80851)

For intercepting dynamic routes we do prerender `.prefetch.rsc`, as well
as `.segment.rsc` files if `clientSegmentCache` is enabled. However, we
omitted adding those routes to the prerender manifest, which resulted in
server errors when looking up the RSC files.

I've added two tests to verify the one-line fix, one in
`test/e2e/app-dir/parallel-routes-and-interception` (relevant when tests
are run with `__NEXT_EXPERIMENTAL_PPR=true`), and one in
`test/e2e/app-dir/segment-cache/basic` (has `dynamicIO` and
`clientSegmentCache` enabled). Both tests are written in the style of
the existing tests in the respective test suites.

---------
Co-authored-by: 's avatarJimmy Lai <laijimmy0@gmail.com>
parent 65fb7439
......@@ -69,7 +69,6 @@ import { normalizeAppPath } from '../shared/lib/router/utils/app-paths'
import { denormalizeAppPagePath } from '../shared/lib/page-path/denormalize-app-path'
import { RouteKind } from '../server/route-kind'
import type { PageExtensions } from './page-extensions-type'
import { isInterceptionRouteAppPath } from '../shared/lib/router/utils/interception-routes'
import { checkIsRoutePPREnabled } from '../server/lib/experimental/ppr'
import type { FallbackMode } from '../lib/fallback'
import type { OutgoingHttpHeaders } from 'http'
......@@ -1175,7 +1174,6 @@ export async function isPageStatic({
// in incremental mode.
isRoutePPREnabled =
routeModule.definition.kind === RouteKind.APP_PAGE &&
!isInterceptionRouteAppPath(page) &&
checkIsRoutePPREnabled(pprConfig, appConfig)
// If force dynamic was set and we don't have PPR enabled, then set the
......
export default async function Page({ params }) {
const { id } = await params
return <p id={`photo-intercepted-${id}`}>Photo INTERCEPTED {id}</p>
}
export function generateStaticParams() {
return Array.from({ length: 3 }).map((_, i) => ({
id: i.toString(),
}))
}
export default async function Page({ params }) {
const { id } = await params
return <p id={`photo-page-${id}`}>Photo PAGE {id}</p>
}
export function generateStaticParams() {
return Array.from({ length: 3 }).map((_, i) => ({
id: i.toString(),
}))
}
import Link from 'next/link'
export default function Page() {
return (
<>
<h1 id="photos-page">Photos</h1>
<ul>
{Array.from({ length: 3 }).map((_, i) => (
<li key={i}>
<Link href={`/intercepting-routes-dynamic-prerendered/photos/${i}`}>
Link {i}
</Link>
</li>
))}
</ul>
</>
)
}
......@@ -487,6 +487,49 @@ describe('parallel-routes-and-interception', () => {
})
})
describe('route intercepting with prerendered dynamic routes ', () => {
it('should render intercepted route', async () => {
const browser = await next.browser(
'/intercepting-routes-dynamic-prerendered/photos'
)
// Check if navigation to modal route works.
await browser
.elementByCss(
'[href="/intercepting-routes-dynamic-prerendered/photos/1"]'
)
.click()
// This should load the intercepted page.
await retry(async () => {
expect(
await browser.waitForElementByCss('#photo-intercepted-1').text()
).toBe('Photo INTERCEPTED 1')
})
// Check if url matches even though it was intercepted.
expect(await browser.url()).toBe(
next.url + '/intercepting-routes-dynamic-prerendered/photos/1'
)
// There must not be any errors from prefetching the intercepted page.
expect(
(await browser.log()).filter(({ source }) => source === 'error')
).toEqual([])
// Trigger a refresh, this should load the normal page, not the modal.
await browser.refresh()
expect(await browser.waitForElementByCss('#photo-page-1').text()).toBe(
'Photo PAGE 1'
)
// Check if the url matches still.
expect(await browser.url()).toBe(
next.url + '/intercepting-routes-dynamic-prerendered/photos/1'
)
})
})
describe('route intercepting with dynamic optional catch-all routes', () => {
it('should render intercepted route', async () => {
const browser = await next.browser(
......
export default async function InterceptedPhotoPage({
params,
}: {
params: Promise<{ id: string }>
}) {
const { id } = await params
return (
<div id="intercepted-photo-page">
Intercepted photo page for id {JSON.stringify(id)}
</div>
)
}
export function generateStaticParams() {
return [{ id: '1' }]
}
export default function FeedLayout({ children }) {
return (
<div>
Feed layout
<div>{children}</div>
</div>
)
}
import { LinkAccordion } from '../../../components/link-accordion'
export default function FeedPage() {
return (
<LinkAccordion href="/interception-with-params/photo/1">
Go to photo
</LinkAccordion>
)
}
export default async function PhotoPage({
params,
}: {
params: Promise<{ id: string }>
}) {
const { id } = await params
return `Photo page for id ${JSON.stringify(id)} (normal, not intercepted)`
}
export function generateStaticParams() {
return [{ id: '1' }]
}
......@@ -162,6 +162,38 @@ describe('segment cache (basic tests)', () => {
)
})
it('prefetch interception route with params', async () => {
let act: ReturnType<typeof createRouterAct>
const browser = await next.browser('/interception-with-params/feed', {
beforePageLoad(page) {
act = createRouterAct(page)
},
})
// Reveal the link to trigger a prefetch.
const reveal = await browser.elementByCss('input[type="checkbox"]')
const link = await act(
async () => {
await reveal.click()
return await browser.elementByCss('a')
},
{ includes: 'intercepted-photo-page' }
)
// Navigate to the test page
await act(
async () => {
await link.click()
// The page should render immediately because it was prefetched
const div = await browser.elementById('intercepted-photo-page')
expect(await div.innerHTML()).toBe('Intercepted photo page for id "1"')
},
// No additional requests were required, because everything was prefetched
'no-requests'
)
})
it('skips dynamic request if prefetched data is fully static', async () => {
let act: ReturnType<typeof createRouterAct>
const browser = await next.browser('/fully-static', {
......
......@@ -275,6 +275,19 @@ ${fulfilled.body}
throw error
}
if (fulfilled.status >= 400) {
error.message = `
Received a response with an error status code.
Status: ${fulfilled.status}
URL: ${request.url()}
Headers: ${JSON.stringify(fulfilled.headers)}
Response:
${fulfilled.body}
`
throw error
}
if (expectedResponses !== null) {
let alreadyMatchedByThisResponse: string | null = null
for (const expectedResponse of expectedResponses) {
......
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