Unverified Commit b89463ac authored by Hendrik Liebau's avatar Hendrik Liebau Committed by GitHub

Prerender with streaming metadata during revalidation (#80245)

During the export phase of `next build` we're hard-coding
`serveStreamingMetadata` to `true`, so we need to do the same during
revalidation.

Otherwise we're checking the user agent to decide if we should serve
streaming metadata.

This fixes a hydration error that occurred when hydrating the resumed
PPR shell. The error was not present for the initial resume of the
prerendered shell, and only after the shell was revalidated once.
parent c2621a14
...@@ -233,16 +233,6 @@ export async function handler( ...@@ -233,16 +233,6 @@ export async function handler(
const botType = getBotType(userAgent) const botType = getBotType(userAgent)
const isHtmlBot = isHtmlBotRequest(req) const isHtmlBot = isHtmlBotRequest(req)
const shouldWaitOnAllReady = isHtmlBot && isRoutePPREnabled const shouldWaitOnAllReady = isHtmlBot && isRoutePPREnabled
let serveStreamingMetadata = shouldServeStreamingMetadata(
userAgent,
// @ts-expect-error update for readonly
nextConfig.htmlLimitedBots
)
if (isHtmlBot && isRoutePPREnabled) {
isIsr = false
serveStreamingMetadata = false
}
// If this is a dynamic route with PPR enabled and the default route // If this is a dynamic route with PPR enabled and the default route
// matches were set, then we should pass the fallback route params to // matches were set, then we should pass the fallback route params to
...@@ -314,6 +304,24 @@ export async function handler( ...@@ -314,6 +304,24 @@ export async function handler(
const isRevalidate = const isRevalidate =
isIsr && !supportsDynamicResponse && !postponed && !isDynamicRSCRequest isIsr && !supportsDynamicResponse && !postponed && !isDynamicRSCRequest
let serveStreamingMetadata =
// During the export phase of `next build` we're hard-coding
// `serveStreamingMetadata` to `true`, so we need to do the same during
// revalidation.
isRevalidate ||
// Otherwise we're checking the user agent to decide if we should
// serve streaming metadata.
shouldServeStreamingMetadata(
userAgent,
// @ts-expect-error update for readonly
nextConfig.htmlLimitedBots
)
if (isHtmlBot && isRoutePPREnabled) {
isIsr = false
serveStreamingMetadata = false
}
const method = req.method || 'GET' const method = req.method || 'GET'
const tracer = getTracer() const tracer = getTracer()
const activeSpan = tracer.getActiveScopeSpan() const activeSpan = tracer.getActiveScopeSpan()
......
import { unstable_cacheLife as cacheLife } from 'next/cache'
import { connection } from 'next/server'
import { Suspense } from 'react'
async function Dynamic() {
await connection()
return <p>Dynamic</p>
}
async function getCachedDate() {
'use cache'
cacheLife({ revalidate: 1, expire: 5 * 60 })
return new Date().toISOString()
}
export default async function Page() {
return (
<div>
<Suspense fallback={<p>Loading...</p>}>
<Dynamic />
</Suspense>
<p id="date">{await getCachedDate()}</p>
</div>
)
}
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
const nextConfig = { const nextConfig = {
experimental: { experimental: {
ppr: true, ppr: true,
useCache: true,
}, },
} }
......
import { nextTestSetup } from 'e2e-utils' import { nextTestSetup } from 'e2e-utils'
import cheerio from 'cheerio' import cheerio from 'cheerio'
import { assertNoConsoleErrors } from 'next-test-utils' import { assertNoConsoleErrors, retry } from 'next-test-utils'
function countSubstring(str: string, substr: string): number { function countSubstring(str: string, substr: string): number {
return str.split(substr).length - 1 return str.split(substr).length - 1
...@@ -19,7 +19,9 @@ describe('ppr-metadata-streaming', () => { ...@@ -19,7 +19,9 @@ describe('ppr-metadata-streaming', () => {
expect($(`${rootSelector} title`).text()).toBe('fully static') expect($(`${rootSelector} title`).text()).toBe('fully static')
expect(countSubstring($.html(), '<title>')).toBe(1) expect(countSubstring($.html(), '<title>')).toBe(1)
const browser = await next.browser('/fully-static') const browser = await next.browser('/fully-static', {
pushErrorAsConsoleLog: true,
})
expect( expect(
await browser.waitForElementByCss(`${rootSelector} title`).text() await browser.waitForElementByCss(`${rootSelector} title`).text()
).toBe('fully static') ).toBe('fully static')
...@@ -31,7 +33,9 @@ describe('ppr-metadata-streaming', () => { ...@@ -31,7 +33,9 @@ describe('ppr-metadata-streaming', () => {
expect($(`body title`).text()).toBe('dynamic page') expect($(`body title`).text()).toBe('dynamic page')
expect(countSubstring($.html(), '<title>')).toBe(1) expect(countSubstring($.html(), '<title>')).toBe(1)
const browser = await next.browser('/dynamic-page') const browser = await next.browser('/dynamic-page', {
pushErrorAsConsoleLog: true,
})
expect(await browser.waitForElementByCss('body title').text()).toBe( expect(await browser.waitForElementByCss('body title').text()).toBe(
'dynamic page' 'dynamic page'
) )
...@@ -46,7 +50,9 @@ describe('ppr-metadata-streaming', () => { ...@@ -46,7 +50,9 @@ describe('ppr-metadata-streaming', () => {
expect($('body title').text()).toBe('fully dynamic') expect($('body title').text()).toBe('fully dynamic')
expect(countSubstring($.html(), '<title>')).toBe(1) expect(countSubstring($.html(), '<title>')).toBe(1)
const browser = await next.browser('/fully-dynamic') const browser = await next.browser('/fully-dynamic', {
pushErrorAsConsoleLog: true,
})
expect(await browser.waitForElementByCss('body title').text()).toBe( expect(await browser.waitForElementByCss('body title').text()).toBe(
'fully dynamic' 'fully dynamic'
) )
...@@ -72,7 +78,9 @@ describe('ppr-metadata-streaming', () => { ...@@ -72,7 +78,9 @@ describe('ppr-metadata-streaming', () => {
expect($('body title').text()).toBe('dynamic-metadata - partial') expect($('body title').text()).toBe('dynamic-metadata - partial')
expect(countSubstring($.html(), '<title>')).toBe(1) expect(countSubstring($.html(), '<title>')).toBe(1)
const browser = await next.browser('/dynamic-metadata/partial') const browser = await next.browser('/dynamic-metadata/partial', {
pushErrorAsConsoleLog: true,
})
expect(await browser.waitForElementByCss('body title').text()).toBe( expect(await browser.waitForElementByCss('body title').text()).toBe(
'dynamic-metadata - partial' 'dynamic-metadata - partial'
) )
...@@ -85,12 +93,31 @@ describe('ppr-metadata-streaming', () => { ...@@ -85,12 +93,31 @@ describe('ppr-metadata-streaming', () => {
expect($(`${rootSelector} title`).text()).toBe('dynamic-page - partial') expect($(`${rootSelector} title`).text()).toBe('dynamic-page - partial')
expect(countSubstring($.html(), '<title>')).toBe(1) expect(countSubstring($.html(), '<title>')).toBe(1)
const browser = await next.browser('/dynamic-page/partial') const browser = await next.browser('/dynamic-page/partial', {
pushErrorAsConsoleLog: true,
})
expect( expect(
await browser.waitForElementByCss(`${rootSelector} title`).text() await browser.waitForElementByCss(`${rootSelector} title`).text()
).toBe('dynamic-page - partial') ).toBe('dynamic-page - partial')
await assertNoConsoleErrors(browser) await assertNoConsoleErrors(browser)
}) })
it('should not yield hydration errors after revalidation', async () => {
const browser = await next.browser('/partially-static', {
pushErrorAsConsoleLog: true,
})
const initialDate = await browser.elementById('date').text()
// Wait for the background revalidation to complete.
await retry(async () => {
await browser.refresh()
expect(await browser.elementById('date').text()).not.toBe(initialDate)
})
// There should be no hydration errors after the revalidation.
await assertNoConsoleErrors(browser)
})
}) })
// Skip the deployment tests for html limited bots // Skip the deployment tests for html limited bots
......
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