Unverified Commit d5f596ed authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

[metadata] only blocking render for html bots in ppr pages (#76379)

### What

In non-PRR mode, we keep the original behavior that metadata is blocking but stream the rest. Previously we also do the full dynamic rendering for both non-PPR and PPR pages, which is required for PPR pages to discard the postpone cache but not necessary for non-PPR pages.

This PR align the behavior as before.

Closes NDX-881
parent 71e139d2
...@@ -2067,11 +2067,6 @@ export default abstract class Server< ...@@ -2067,11 +2067,6 @@ export default abstract class Server<
} }
} }
const isHtmlBot = isHtmlBotRequest(req)
if (isHtmlBot) {
this.renderOpts.serveStreamingMetadata = false
}
if ( if (
hasFallback || hasFallback ||
staticPaths?.includes(resolvedUrlPathname) || staticPaths?.includes(resolvedUrlPathname) ||
...@@ -2082,11 +2077,6 @@ export default abstract class Server< ...@@ -2082,11 +2077,6 @@ export default abstract class Server<
isSSG = true isSSG = true
} else if (!this.renderOpts.dev) { } else if (!this.renderOpts.dev) {
isSSG ||= !!prerenderManifest.routes[toRoute(pathname)] isSSG ||= !!prerenderManifest.routes[toRoute(pathname)]
if (isHtmlBot) {
// When it's html limited bots request, disable SSG
// and perform the full blocking & dynamic rendering.
isSSG = false
}
} }
// Toggle whether or not this is a Data request // Toggle whether or not this is a Data request
...@@ -2227,6 +2217,12 @@ export default abstract class Server< ...@@ -2227,6 +2217,12 @@ export default abstract class Server<
'segmentPrefetchRSCRequest' 'segmentPrefetchRSCRequest'
) )
const isHtmlBot = isHtmlBotRequest(req)
if (isHtmlBot && isRoutePPREnabled) {
isSSG = false
this.renderOpts.serveStreamingMetadata = false
}
// we need to ensure the status code if /404 is visited directly // we need to ensure the status code if /404 is visited directly
if (is404Page && !isNextDataRequest && !isRSCRequest) { if (is404Page && !isNextDataRequest && !isRSCRequest) {
res.statusCode = 404 res.statusCode = 404
...@@ -2483,7 +2479,11 @@ export default abstract class Server< ...@@ -2483,7 +2479,11 @@ export default abstract class Server<
query: origQuery, query: origQuery,
}) })
const shouldWaitOnAllReady = !supportsDynamicResponse || isHtmlBot const shouldWaitOnAllReady =
!supportsDynamicResponse ||
// When html bots request PPR page, perform the full dynamic rendering.
(isHtmlBot && isRoutePPREnabled)
const renderOpts: LoadedRenderOpts = { const renderOpts: LoadedRenderOpts = {
...components, ...components,
...opts, ...opts,
......
...@@ -2,12 +2,13 @@ import { connection } from 'next/server' ...@@ -2,12 +2,13 @@ import { connection } from 'next/server'
export default async function Page() { export default async function Page() {
await connection() await connection()
await new Promise((resolve) => setTimeout(resolve, 500))
return <p>suspenseful - dynamic</p> return <p>suspenseful - dynamic</p>
} }
export async function generateMetadata() { export async function generateMetadata() {
await connection() await connection()
await new Promise((resolve) => setTimeout(resolve, 1 * 1000)) await new Promise((resolve) => setTimeout(resolve, 200))
return { return {
title: 'suspenseful page - dynamic', title: 'suspenseful page - dynamic',
} }
......
...@@ -2,12 +2,15 @@ import { nextTestSetup } from 'e2e-utils' ...@@ -2,12 +2,15 @@ import { nextTestSetup } from 'e2e-utils'
const isPPREnabled = process.env.__NEXT_EXPERIMENTAL_PPR === 'true' const isPPREnabled = process.env.__NEXT_EXPERIMENTAL_PPR === 'true'
describe('app-dir - metadata-streaming-static-generation', () => { // Skip PPR test as it's covered in test/e2e/app-dir/ppr-metadata-streaming/ppr-metadata-streaming.test.ts
;(isPPREnabled ? describe.skip : describe)(
'app-dir - metadata-streaming-static-generation',
() => {
const { next, isNextDev, isNextStart } = nextTestSetup({ const { next, isNextDev, isNextStart } = nextTestSetup({
files: __dirname, files: __dirname,
}) })
if (isNextStart && !isPPREnabled) { if (isNextStart) {
// Precondition for the following tests in build mode. // Precondition for the following tests in build mode.
// This test is only useful for non-PPR mode as in PPR mode those routes // This test is only useful for non-PPR mode as in PPR mode those routes
// are all listed in the prerender manifest. // are all listed in the prerender manifest.
...@@ -74,16 +77,24 @@ describe('app-dir - metadata-streaming-static-generation', () => { ...@@ -74,16 +77,24 @@ describe('app-dir - metadata-streaming-static-generation', () => {
}) })
describe('dynamic pages with html limited bots', () => { describe('dynamic pages with html limited bots', () => {
it('should contain async generated metadata in head for simple dynamics page', async () => { it('should contain stream metadata in head for suspenseful dynamic page', async () => {
const $ = await next.render$('/suspenseful/dynamic', undefined, { const $ = await next.render$('/suspenseful/dynamic', undefined, {
headers: { headers: {
'User-Agent': 'Discordbot/2.0;', 'User-Agent': 'Discordbot/2.0;',
}, },
}) })
expect($('head title').text()).toBe('suspenseful page - dynamic') expect($('head title').text()).toBe('suspenseful page - dynamic')
// Ensure it's suspenseful content
expect($('.suspenseful-layout').text()).toBe('')
// Can still render the suspenseful content with browser
const browser = await next.browser('/suspenseful/dynamic')
expect(await browser.elementByCss('.suspenseful-layout').text()).toBe(
'suspenseful - dynamic'
)
}) })
it('should contain async generated metadata in head for suspenseful dynamic page', async () => { it('should contain async generated metadata in head for simple dynamic page', async () => {
const $ = await next.render$('/slow/dynamic', undefined, { const $ = await next.render$('/slow/dynamic', undefined, {
headers: { headers: {
'User-Agent': 'Discordbot/2.0;', 'User-Agent': 'Discordbot/2.0;',
...@@ -92,4 +103,5 @@ describe('app-dir - metadata-streaming-static-generation', () => { ...@@ -92,4 +103,5 @@ describe('app-dir - metadata-streaming-static-generation', () => {
expect($('head title').text()).toBe('slow page - dynamic') expect($('head title').text()).toBe('slow page - dynamic')
}) })
}) })
}) }
)
...@@ -14,7 +14,7 @@ async function SuspendedComponent() { ...@@ -14,7 +14,7 @@ async function SuspendedComponent() {
await new Promise((resolve) => setTimeout(resolve, 500)) await new Promise((resolve) => setTimeout(resolve, 500))
return ( return (
<div> <div>
<div>suspended component</div> <div>outer suspended component</div>
<NestedSuspendedComponent /> <NestedSuspendedComponent />
</div> </div>
) )
......
...@@ -103,7 +103,7 @@ describe('ppr-metadata-streaming', () => { ...@@ -103,7 +103,7 @@ describe('ppr-metadata-streaming', () => {
if (!isNextDev && !isNextDeploy) { if (!isNextDev && !isNextDeploy) {
// This test is only relevant in production mode, as it's testing PPR results // This test is only relevant in production mode, as it's testing PPR results
describe('html limited bots', () => { describe('html limited bots', () => {
it('should serve partial static shell when normal UA requests the page', async () => { it('should serve partial static shell when normal UA requests the PPR page', async () => {
const res1 = await next.fetch('/dynamic-page/partial') const res1 = await next.fetch('/dynamic-page/partial')
const res2 = await next.fetch('/dynamic-page/partial') const res2 = await next.fetch('/dynamic-page/partial')
...@@ -123,7 +123,7 @@ describe('ppr-metadata-streaming', () => { ...@@ -123,7 +123,7 @@ describe('ppr-metadata-streaming', () => {
expect(headers.get('x-nextjs-postponed')).toBe('1') expect(headers.get('x-nextjs-postponed')).toBe('1')
}) })
it('should not serve partial static shell when html limited bots requests the page', async () => { it('should perform blocking and dynamic rendering when html limited bots requests the PPR page', async () => {
const htmlLimitedBotUA = 'Discordbot' const htmlLimitedBotUA = 'Discordbot'
const res1 = await next.fetch('/dynamic-page/partial', { const res1 = await next.fetch('/dynamic-page/partial', {
headers: { headers: {
...@@ -150,6 +150,11 @@ describe('ppr-metadata-streaming', () => { ...@@ -150,6 +150,11 @@ describe('ppr-metadata-streaming', () => {
// Two requests are dynamic and should not have the same data-date attribute // Two requests are dynamic and should not have the same data-date attribute
expect(attribute2).toBeGreaterThan(attribute1) expect(attribute2).toBeGreaterThan(attribute1)
expect(attribute1).toBeTruthy() expect(attribute1).toBeTruthy()
// Should contain resolved suspense content
const bodyHtml = $1('body').html()
expect(bodyHtml).toContain('outer suspended component')
expect(bodyHtml).toContain('nested suspended component')
}) })
}) })
} }
......
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