Unverified Commit 238a8ac8 authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

Revert "Partial Fallback Prerendering Route Shells (#69282)" (#79258)

parent 21e44116
......@@ -168,7 +168,6 @@ import { RouteKind } from './route-kind'
import type { RouteModule } from './route-modules/route-module'
import { FallbackMode, parseFallbackField } from '../lib/fallback'
import { toResponseCacheEntry } from './response-cache/utils'
import { scheduleOnNextTick } from '../lib/scheduler'
import { SegmentPrefixRSCPathnameNormalizer } from './normalizers/request/segment-prefix-rsc'
import {
shouldServeStreamingMetadata,
......@@ -2674,7 +2673,6 @@ export default abstract class Server<
headers,
},
cacheControl: { revalidate, expire },
isFallback: false,
}
return cacheEntry
......@@ -2881,7 +2879,6 @@ export default abstract class Server<
return {
value: null,
cacheControl,
isFallback: false,
} satisfies ResponseCacheEntry
}
......@@ -2893,7 +2890,6 @@ export default abstract class Server<
props: metadata.pageData ?? metadata.flightData,
} satisfies CachedRedirectValue,
cacheControl,
isFallback: false,
} satisfies ResponseCacheEntry
}
......@@ -2915,7 +2911,6 @@ export default abstract class Server<
segmentData: metadata.segmentData,
} satisfies CachedAppPageValue,
cacheControl,
isFallback: !!fallbackRouteParams,
} satisfies ResponseCacheEntry
}
......@@ -2928,7 +2923,6 @@ export default abstract class Server<
status: isAppPath ? res.statusCode : undefined,
} satisfies CachedPageValue,
cacheControl,
isFallback: pagesFallback,
}
}
......@@ -3155,7 +3149,6 @@ export default abstract class Server<
) {
return {
cacheControl: { revalidate: 1, expire: undefined },
isFallback: false,
value: {
kind: CachedRouteKind.PAGES,
html: RenderResult.fromStatic(''),
......@@ -3220,46 +3213,6 @@ export default abstract class Server<
return null
}
// If we're not in minimal mode and the cache entry that was returned was a
// app page fallback, then we need to kick off the dynamic shell generation.
if (
ssgCacheKey &&
!this.minimalMode &&
isRoutePPREnabled &&
cacheEntry.value?.kind === CachedRouteKind.APP_PAGE &&
cacheEntry.isFallback &&
!isOnDemandRevalidate &&
// When we're debugging the fallback shell, we don't want to regenerate
// the route shell.
!isDebugFallbackShell &&
process.env.DISABLE_ROUTE_SHELL_GENERATION !== 'true'
) {
scheduleOnNextTick(async () => {
try {
await this.responseCache.get(
ssgCacheKey,
() =>
doRender({
// We're an on-demand request, so we don't need to pass in the
// fallbackRouteParams.
fallbackRouteParams: null,
pagesFallback: undefined,
postponed: undefined,
}),
{
routeKind: RouteKind.APP_PAGE,
incrementalCache,
isOnDemandRevalidate: true,
isPrefetch: false,
isRoutePPREnabled: true,
}
)
} catch (err) {
console.error('Error occurred while rendering dynamic shell', err)
}
})
}
const didPostpone =
cacheEntry.value?.kind === CachedRouteKind.APP_PAGE &&
typeof cacheEntry.value.postponed === 'string'
......
......@@ -418,7 +418,6 @@ export class ImageOptimizerCache {
Date.now(),
cacheControl: { revalidate: maxAge, expire: undefined },
isStale: now > expireAt,
isFallback: false,
}
}
} catch (_) {
......
......@@ -477,7 +477,6 @@ export class IncrementalCache implements IncrementalCacheType {
}
let entry: IncrementalResponseCacheEntry | null = null
const { isFallback } = ctx
const cacheControl = this.cacheControls.get(toRoute(cacheKey))
let isStale: boolean | -1 | undefined
......@@ -506,7 +505,6 @@ export class IncrementalCache implements IncrementalCacheType {
cacheControl,
revalidateAfter,
value: cacheData.value,
isFallback,
}
}
......@@ -524,7 +522,6 @@ export class IncrementalCache implements IncrementalCacheType {
value: null,
cacheControl,
revalidateAfter,
isFallback,
}
this.set(cacheKey, entry.value, { ...ctx, cacheControl })
}
......
......@@ -1049,7 +1049,6 @@ export default class NextNodeServer extends BaseServer<
extension: getExtension(contentType) as string,
upstreamEtag,
},
isFallback: false,
cacheControl: { revalidate: maxAge, expire: undefined },
}
},
......
......@@ -142,7 +142,6 @@ export interface IncrementalResponseCacheEntry {
*/
isStale?: boolean | -1
isMiss?: boolean
isFallback: boolean | undefined
value: Exclude<IncrementalCacheValue, CachedFetchValue> | null
}
......@@ -178,7 +177,6 @@ export type ResponseCacheEntry = {
value: ResponseCacheValue | null
isStale?: boolean | -1
isMiss?: boolean
isFallback: boolean | undefined
}
/**
......
......@@ -47,7 +47,6 @@ export async function toResponseCacheEntry(
isMiss: response.isMiss,
isStale: response.isStale,
cacheControl: response.cacheControl,
isFallback: response.isFallback,
value:
response.value?.kind === CachedRouteKind.PAGES
? ({
......
......@@ -2818,9 +2818,7 @@ describe('app-dir static/dynamic handling', () => {
for (let i = 0; i < 5; i++) {
const res = await next.fetch('/articles/non-existent')
// Only the first request should be a 200 while using PPR. When the route
// shell is generated, the status code will switch to 404.
if (process.env.__NEXT_EXPERIMENTAL_PPR && !isNextDev && i === 0) {
if (process.env.__NEXT_EXPERIMENTAL_PPR && !isNextDev) {
expect(res.status).toBe(200)
} else {
expect(res.status).toBe(404)
......@@ -3789,34 +3787,20 @@ describe('app-dir static/dynamic handling', () => {
expect(res.status).toBe(200)
const html = await res.text()
let $ = cheerio.load(html)
const $ = cheerio.load(html)
expect(JSON.parse($('#params').text())).toEqual({ slug: 'random' })
expect(JSON.parse($('#headers').text())).toEqual([])
expect(JSON.parse($('#cookies').text())).toEqual([])
const firstTime = $('#now').text()
if (!(global as any).isNextDev) {
if (process.env.__NEXT_EXPERIMENTAL_PPR) {
// When PPR is enabled, we first encounter the fallback shell. We do
// expect though that the page does not change after the dynamic shell
// has been finalized.
await retry(async () => {
$ = await next.render$('/force-static/random')
const first = $('#now').text()
$ = await next.render$('/force-static/random')
// The page should not change after the dynamic shell has been
// finalized.
expect($('#now').text()).toBe(first)
})
} else {
const first = $('#now').text()
$ = await next.render$('/force-static/random')
expect($('#now').text()).toBe(first)
}
const res2 = await next.fetch('/force-static/random')
expect(res2.status).toBe(200)
const $2 = cheerio.load(await res2.text())
expect(firstTime).toBe($2('#now').text())
}
})
}
......
......@@ -5,7 +5,11 @@ export const revalidate = 3600 // arbitrarily long, just so that it doesn't happ
export const dynamicParams = true
export async function generateStaticParams() {
return []
return ['nodejs', 'edge'].flatMap((runtime) =>
['dynamic-page', 'middleware', 'route', 'server-action'].map((page) => ({
key: `/${runtime}/${page}`,
}))
)
}
export default async function Page({ params }) {
......
......@@ -353,176 +353,58 @@ describe('ppr-full', () => {
expect($('[data-agent]').closest('[hidden]').length).toBe(1)
})
if (isNextDeploy) {
it('should render the fallback shell every time', async () => {
const random = Math.random().toString(16).slice(2)
const pathname = `/fallback/dynamic/params/on-second-visit-${random}`
let $ = await next.render$(pathname)
expect($('[data-slug]').closest('[hidden]').length).toBe(1)
expect($('[data-agent]').closest('[hidden]').length).toBe(1)
for (let i = 0; i < 10; i++) {
$ = await next.render$(pathname)
expect($('[data-slug]').closest('[hidden]').length).toBe(1)
expect($('[data-agent]').closest('[hidden]').length).toBe(1)
}
})
it('should render the fallback shell even if the page is static', async () => {
const random = Math.random().toString(16).slice(2)
const pathname = `/fallback/params/on-second-visit-${random}`
// Expect that the slug had to be resumed.
let $ = await next.render$(pathname)
expect($('[data-slug]').closest('[hidden]').length).toBe(1)
for (let i = 0; i < 10; i++) {
$ = await next.render$(pathname)
expect($('[data-slug]').closest('[hidden]').length).toBe(1)
}
})
it('will not revalidate the fallback shell', async () => {
const random = Math.random().toString(16).slice(2)
const pathname = `/fallback/dynamic/params/revalidate-${random}`
let $ = await next.render$(pathname)
const fallbackID = $('[data-layout]').data('layout') as string
it('should render the fallback shell every time', async () => {
const random = Math.random().toString(16).slice(2)
const pathname = `/fallback/dynamic/params/on-second-visit-${random}`
// Now let's revalidate the page.
await next.fetch(
`/api/revalidate?pathname=${encodeURIComponent(pathname)}`
)
let $ = await next.render$(pathname)
expect($('[data-slug]').closest('[hidden]').length).toBe(1)
expect($('[data-agent]').closest('[hidden]').length).toBe(1)
// We expect to get the fallback shell again.
for (let i = 0; i < 10; i++) {
$ = await next.render$(pathname)
expect($('[data-layout]').data('layout')).toBe(fallbackID)
// Let's wait for the page to be revalidated.
await retry(async () => {
$ = await next.render$(pathname)
const newDynamicID = $('[data-layout]').data('layout') as string
expect(newDynamicID).toBe(fallbackID)
})
})
} else {
it('should render the route shell on the second visit', async () => {
const random = Math.random().toString(16).slice(2)
const pathname = `/fallback/dynamic/params/on-second-visit-${random}`
let $ = await next.render$(pathname)
expect($('[data-slug]').closest('[hidden]').length).toBe(1)
expect($('[data-agent]').closest('[hidden]').length).toBe(1)
}
})
await retry(async () => {
$ = await next.render$(pathname)
expect($('[data-slug]').closest('[hidden]').length).toBe(0)
expect($('[data-agent]').closest('[hidden]').length).toBe(1)
})
})
it('should render the dynamic shell as static if the page is static', async () => {
const random = Math.random().toString(16).slice(2)
const pathname = `/fallback/params/on-second-visit-${random}`
// Expect that the slug had to be resumed.
let $ = await next.render$(pathname)
expect($('[data-slug]').closest('[hidden]').length).toBe(1)
// The slug didn't have to be resumed, and it should all be static.
await retry(async () => {
$ = await next.render$(pathname)
expect($('[data-slug]').closest('[hidden]').length).toBe(0)
const {
timings: { streamFirstChunk, start, streamEnd },
chunks,
} = await measurePPRTimings(async () => {
const res = await next.fetch(pathname)
expect(res.status).toBe(200)
expect(res.headers.get('x-nextjs-cache')).toBe('HIT')
return res.body
}, 1000)
expect(chunks.dynamic).toBe('')
expect(streamFirstChunk - start).toBeLessThan(500)
expect(streamEnd - start).toBeLessThan(500)
})
})
it('will only revalidate the page', async () => {
const random = Math.random().toString(16).slice(2)
const pathname = `/fallback/dynamic/params/revalidate-${random}`
let $ = await next.render$(pathname)
const fallbackID = $('[data-layout]').data('layout') as string
let dynamicID: string
await retry(async () => {
$ = await next.render$(pathname)
dynamicID = $('[data-layout]').data('layout') as string
// These should be different,
expect(dynamicID).not.toBe(fallbackID)
})
it('should render the fallback shell even if the page is static', async () => {
const random = Math.random().toString(16).slice(2)
const pathname = `/fallback/params/on-second-visit-${random}`
// Now let's revalidate the page.
await next.fetch(
`/api/revalidate?pathname=${encodeURIComponent(pathname)}`
)
// Expect that the slug had to be resumed.
let $ = await next.render$(pathname)
expect($('[data-slug]').closest('[hidden]').length).toBe(1)
// We expect to get the fallback shell again.
for (let i = 0; i < 10; i++) {
$ = await next.render$(pathname)
expect($('[data-layout]').data('layout')).toBe(fallbackID)
// Let's wait for the page to be revalidated.
await retry(async () => {
$ = await next.render$(pathname)
const newDynamicID = $('[data-layout]').data('layout') as string
expect(newDynamicID).not.toBe(dynamicID)
})
})
it('will revalidate the page and fallback shell', async () => {
const random = Math.random().toString(16).slice(2)
const pathname = `/fallback/dynamic/params/revalidate-${random}`
expect($('[data-slug]').closest('[hidden]').length).toBe(1)
}
})
let $ = await next.render$(pathname)
const fallbackID = $('[data-layout]').data('layout') as string
it('will not revalidate the fallback shell', async () => {
const random = Math.random().toString(16).slice(2)
const pathname = `/fallback/dynamic/params/revalidate-${random}`
let dynamicID: string
await retry(async () => {
$ = await next.render$(pathname)
dynamicID = $('[data-layout]').data('layout') as string
let $ = await next.render$(pathname)
const fallbackID = $('[data-layout]').data('layout') as string
// These should be different,
expect(dynamicID).not.toBe(fallbackID)
})
// Now let's revalidate the page.
await next.fetch(
`/api/revalidate?pathname=${encodeURIComponent(pathname)}`
)
// Now let's revalidate the page.
await next.fetch(
`/api/revalidate?pathname=${encodeURIComponent(pathname)}`
)
// We expect to get the fallback shell again.
$ = await next.render$(pathname)
expect($('[data-layout]').data('layout')).toBe(fallbackID)
// We expect to get the fallback shell.
// Let's wait for the page to be revalidated.
await retry(async () => {
$ = await next.render$(pathname)
// When deployed to Vercel, it will serve a stale version of the dynamic shell
// Whereas with `next start` it will serve the fallback shell
expect($('[data-layout]').data('layout')).toBe(fallbackID)
// Let's wait for the page to be revalidated.
let revalidatedDynamicID: string
await retry(async () => {
$ = await next.render$(pathname)
revalidatedDynamicID = $('[data-layout]').data('layout') as string
expect(revalidatedDynamicID).not.toBe(dynamicID)
expect(revalidatedDynamicID).not.toBe(fallbackID)
})
const newDynamicID = $('[data-layout]').data('layout') as string
expect(newDynamicID).toBe(fallbackID)
})
}
})
/**
* This test is really here to just to force the the suite to have the expected route
......
......@@ -144,7 +144,7 @@ describe('ppr-incremental', () => {
it.each(pathnames)('%s', async (pathname) => {
const $ = await next.render$(pathname)
expect($('#dynamic')).toHaveLength(1)
expect($('#dynamic').closest('[hidden]')).toHaveLength(0)
expect($('#dynamic').parent('[hidden]')).toHaveLength(0)
})
})
}
......@@ -196,7 +196,7 @@ describe('ppr-incremental', () => {
it.each(pathnames)('%s', async (pathname) => {
const $ = await next.render$(pathname)
expect($('#dynamic')).toHaveLength(1)
expect($('#dynamic').closest('[hidden]')).toHaveLength(1)
expect($('#dynamic').parent('[hidden]')).toHaveLength(1)
})
})
}
......
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