Unverified Commit 47753fe7 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

[metadata] enable streaming metadata by default (#76221)

parent d5f596ed
...@@ -387,7 +387,12 @@ async function exportAppImpl( ...@@ -387,7 +387,12 @@ async function exportAppImpl(
clientSegmentCache: nextConfig.experimental.clientSegmentCache ?? false, clientSegmentCache: nextConfig.experimental.clientSegmentCache ?? false,
inlineCss: nextConfig.experimental.inlineCss ?? false, inlineCss: nextConfig.experimental.inlineCss ?? false,
authInterrupts: !!nextConfig.experimental.authInterrupts, authInterrupts: !!nextConfig.experimental.authInterrupts,
streamingMetadata: !!nextConfig.experimental.streamingMetadata, streamingMetadata:
// Disable streaming metadata when dynamic IO is enabled.
// FIXME: remove dynamic IO guard once we fixed the dynamic indicator case.
// test/e2e/app-dir/dynamic-io/dynamic-io.test.ts - should not have static indicator on not-found route
!nextConfig.experimental.dynamicIO &&
!!nextConfig.experimental.streamingMetadata,
htmlLimitedBots: nextConfig.experimental.htmlLimitedBots, htmlLimitedBots: nextConfig.experimental.htmlLimitedBots,
}, },
reactMaxHeadersLength: nextConfig.reactMaxHeadersLength, reactMaxHeadersLength: nextConfig.reactMaxHeadersLength,
......
...@@ -424,8 +424,13 @@ export async function exportPages( ...@@ -424,8 +424,13 @@ export async function exportPages(
debugOutput: options.debugOutput, debugOutput: options.debugOutput,
enableExperimentalReact: needsExperimentalReact(nextConfig), enableExperimentalReact: needsExperimentalReact(nextConfig),
sriEnabled: Boolean(nextConfig.experimental.sri?.algorithm), sriEnabled: Boolean(nextConfig.experimental.sri?.algorithm),
streamingMetadata: nextConfig.experimental.streamingMetadata,
buildId: input.buildId, buildId: input.buildId,
streamingMetadata:
// Disable streaming metadata when dynamic IO is enabled.
// FIXME: remove dynamic IO guard once we fixed the dynamic indicator case.
// test/e2e/app-dir/dynamic-io/dynamic-io.test.ts - should not have static indicator on not-found route
!nextConfig.experimental.dynamicIO &&
!!nextConfig.experimental.streamingMetadata,
}), }),
// If exporting the page takes longer than the timeout, reject the promise. // If exporting the page takes longer than the timeout, reject the promise.
new Promise((_, reject) => { new Promise((_, reject) => {
......
...@@ -610,7 +610,12 @@ export default abstract class Server< ...@@ -610,7 +610,12 @@ export default abstract class Server<
this.nextConfig.experimental.clientSegmentCache ?? false, this.nextConfig.experimental.clientSegmentCache ?? false,
inlineCss: this.nextConfig.experimental.inlineCss ?? false, inlineCss: this.nextConfig.experimental.inlineCss ?? false,
authInterrupts: !!this.nextConfig.experimental.authInterrupts, authInterrupts: !!this.nextConfig.experimental.authInterrupts,
streamingMetadata: !!this.nextConfig.experimental.streamingMetadata, streamingMetadata:
// Disable streaming metadata when dynamic IO is enabled.
// FIXME: remove dynamic IO guard once we fixed the dynamic indicator case.
// test/e2e/app-dir/dynamic-io/dynamic-io.test.ts - should not have static indicator on not-found route
!this.nextConfig.experimental.dynamicIO &&
!!this.nextConfig.experimental.streamingMetadata,
htmlLimitedBots: this.nextConfig.experimental.htmlLimitedBots, htmlLimitedBots: this.nextConfig.experimental.htmlLimitedBots,
}, },
onInstrumentationRequestError: onInstrumentationRequestError:
......
...@@ -1258,7 +1258,7 @@ export const defaultConfig: NextConfig = { ...@@ -1258,7 +1258,7 @@ export const defaultConfig: NextConfig = {
staticGenerationMinPagesPerWorker: 25, staticGenerationMinPagesPerWorker: 25,
dynamicIO: false, dynamicIO: false,
inlineCss: false, inlineCss: false,
streamingMetadata: false, streamingMetadata: true,
htmlLimitedBots: undefined, htmlLimitedBots: undefined,
useCache: undefined, useCache: undefined,
slowModuleDetection: undefined, slowModuleDetection: undefined,
......
export default function Page() {
return <p>redirect dest page</p>
}
import { redirect } from 'next/navigation' import { redirect } from 'next/navigation'
export default function page() { export default function page() {
return 'redirect to basic' return 'redirect page'
} }
export async function generateMetadata() { export async function generateMetadata() {
redirect('/basic') redirect('/async/redirect/dest')
} }
...@@ -3,6 +3,7 @@ import { ...@@ -3,6 +3,7 @@ import {
createMultiDomMatcher, createMultiDomMatcher,
createMultiHtmlMatcher, createMultiHtmlMatcher,
getTitle, getTitle,
retry,
} from 'next-test-utils' } from 'next-test-utils'
describe('app dir - metadata navigation', () => { describe('app dir - metadata navigation', () => {
...@@ -28,7 +29,8 @@ describe('app dir - metadata navigation', () => { ...@@ -28,7 +29,8 @@ describe('app dir - metadata navigation', () => {
it('should support notFound in generateMetadata', async () => { it('should support notFound in generateMetadata', async () => {
const res = await next.fetch('/async/not-found') const res = await next.fetch('/async/not-found')
expect(res.status).toBe(404) // metadata is suspended in SSR, it won't affect the response status
expect(res.status).toBe(200)
const $ = await next.render$('/async/not-found') const $ = await next.render$('/async/not-found')
// TODO-APP: support render custom not-found in SSR for generateMetadata. // TODO-APP: support render custom not-found in SSR for generateMetadata.
...@@ -75,7 +77,14 @@ describe('app dir - metadata navigation', () => { ...@@ -75,7 +77,14 @@ describe('app dir - metadata navigation', () => {
const res = await next.fetch('/async/redirect', { const res = await next.fetch('/async/redirect', {
redirect: 'manual', redirect: 'manual',
}) })
expect(res.status).toBe(307) // metadata is suspended in SSR, it won't affect the response status
expect(res.status).toBe(200)
const browser = await next.browser('/async/redirect')
await retry(async () => {
expect(await browser.elementByCss('p').text()).toBe(
'redirect dest page'
)
})
}) })
it('should show the index title', async () => { it('should show the index title', async () => {
......
...@@ -2,14 +2,17 @@ import { nextTestSetup } from 'e2e-utils' ...@@ -2,14 +2,17 @@ import { nextTestSetup } from 'e2e-utils'
const isPPREnabled = process.env.__NEXT_EXPERIMENTAL_PPR === 'true' const isPPREnabled = process.env.__NEXT_EXPERIMENTAL_PPR === 'true'
// PPR tests are covered in test/e2e/app-dir/ppr-metadata-blocking
;(isPPREnabled ? describe.skip : describe)( ;(isPPREnabled ? describe.skip : describe)(
'app-dir - metadata-static-generation', 'app-dir - metadata-static-generation',
() => { () => {
const { next, isNextStart } = nextTestSetup({ const { next, isNextDev, isNextStart } = nextTestSetup({
files: __dirname, files: __dirname,
}) })
// In dev, it suspenses as dynamic rendering so it's inserted into body;
// In build, it's resolved as static rendering so it's inserted into head.
const rootSelector = isNextDev ? 'body' : 'head'
if (isNextStart) { 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
...@@ -28,20 +31,22 @@ const isPPREnabled = process.env.__NEXT_EXPERIMENTAL_PPR === 'true' ...@@ -28,20 +31,22 @@ const isPPREnabled = process.env.__NEXT_EXPERIMENTAL_PPR === 'true'
it('should contain async generated metadata in head for simple static page', async () => { it('should contain async generated metadata in head for simple static page', async () => {
const $ = await next.render$('/') const $ = await next.render$('/')
expect($('head title').text()).toBe('index page') expect($(`${rootSelector} title`).text()).toBe('index page')
expect($('head meta[name="description"]').attr('content')).toBe( expect(
'index page description' $(`${rootSelector} meta[name="description"]`).attr('content')
) ).toBe('index page description')
}) })
it('should contain async generated metadata in head static page with suspenseful content', async () => { it('should contain async generated metadata in head static page with suspenseful content', async () => {
const $ = await next.render$('/suspenseful/static') const $ = await next.render$('/suspenseful/static')
expect($('head title').text()).toBe('suspenseful page - static') expect($(`${rootSelector} title`).text()).toBe(
'suspenseful page - static'
)
}) })
it('should contain async generated metadata in head for dynamic page', async () => { it('should contain async generated metadata in body for dynamic page', async () => {
const $ = await next.render$('/suspenseful/dynamic') const $ = await next.render$('/suspenseful/dynamic')
expect($('head title').text()).toBe('suspenseful page - dynamic') expect($('body title').text()).toBe('suspenseful page - dynamic')
}) })
} }
) )
...@@ -21,8 +21,8 @@ export default function HashPage() { ...@@ -21,8 +21,8 @@ export default function HashPage() {
{items.map((item) => { {items.map((item) => {
if (item.id === 160) { if (item.id === 160) {
return ( return (
<>
<div key={item.id}> <div key={item.id}>
<div>
<div id={`hash-${item.id}`}>{item.id}</div> <div id={`hash-${item.id}`}>{item.id}</div>
</div> </div>
<div key="to-other-page"> <div key="to-other-page">
...@@ -35,7 +35,7 @@ export default function HashPage() { ...@@ -35,7 +35,7 @@ export default function HashPage() {
</Link> </Link>
</div> </div>
</div> </div>
</> </div>
) )
} }
return ( return (
......
...@@ -871,7 +871,9 @@ describe('app dir - navigation', () => { ...@@ -871,7 +871,9 @@ describe('app dir - navigation', () => {
const scrollPosition = await browser.eval('window.pageYOffset') const scrollPosition = await browser.eval('window.pageYOffset')
await browser.elementByCss("[href='/scroll-restoration/other']").click() await browser.elementByCss("[href='/scroll-restoration/other']").click()
await retry(async () => {
await browser.elementById('back-button').click() await browser.elementById('back-button').click()
})
const newScrollPosition = await browser.eval('window.pageYOffset') const newScrollPosition = await browser.eval('window.pageYOffset')
...@@ -910,15 +912,6 @@ describe('app dir - navigation', () => { ...@@ -910,15 +912,6 @@ describe('app dir - navigation', () => {
.click() .click()
if (!isNextDev) { if (!isNextDev) {
expect(
await browser
.waitForElementByCss(
'#loading',
// Give it some time to commit
100
)
.text()
).toEqual('Loading')
expect(await browser.elementByCss('title').text()).toBe('Async Title') expect(await browser.elementByCss('title').text()).toBe('Async Title')
await waitFor(resolveMetadataDuration + 500) await waitFor(resolveMetadataDuration + 500)
......
...@@ -4,6 +4,7 @@ function countSubstring(str: string, substr: string): number { ...@@ -4,6 +4,7 @@ function countSubstring(str: string, substr: string): number {
return str.split(substr).length - 1 return str.split(substr).length - 1
} }
// The default ppr-metadata-blocking is covered by test/e2e/app-dir/ppr-metadata-streaming/ppr-metadata-streaming.test.ts
describe('ppr-metadata-blocking-ppr-fallback', () => { describe('ppr-metadata-blocking-ppr-fallback', () => {
const { next, skipped } = nextTestSetup({ const { next, skipped } = nextTestSetup({
files: __dirname, files: __dirname,
......
import { nextTestSetup } from 'e2e-utils'
import cheerio from 'cheerio'
import { assertNoConsoleErrors } from 'next-test-utils'
function countSubstring(str: string, substr: string): number {
return str.split(substr).length - 1
}
describe('ppr-metadata-blocking', () => {
const { next, isNextDev, skipped } = nextTestSetup({
files: __dirname,
skipDeployment: true,
})
if (skipped) return
// No dynamic APIs used in metadata
describe('static metadata', () => {
it('should generate metadata in head when page is fully static', async () => {
const rootSelector = 'head'
const $ = await next.render$('/fully-static')
expect($(`${rootSelector} title`).text()).toBe('fully static')
expect(countSubstring($.html(), '<title>')).toBe(1)
const browser = await next.browser('/fully-static')
expect(
await browser.waitForElementByCss(`${rootSelector} title`).text()
).toBe('fully static')
await assertNoConsoleErrors(browser)
})
it('should insert metadata in head when page is dynamic page content', async () => {
const $ = await next.render$('/dynamic-page')
expect($(`head title`).text()).toBe('dynamic page')
expect(countSubstring($.html(), '<title>')).toBe(1)
const browser = await next.browser('/dynamic-page')
expect(await browser.waitForElementByCss('head title').text()).toBe(
'dynamic page'
)
await assertNoConsoleErrors(browser)
})
})
// Dynamic APIs used in metadata, metadata should be suspended and inserted into head
describe('dynamic metadata', () => {
it('should generate metadata in head when page is fully dynamic', async () => {
const $ = await next.render$('/fully-dynamic')
expect($('head title').text()).toBe('fully dynamic')
expect(countSubstring($.html(), '<title>')).toBe(1)
const browser = await next.browser('/fully-dynamic')
expect(await browser.waitForElementByCss('head title').text()).toBe(
'fully dynamic'
)
await assertNoConsoleErrors(browser)
})
it('should generate metadata in head when page content is static', async () => {
const $ = await next.render$('/dynamic-metadata')
expect(countSubstring($.html(), '<title>')).toBe(1)
expect($('head title').text()).toBe('dynamic metadata')
const browser = await next.browser('/dynamic-metadata')
expect(await browser.waitForElementByCss('head title').text()).toBe(
'dynamic metadata'
)
await assertNoConsoleErrors(browser)
})
})
describe('partial shell', () => {
it('should insert metadata into head with dynamic metadata and wrapped under layout Suspense boundary', async () => {
const $ = await next.render$('/dynamic-metadata/partial')
// Dev: dynamic rendering
if (isNextDev) {
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')
expect(await browser.waitForElementByCss('head title').text()).toBe(
'dynamic-metadata - partial'
)
await assertNoConsoleErrors(browser)
})
it('should insert metadata into head with dynamic metadata and dynamic page wrapped under layout Suspense boundary', async () => {
const rootSelector = 'head'
const $ = await next.render$('/dynamic-page/partial')
expect($(`${rootSelector} title`).text()).toBe('dynamic-page - partial')
expect(countSubstring($.html(), '<title>')).toBe(1)
const browser = await next.browser('/dynamic-page/partial')
expect(
await browser.waitForElementByCss(`${rootSelector} title`).text()
).toBe('dynamic-page - partial')
await assertNoConsoleErrors(browser)
})
})
if (!isNextDev) {
// This test is only relevant in production mode, as it's testing PPR results
describe('html limited bots', () => {
it('should serve partial static shell when normal UA requests the page', async () => {
const res1 = await next.fetch('/dynamic-page/partial')
const res2 = await next.fetch('/dynamic-page/partial')
const $1 = cheerio.load(await res1.text())
const $2 = cheerio.load(await res2.text())
const attribute1 = parseInt($1('[data-date]').attr('data-date'))
const attribute2 = parseInt($2('[data-date]').attr('data-date'))
// Normal UA should still get the partial static shell produced by PPR
expect(attribute1).toBe(attribute2)
expect(attribute1).toBeTruthy()
const headers = res1.headers
// Static render should have postponed header
expect(headers.get('x-nextjs-postponed')).toBe('1')
})
it('should not serve partial static shell when html limited bots requests the page', async () => {
const htmlLimitedBotUA = 'Discordbot'
const res1 = await next.fetch('/dynamic-page/partial', {
headers: {
'User-Agent': htmlLimitedBotUA,
},
})
const res2 = await next.fetch('/dynamic-page/partial', {
headers: {
'User-Agent': htmlLimitedBotUA,
},
})
// Dynamic render should not have postponed header
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)
const $1 = cheerio.load(await res1.text())
const $2 = cheerio.load(await res2.text())
const attribute1 = parseInt($1('[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
expect(attribute2).not.toEqual(attribute1)
expect(attribute1).toBeTruthy()
})
})
}
})
...@@ -19,6 +19,10 @@ async function resolveStreamResponse(response: any, onData?: any) { ...@@ -19,6 +19,10 @@ async function resolveStreamResponse(response: any, onData?: any) {
return result return result
} }
function stripHTMLComments(html: string): string {
return html.replace(/<!--[\s\S]*?-->/g, '')
}
describe('app dir - rsc basics', () => { describe('app dir - rsc basics', () => {
const { next, isNextDev, isNextStart, isTurbopack } = nextTestSetup({ const { next, isNextDev, isNextStart, isTurbopack } = nextTestSetup({
files: __dirname, files: __dirname,
...@@ -72,13 +76,13 @@ describe('app dir - rsc basics', () => { ...@@ -72,13 +76,13 @@ describe('app dir - rsc basics', () => {
it('should correctly render page returning null', async () => { it('should correctly render page returning null', async () => {
const homeHTML = await next.render('/return-null/page') const homeHTML = await next.render('/return-null/page')
const $ = cheerio.load(homeHTML) const $ = cheerio.load(homeHTML)
expect($('#return-null-layout').html()).toBeEmpty() expect(stripHTMLComments($('#return-null-layout').html())).toBeEmpty()
}) })
it('should correctly render component returning null', async () => { it('should correctly render component returning null', async () => {
const homeHTML = await next.render('/return-null/component') const homeHTML = await next.render('/return-null/component')
const $ = cheerio.load(homeHTML) const $ = cheerio.load(homeHTML)
expect($('#return-null-layout').html()).toBeEmpty() expect(stripHTMLComments($('#return-null-layout').html())).toBeEmpty()
}) })
it('should correctly render layout returning null', async () => { it('should correctly render layout returning null', async () => {
...@@ -90,13 +94,13 @@ describe('app dir - rsc basics', () => { ...@@ -90,13 +94,13 @@ describe('app dir - rsc basics', () => {
it('should correctly render page returning undefined', async () => { it('should correctly render page returning undefined', async () => {
const homeHTML = await next.render('/return-undefined/page') const homeHTML = await next.render('/return-undefined/page')
const $ = cheerio.load(homeHTML) const $ = cheerio.load(homeHTML)
expect($('#return-undefined-layout').html()).toBeEmpty() expect(stripHTMLComments($('#return-undefined-layout').html())).toBeEmpty()
}) })
it('should correctly render component returning undefined', async () => { it('should correctly render component returning undefined', async () => {
const homeHTML = await next.render('/return-undefined/component') const homeHTML = await next.render('/return-undefined/component')
const $ = cheerio.load(homeHTML) const $ = cheerio.load(homeHTML)
expect($('#return-undefined-layout').html()).toBeEmpty() expect(stripHTMLComments($('#return-undefined-layout').html())).toBeEmpty()
}) })
it('should correctly render layout returning undefined', async () => { it('should correctly render layout returning undefined', async () => {
......
...@@ -172,8 +172,8 @@ describe('opentelemetry', () => { ...@@ -172,8 +172,8 @@ describe('opentelemetry', () => {
'next.clientComponentLoadCount': isNextDev 'next.clientComponentLoadCount': isNextDev
? // In dev, additional client components are being loaded ? // In dev, additional client components are being loaded
// due to RSC props being deserialized. // due to RSC props being deserialized.
9 11
: 6, : 8,
'next.span_type': 'next.span_type':
'NextNodeServer.clientComponentLoading', 'NextNodeServer.clientComponentLoading',
}, },
......
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