Unverified Commit cf0f090f authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

disable static generation on interception routes (#61004)

### What & Why?
Interception routes depend on contextual information that are provided
via request headers. Specifically it needs to know about the
`Next-Router-State-Tree` when generating the interception route RSC
data, which isn't available at build time. This doesn't currently cause
any usage issues, but it erroneously emits static files & RSC payloads
that the client router won't be able to use and will instead fallback to
a dynamic request.

I removed some special case in an existing test since this fix also
resolves a discrepancy in behavior when PPR is turned on

### How?
This excludes interception routes from `appStaticPaths` at builds which
currently determines which pages should be statically generated.

Closes NEXT-2190
parent 2e977f16
...@@ -160,6 +160,7 @@ import { hasCustomExportOutput } from '../export/utils' ...@@ -160,6 +160,7 @@ import { hasCustomExportOutput } from '../export/utils'
import { interopDefault } from '../lib/interop-default' import { interopDefault } from '../lib/interop-default'
import { formatDynamicImportPath } from '../lib/format-dynamic-import-path' import { formatDynamicImportPath } from '../lib/format-dynamic-import-path'
import { isDefaultRoute } from '../lib/is-default-route' import { isDefaultRoute } from '../lib/is-default-route'
import { isInterceptionRouteAppPath } from '../server/future/helpers/interception-routes'
interface ExperimentalBypassForInfo { interface ExperimentalBypassForInfo {
experimentalBypassFor?: RouteHas[] experimentalBypassFor?: RouteHas[]
...@@ -1857,7 +1858,7 @@ export default async function build( ...@@ -1857,7 +1858,7 @@ export default async function build(
) )
} else { } else {
// If this route can be partially pre-rendered, then // If this route can be partially pre-rendered, then
// mark it as such and mark it that it can be // mark it as such and mark that it can be
// generated server-side. // generated server-side.
if (workerResult.isPPR) { if (workerResult.isPPR) {
isPPR = workerResult.isPPR isPPR = workerResult.isPPR
...@@ -1885,6 +1886,8 @@ export default async function build( ...@@ -1885,6 +1886,8 @@ export default async function build(
} }
const appConfig = workerResult.appConfig || {} const appConfig = workerResult.appConfig || {}
const isInterceptionRoute =
isInterceptionRouteAppPath(page)
if (appConfig.revalidate !== 0) { if (appConfig.revalidate !== 0) {
const isDynamic = isDynamicRoute(page) const isDynamic = isDynamicRoute(page)
const hasGenerateStaticParams = const hasGenerateStaticParams =
...@@ -1900,26 +1903,29 @@ export default async function build( ...@@ -1900,26 +1903,29 @@ export default async function build(
) )
} }
if ( // Mark the app as static if:
// Mark the app as static if: // - It's not an interception route (these currently depend on request headers and cannot be computed at build)
// - It has no dynamic param // - It has no dynamic param
// - It doesn't have generateStaticParams but `dynamic` is set to // - It doesn't have generateStaticParams but `dynamic` is set to
// `error` or `force-static` // `error` or `force-static`
!isDynamic if (!isInterceptionRoute) {
) { if (!isDynamic) {
appStaticPaths.set(originalAppPath, [page]) appStaticPaths.set(originalAppPath, [page])
appStaticPathsEncoded.set(originalAppPath, [page]) appStaticPathsEncoded.set(originalAppPath, [
isStatic = true page,
} else if ( ])
isDynamic && isStatic = true
!hasGenerateStaticParams && } else if (
(appConfig.dynamic === 'error' || isDynamic &&
appConfig.dynamic === 'force-static') !hasGenerateStaticParams &&
) { (appConfig.dynamic === 'error' ||
appStaticPaths.set(originalAppPath, []) appConfig.dynamic === 'force-static')
appStaticPathsEncoded.set(originalAppPath, []) ) {
isStatic = true appStaticPaths.set(originalAppPath, [])
isPPR = false appStaticPathsEncoded.set(originalAppPath, [])
isStatic = true
isPPR = false
}
} }
} }
...@@ -1936,7 +1942,8 @@ export default async function build( ...@@ -1936,7 +1942,8 @@ export default async function build(
!isStatic && !isStatic &&
!isAppRouteRoute(originalAppPath) && !isAppRouteRoute(originalAppPath) &&
!isDynamicRoute(originalAppPath) && !isDynamicRoute(originalAppPath) &&
!isPPR !isPPR &&
!isInterceptionRoute
) { ) {
appPrefetchPaths.set(originalAppPath, page) appPrefetchPaths.set(originalAppPath, page)
} }
......
...@@ -83,6 +83,7 @@ import { isAppRouteRouteModule } from '../server/future/route-modules/checks' ...@@ -83,6 +83,7 @@ import { isAppRouteRouteModule } from '../server/future/route-modules/checks'
import { interopDefault } from '../lib/interop-default' import { interopDefault } from '../lib/interop-default'
import type { PageExtensions } from './page-extensions-type' import type { PageExtensions } from './page-extensions-type'
import { formatDynamicImportPath } from '../lib/format-dynamic-import-path' import { formatDynamicImportPath } from '../lib/format-dynamic-import-path'
import { isInterceptionRouteAppPath } from '../server/future/helpers/interception-routes'
export type ROUTER_TYPE = 'pages' | 'app' export type ROUTER_TYPE = 'pages' | 'app'
...@@ -1744,7 +1745,7 @@ export async function isPageStatic({ ...@@ -1744,7 +1745,7 @@ export async function isPageStatic({
isStatic = true isStatic = true
} }
// When PPR is enabled, any route may contain or be completely static, so // When PPR is enabled, any route may be completely static, so
// mark this route as static. // mark this route as static.
let isPPR = false let isPPR = false
if (ppr && routeModule.definition.kind === RouteKind.APP_PAGE) { if (ppr && routeModule.definition.kind === RouteKind.APP_PAGE) {
...@@ -1752,6 +1753,12 @@ export async function isPageStatic({ ...@@ -1752,6 +1753,12 @@ export async function isPageStatic({
isStatic = true isStatic = true
} }
// interception routes depend on `Next-URL` and `Next-Router-State-Tree` request headers and thus cannot be prerendered
if (isInterceptionRouteAppPath(page)) {
isStatic = false
isPPR = false
}
return { return {
isStatic, isStatic,
isPPR, isPPR,
......
...@@ -127,27 +127,12 @@ createNextDescribe( ...@@ -127,27 +127,12 @@ createNextDescribe(
app: new FileRef(path.join(__dirname, 'app')), app: new FileRef(path.join(__dirname, 'app')),
}, },
}, },
({ next, isNextStart }) => { ({ next }) => {
if (process.env.__NEXT_EXPERIMENTAL_PPR === 'true' && isNextStart) { it('should use the default fallback (a 404) if there is no custom default page', async () => {
// The PPR prefetch will 404 since it'll request the full page (which won't exist, since the intercepted route const browser = await next.browser('/')
// has no default). The default router behavior if a prefetch fails is to trigger an MPA navigation
it('should render the non-intercepted page on navigation', async () => { await browser.elementByCss('[href="/photos/1"]').click()
const browser = await next.browser('/') await check(() => browser.elementByCss('body').text(), /404/)
})
await browser.elementByCss('[href="/photos/1"]').click()
await check(() => browser.elementById('slot').text(), /@slot default/)
await check(
() => browser.elementById('children').text(),
/Photo Page \(non-intercepted\) 1/
)
})
} else {
it('should use the default fallback (a 404) if there is no custom default page', async () => {
const browser = await next.browser('/')
await browser.elementByCss('[href="/photos/1"]').click()
await check(() => browser.elementByCss('body').text(), /404/)
})
}
} }
) )
export default function Page() {
return <div>Interception Modal</div>
}
export default function Layout(props) {
return (
<>
<div>{props.children}</div>
<div>{props.modal}</div>
</>
)
}
export default function Page() {
return <div>Modal Page</div>
}
import Link from 'next/link'
export default function Page() {
return (
<div>
<Link href="/baz/modal">Open Interception Modal</Link>
</div>
)
}
import { createNextDescribe } from 'e2e-utils' import { createNextDescribe } from 'e2e-utils'
import { check } from 'next-test-utils' import { check } from 'next-test-utils'
import { Response } from 'playwright-chromium'
createNextDescribe( createNextDescribe(
'interception-route-prefetch-cache', 'interception-route-prefetch-cache',
{ {
files: __dirname, files: __dirname,
}, },
({ next }) => { ({ next, isNextStart }) => {
it('should render the correct interception when two distinct layouts share the same path structure', async () => { it('should render the correct interception when two distinct layouts share the same path structure', async () => {
const browser = await next.browser('/') const browser = await next.browser('/')
...@@ -41,5 +42,41 @@ createNextDescribe( ...@@ -41,5 +42,41 @@ createNextDescribe(
/Intercepted on Bar Page/ /Intercepted on Bar Page/
) )
}) })
if (isNextStart) {
it('should not be a cache HIT when prefetching an interception route', async () => {
const responses: { cacheStatus: string; pathname: string }[] = []
const browser = await next.browser('/baz', {
beforePageLoad(page) {
page.on('response', (response: Response) => {
const url = new URL(response.url())
const request = response.request()
const responseHeaders = response.headers()
const requestHeaders = request.headers()
if (requestHeaders['next-router-prefetch']) {
responses.push({
cacheStatus: responseHeaders['x-nextjs-cache'],
pathname: url.pathname,
})
}
})
},
})
expect(await browser.elementByCss('body').text()).toContain(
'Open Interception Modal'
)
const interceptionPrefetchResponse = responses.find(
(response) => response.pathname === '/baz/modal'
)
const homePrefetchResponse = responses.find(
(response) => response.pathname === '/'
)
expect(homePrefetchResponse.cacheStatus).toBe('HIT') // sanity check to ensure we're seeing cache statuses
expect(interceptionPrefetchResponse.cacheStatus).toBeUndefined()
})
}
} }
) )
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