Skip generating fallback shells for routes with undefined root params

Routes with undefined root params always use `BLOCKING_STATIC_RENDER`
(or `NOT_FOUND` when `dynamicParams` is `false`), which means the server
does a full on-demand prerender instead of serving a prebuilt shell. The
fallback shell HTML was therefore dead output that was never served at
runtime.

The previous code already skipped this generation when `cacheComponents`
was disabled, but the `cacheComponents` exception (added in badf4b6c
for `clientParamParsing`) was unnecessary because
`calculateFallbackMode` was never updated to return `PRERENDER` for
these routes. The generated shells also contained incorrect content due
to the Resume Data Cache (RDC) leaking values from a previous specific
shell prerender.

When we want to serve these fallback shells in the future, we will also
need to overhaul how the RDC is shared between specific shell
prerendering and fallback shell prerendering, similar to the cache key
upgrades already done for cache handlers. Cache functions that access
root fallback params would then likely become dynamic holes in the
fallback shell, the same as for normal route fallback params today.
parent 47230b12
...@@ -2909,19 +2909,14 @@ export default async function build( ...@@ -2909,19 +2909,14 @@ export default async function build(
: false : false
routes.forEach((route) => { routes.forEach((route) => {
// If the route has any dynamic root segments, we need to skip // Skip rendering fallback shells for routes with undefined
// rendering the route. This is because we don't support // root params. These routes always use BLOCKING_STATIC_RENDER
// revalidating the shells without the parameters present. // (or NOT_FOUND when dynamicParams is false), so the server
// Note that we only have fallback root params if we also have // does a full on-demand prerender instead of resuming a
// PPR enabled for this route/app already. // prebuilt fallback shell.
if ( if (
route.fallbackRootParams && route.fallbackRootParams &&
route.fallbackRootParams.length > 0 && route.fallbackRootParams.length > 0
// We don't skip rendering the route if we have the
// following enabled. This is because the flight data now
// does not contain any of the route params and is instead
// completely static.
!config.cacheComponents
) { ) {
return return
} }
......
import { nextTestSetup } from 'e2e-utils' import { nextTestSetup } from 'e2e-utils'
import { waitForNoRedbox, retry } from 'next-test-utils' import { waitForNoRedbox, retry } from 'next-test-utils'
import { readdir } from 'fs/promises'
import { join } from 'path' import { join } from 'path'
import { createSandbox } from 'development-sandbox' import { createSandbox } from 'development-sandbox'
import { outdent } from 'outdent' import { outdent } from 'outdent'
import { createRequestTracker } from '../../../lib/e2e-utils/request-tracker' import { createRequestTracker } from '../../../lib/e2e-utils/request-tracker'
const isCacheComponentsEnabled = process.env.__NEXT_CACHE_COMPONENTS === 'true'
describe('app-root-param-getters - simple', () => { describe('app-root-param-getters - simple', () => {
let currentCliOutputIndex = 0 let currentCliOutputIndex = 0
beforeEach(() => { beforeEach(() => {
...@@ -175,6 +178,48 @@ describe('app-root-param-getters - simple', () => { ...@@ -175,6 +178,48 @@ describe('app-root-param-getters - simple', () => {
) )
}) })
if (!isNextDev && !isNextDeploy) {
it('should not generate fallback shells for routes with undefined root params', async () => {
const serverAppDir = join(next.testDir, '.next/server/app')
const allFiles = await readdir(serverAppDir, { recursive: true })
const htmlFiles = allFiles
.filter((f) => f.endsWith('.html'))
.map((f) => f.replace(/\\/g, '/'))
.sort()
// Fallback shells with undefined root params use BLOCKING_STATIC_RENDER
// and are never served, so they should not be generated at build time.
expect(htmlFiles).not.toContainEqual(
expect.stringMatching(/^\[lang\]\/\[locale\]/)
)
expect(htmlFiles).not.toContainEqual(expect.stringContaining('[...path]'))
// Concrete prerendered shells and shells without undefined root params
// should still be generated.
const alwaysExpected = [
'en/us.html',
'en/us/other.html',
'en/us/server-action.html',
'catch-all/foo.html',
'optional-catch-all.html',
]
// With Cache Components enabled we also generate partially static shells.
const cacheComponentsOnly = [
'en/us/other/[slug].html',
'en/us/rerender-after-server-action.html',
]
expect(htmlFiles).toEqual(
expect.arrayContaining(
isCacheComponentsEnabled
? [...alwaysExpected, ...cacheComponentsOnly]
: alwaysExpected
)
)
})
}
// TODO(root-params): add support for route handlers // TODO(root-params): add support for route handlers
it('should error when used in a route handler (until we implement it)', async () => { it('should error when used in a route handler (until we implement it)', async () => {
const params = { lang: 'en', locale: 'us' } const params = { lang: 'en', locale: 'us' }
......
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