Unverified Commit 4734763e authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

[metadata] do not rethrow postpone when streaming metadata is not enabled (#75887)

### What

We should not throw the postpone error in metadata resolving when
streaming metadata is not enabled.

### Why

The suspense boundary of streaming metadata can catch the postpone error
and render the fallback, but in blocking metadata world we should just
swallow it otherwise it will send empty fallback shell
parent 9f5806ab
...@@ -162,14 +162,14 @@ export function createMetadataComponents({ ...@@ -162,14 +162,14 @@ export function createMetadataComponents({
} catch (notFoundMetadataErr) { } catch (notFoundMetadataErr) {
// In PPR rendering we still need to throw the postpone error. // In PPR rendering we still need to throw the postpone error.
// If metadata is postponed, React needs to be aware of the location of error. // If metadata is postponed, React needs to be aware of the location of error.
if (isPostpone(notFoundMetadataErr)) { if (serveStreamingMetadata && isPostpone(notFoundMetadataErr)) {
throw notFoundMetadataErr throw notFoundMetadataErr
} }
} }
} }
// In PPR rendering we still need to throw the postpone error. // In PPR rendering we still need to throw the postpone error.
// If metadata is postponed, React needs to be aware of the location of error. // If metadata is postponed, React needs to be aware of the location of error.
if (isPostpone(metadataErr)) { if (serveStreamingMetadata && isPostpone(metadataErr)) {
throw metadataErr throw metadataErr
} }
// We don't actually want to error in this component. We will // We don't actually want to error in this component. We will
......
...@@ -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-static-generation', () => { // PPR tests are covered in test/e2e/app-dir/ppr-metadata-blocking
;(isPPREnabled ? describe.skip : describe)(
'app-dir - metadata-static-generation',
() => {
const { next, isNextStart } = nextTestSetup({ const { next, 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.
...@@ -40,4 +43,5 @@ describe('app-dir - metadata-static-generation', () => { ...@@ -40,4 +43,5 @@ describe('app-dir - metadata-static-generation', () => {
const $ = await next.render$('/suspenseful/dynamic') const $ = await next.render$('/suspenseful/dynamic')
expect($('head title').text()).toBe('suspenseful page - dynamic') expect($('head title').text()).toBe('suspenseful page - dynamic')
}) })
}) }
)
import { nextTestSetup } from 'e2e-utils'
function countSubstring(str: string, substr: string): number {
return str.split(substr).length - 1
}
describe('ppr-metadata-blocking-ppr-fallback', () => {
const { next, skipped } = nextTestSetup({
files: __dirname,
skipDeployment: true,
env: {
__NEXT_EXPERIMENTAL_STATIC_SHELL_DEBUGGING: '1',
},
})
if (skipped) return
it('should not include metadata in partial shell when page is fully dynamic', async () => {
const $ = await next.render$('/fully-dynamic?__nextppronly=fallback')
expect(countSubstring($.html(), '<title>')).toBe(0)
})
it('should include viewport metadata in partial shell when metadata is dynamic under suspense', async () => {
const $ = await next.render$(
'/dynamic-metadata/partial?__nextppronly=fallback'
)
expect(countSubstring($.html(), '<title>')).toBe(0)
expect(countSubstring($.html(), '<meta name="viewport"')).toBe(1)
})
it('should include viewport metadata in partial shell when page is partially dynamic', async () => {
const $ = await next.render$('/dynamic-page/partial?__nextppronly=fallback')
expect($('head title').text()).toBe('dynamic-page - partial')
expect(countSubstring($.html(), '<title>')).toBe(1)
expect(countSubstring($.html(), '<meta name="viewport"')).toBe(1)
})
})
...@@ -7,7 +7,7 @@ function countSubstring(str: string, substr: string): number { ...@@ -7,7 +7,7 @@ function countSubstring(str: string, substr: string): number {
} }
describe('ppr-metadata-blocking', () => { describe('ppr-metadata-blocking', () => {
const { next, isNextStart } = nextTestSetup({ const { next, isNextDev, isNextStart } = nextTestSetup({
files: __dirname, files: __dirname,
}) })
...@@ -55,8 +55,8 @@ describe('ppr-metadata-blocking', () => { ...@@ -55,8 +55,8 @@ describe('ppr-metadata-blocking', () => {
it('should generate metadata in head when page content is static', async () => { it('should generate metadata in head when page content is static', async () => {
const $ = await next.render$('/dynamic-metadata') const $ = await next.render$('/dynamic-metadata')
expect($('head title').text()).toBe('dynamic metadata')
expect(countSubstring($.html(), '<title>')).toBe(1) expect(countSubstring($.html(), '<title>')).toBe(1)
expect($('head title').text()).toBe('dynamic metadata')
const browser = await next.browser('/dynamic-metadata') const browser = await next.browser('/dynamic-metadata')
expect(await browser.waitForElementByCss('head title').text()).toBe( expect(await browser.waitForElementByCss('head title').text()).toBe(
...@@ -69,8 +69,14 @@ describe('ppr-metadata-blocking', () => { ...@@ -69,8 +69,14 @@ describe('ppr-metadata-blocking', () => {
describe('partial shell', () => { describe('partial shell', () => {
it('should insert metadata into head with dynamic metadata and wrapped under layout Suspense boundary', async () => { it('should insert metadata into head with dynamic metadata and wrapped under layout Suspense boundary', async () => {
const $ = await next.render$('/dynamic-metadata/partial') const $ = await next.render$('/dynamic-metadata/partial')
expect($('head title').text()).toBe('dynamic-metadata - partial') // Dev: dynamic rendering
if (isNextDev) {
expect(countSubstring($.html(), '<title>')).toBe(1) expect(countSubstring($.html(), '<title>')).toBe(1)
expect($('head title').text()).toBe('dynamic-metadata - partial')
} else {
// Production: PPR
expect(countSubstring($.html(), '<title>')).toBe(0)
}
const browser = await next.browser('/dynamic-metadata/partial') const browser = await next.browser('/dynamic-metadata/partial')
expect(await browser.waitForElementByCss('head title').text()).toBe( expect(await browser.waitForElementByCss('head title').text()).toBe(
...@@ -133,6 +139,9 @@ describe('ppr-metadata-blocking', () => { ...@@ -133,6 +139,9 @@ describe('ppr-metadata-blocking', () => {
// Dynamic render should not have postponed header // Dynamic render should not have postponed header
const headers = res1.headers const headers = res1.headers
// In blocking mode of metadata, it's still postponed if metadata or page is dynamic.
// It won't behave differently when the bot is visiting.
expect(headers.get('x-nextjs-postponed')).toBe(null) expect(headers.get('x-nextjs-postponed')).toBe(null)
const $1 = cheerio.load(await res1.text()) const $1 = cheerio.load(await res1.text())
...@@ -142,7 +151,7 @@ describe('ppr-metadata-blocking', () => { ...@@ -142,7 +151,7 @@ describe('ppr-metadata-blocking', () => {
const attribute2 = parseInt($2('[data-date]').attr('data-date')) const attribute2 = parseInt($2('[data-date]').attr('data-date'))
// 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).not.toEqual(attribute1)
expect(attribute1).toBeTruthy() expect(attribute1).toBeTruthy()
}) })
}) })
......
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