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

Fix default metadata is missing in root not-found (#50044)

* Adding default metadata to root not found under root layout
* Refactor the tests a bit, previously it only checks if the text existed in html, it actually exisits in flight response

Fixes #49979
Closes NEXT-1207
parent 448b7429
...@@ -76,6 +76,8 @@ import { appendMutableCookies } from '../web/spec-extension/adapters/request-coo ...@@ -76,6 +76,8 @@ import { appendMutableCookies } from '../web/spec-extension/adapters/request-coo
export const isEdgeRuntime = process.env.NEXT_RUNTIME === 'edge' export const isEdgeRuntime = process.env.NEXT_RUNTIME === 'edge'
const emptyLoaderTree: LoaderTree = ['', {}, {}]
export type GetDynamicParamFromSegment = ( export type GetDynamicParamFromSegment = (
// [slug] / [[slug]] / [...slug] // [slug] / [[slug]] / [...slug]
segment: string segment: string
...@@ -1292,6 +1294,18 @@ export async function renderToHTMLOrFlight( ...@@ -1292,6 +1294,18 @@ export async function renderToHTMLOrFlight(
query query
) )
const createMetadata = (tree: LoaderTree) => (
// Adding key={requestId} to make metadata remount for each render
// @ts-expect-error allow to use async server component
<MetadataTree
key={requestId}
tree={tree}
pathname={pathname}
searchParams={providedSearchParams}
getDynamicParamFromSegment={getDynamicParamFromSegment}
/>
)
return ( return (
<> <>
{styles} {styles}
...@@ -1301,15 +1315,7 @@ export async function renderToHTMLOrFlight( ...@@ -1301,15 +1315,7 @@ export async function renderToHTMLOrFlight(
initialTree={initialTree} initialTree={initialTree}
initialHead={ initialHead={
<> <>
{/* Adding key={requestId} to make metadata remount for each render */} {createMetadata(loaderTree)}
{/* @ts-expect-error allow to use async server component */}
<MetadataTree
key={requestId}
tree={loaderTree}
pathname={pathname}
searchParams={providedSearchParams}
getDynamicParamFromSegment={getDynamicParamFromSegment}
/>
{appUsingSizeAdjust ? <meta name="next-size-adjust" /> : null} {appUsingSizeAdjust ? <meta name="next-size-adjust" /> : null}
</> </>
} }
...@@ -1317,6 +1323,7 @@ export async function renderToHTMLOrFlight( ...@@ -1317,6 +1323,7 @@ export async function renderToHTMLOrFlight(
notFound={ notFound={
NotFound && RootLayout ? ( NotFound && RootLayout ? (
<RootLayout params={{}}> <RootLayout params={{}}>
{createMetadata(emptyLoaderTree)}
{notFoundStyles} {notFoundStyles}
<NotFound /> <NotFound />
</RootLayout> </RootLayout>
...@@ -1526,7 +1533,7 @@ export async function renderToHTMLOrFlight( ...@@ -1526,7 +1533,7 @@ export async function renderToHTMLOrFlight(
{/* @ts-expect-error allow to use async server component */} {/* @ts-expect-error allow to use async server component */}
<MetadataTree <MetadataTree
key={requestId} key={requestId}
tree={['', {}, {}]} tree={emptyLoaderTree}
pathname={pathname} pathname={pathname}
searchParams={providedSearchParams} searchParams={providedSearchParams}
getDynamicParamFromSegment={getDynamicParamFromSegment} getDynamicParamFromSegment={getDynamicParamFromSegment}
......
...@@ -76,7 +76,7 @@ createNextDescribe( ...@@ -76,7 +76,7 @@ createNextDescribe(
* @example * @example
* *
* const $ = await next.render$('html') * const $ = await next.render$('html')
* const matchHtml = createHtmlMatcher($) * const matchHtml = createMultiHtmlMatcher($)
* await matchHtml('meta', 'name', 'property', { * await matchHtml('meta', 'name', 'property', {
* description: 'description', * description: 'description',
* og: 'og:description' * og: 'og:description'
...@@ -414,12 +414,42 @@ createNextDescribe( ...@@ -414,12 +414,42 @@ createNextDescribe(
) )
}) })
it('should render root not-found with default metadata', async () => {
const $ = await next.render$('/does-not-exist')
// Should contain default metadata and noindex tag
const matchHtml = createMultiHtmlMatcher($)
expect($('meta[charset="utf-8"]').length).toBe(1)
await matchHtml('meta', 'name', 'content', {
viewport: 'width=device-width, initial-scale=1',
robots: 'noindex',
})
})
it('should support notFound in generateMetadata', async () => { it('should support notFound in generateMetadata', async () => {
// TODO-APP: support custom not-found for generateMetadata
const res = await next.fetch('/async/not-found') const res = await next.fetch('/async/not-found')
expect(res.status).toBe(404) expect(res.status).toBe(404)
const html = await res.text() const html = await res.text()
expect(html).toContain('root not found page') const $ = cheerio.load(html)
// TODO-APP: support render custom not-found in SSR for generateMetadata.
// Check contains root not-found payload in flight response for now.
let hasRootNotFoundFlight = false
for (const el of $('script').toArray()) {
const text = $(el).text()
if (text.includes('root not found page')) {
hasRootNotFoundFlight = true
}
}
expect(hasRootNotFoundFlight).toBe(true)
// Should contain default metadata and noindex tag
const matchHtml = createMultiHtmlMatcher($)
expect($('meta[charset="utf-8"]').length).toBe(1)
await matchHtml('meta', 'name', 'content', {
viewport: 'width=device-width, initial-scale=1',
robots: 'noindex',
})
const browser = await next.browser('/async/not-found') const browser = await next.browser('/async/not-found')
expect(await browser.elementByCss('h2').text()).toBe( expect(await browser.elementByCss('h2').text()).toBe(
......
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