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

Assign default not-found boundary if custom not-found is not present for root layer only (#54185)

Fixes #54174 

We should only add default not-found boundary to loader tree components for root layout. It was accidently added for children routes before
parent 847c14ea
...@@ -53,6 +53,8 @@ const GLOBAL_ERROR_FILE_TYPE = 'global-error' ...@@ -53,6 +53,8 @@ const GLOBAL_ERROR_FILE_TYPE = 'global-error'
const PAGE_SEGMENT = 'page$' const PAGE_SEGMENT = 'page$'
const PARALLEL_CHILDREN_SEGMENT = 'children$' const PARALLEL_CHILDREN_SEGMENT = 'children$'
const defaultNotFoundPath = 'next/dist/client/components/not-found-error'
type DirResolver = (pathToResolve: string) => string type DirResolver = (pathToResolve: string) => string
type PathResolver = ( type PathResolver = (
pathname: string pathname: string
...@@ -311,6 +313,14 @@ async function createTreeCodeFromPath( ...@@ -311,6 +313,14 @@ async function createTreeCodeFromPath(
([, filePath]) => filePath !== undefined ([, filePath]) => filePath !== undefined
) )
// Add default not found error as root not found if not present
const hasNotFound = definedFilePaths.some(
([type]) => type === 'not-found'
)
if (isRootLayer && !hasNotFound) {
definedFilePaths.push(['not-found', defaultNotFoundPath])
}
if (!rootLayout) { if (!rootLayout) {
const layoutPath = definedFilePaths.find( const layoutPath = definedFilePaths.find(
([type]) => type === 'layout' ([type]) => type === 'layout'
...@@ -321,17 +331,6 @@ async function createTreeCodeFromPath( ...@@ -321,17 +331,6 @@ async function createTreeCodeFromPath(
globalError = await resolver( globalError = await resolver(
`${path.dirname(layoutPath)}/${GLOBAL_ERROR_FILE_TYPE}` `${path.dirname(layoutPath)}/${GLOBAL_ERROR_FILE_TYPE}`
) )
const hasNotFound = definedFilePaths.some(
([type]) => type === 'not-found'
)
// Add default not found error as root not found if not present
if (!hasNotFound) {
const notFoundPath = 'next/dist/client/components/not-found-error'
if (notFoundPath) {
definedFilePaths.push(['not-found', notFoundPath])
}
}
} }
} }
...@@ -350,7 +349,7 @@ async function createTreeCodeFromPath( ...@@ -350,7 +349,7 @@ async function createTreeCodeFromPath(
if (isNotFoundRoute && normalizedParallelKey === 'children') { if (isNotFoundRoute && normalizedParallelKey === 'children') {
const notFoundPath = const notFoundPath =
definedFilePaths.find(([type]) => type === 'not-found')?.[1] ?? definedFilePaths.find(([type]) => type === 'not-found')?.[1] ??
'next/dist/client/components/not-found-error' defaultNotFoundPath
subtreeCode = `{ subtreeCode = `{
children: ['__PAGE__', {}, { children: ['__PAGE__', {}, {
page: [ page: [
......
import { notFound } from 'next/navigation'
// avoid static generation to fill the dynamic params
export const dynamic = 'force-dynamic'
export default function Page({ params: { id } }) {
if (id === '404') {
notFound()
}
return <p id="page">{`dynamic-layout-without-not-found [id]`}</p>
}
export default function Layout({ children }) {
return (
<div>
<h1>Dynamic with Layout</h1>
{children}
</div>
)
}
export default function Page() {
return <div>dynamic-with-layout</div>
}
...@@ -3,8 +3,10 @@ import { notFound } from 'next/navigation' ...@@ -3,8 +3,10 @@ import { notFound } from 'next/navigation'
// avoid static generation to fill the dynamic params // avoid static generation to fill the dynamic params
export const dynamic = 'force-dynamic' export const dynamic = 'force-dynamic'
export default function Page() { export default function Page({ params: { id } }) {
if (id === '404') {
notFound() notFound()
}
return <>{`dynamic [id]`}</> return <p id="page">{`dynamic [id]`}</p>
} }
export default function NotFound() { export default function NotFound() {
return ( return (
<> <>
<h1>This Is The Not Found Page</h1> <h1>Root Not Found</h1>
<div id="timestamp">{Date.now()}</div> <div id="timestamp">{Date.now()}</div>
</> </>
......
...@@ -29,7 +29,7 @@ createNextDescribe( ...@@ -29,7 +29,7 @@ createNextDescribe(
it('should use root not-found content for 404 html', async () => { it('should use root not-found content for 404 html', async () => {
// static /404 page will use /_not-found content // static /404 page will use /_not-found content
const page404 = await next.readFile('.next/server/pages/404.html') const page404 = await next.readFile('.next/server/pages/404.html')
expect(page404).toContain('This Is The Not Found Page') expect(page404).toContain('Root Not Found')
}) })
} }
...@@ -37,7 +37,7 @@ createNextDescribe( ...@@ -37,7 +37,7 @@ createNextDescribe(
it('should use the not-found page for non-matching routes', async () => { it('should use the not-found page for non-matching routes', async () => {
const browser = await next.browser('/random-content') const browser = await next.browser('/random-content')
expect(await browser.elementByCss('h1').text()).toContain( expect(await browser.elementByCss('h1').text()).toContain(
'This Is The Not Found Page' 'Root Not Found'
) )
// should contain root layout content // should contain root layout content
expect(await browser.elementByCss('#layout-nav').text()).toBe('Navbar') expect(await browser.elementByCss('#layout-nav').text()).toBe('Navbar')
...@@ -48,11 +48,40 @@ createNextDescribe( ...@@ -48,11 +48,40 @@ createNextDescribe(
const browserDynamic = await next.browser('/dynamic') const browserDynamic = await next.browser('/dynamic')
expect(await browserDynamic.elementByCss('main').text()).toBe('dynamic') expect(await browserDynamic.elementByCss('main').text()).toBe('dynamic')
// `/dynamic/[id]` calling notFound() will match the same level not-found boundary // `/dynamic/404` calling notFound() will match the same level not-found boundary
const browserDynamicId = await next.browser('/dynamic/123') const browserDynamic404 = await next.browser('/dynamic/404')
expect(await browserDynamicId.elementByCss('#not-found').text()).toBe( expect(await browserDynamic404.elementByCss('#not-found').text()).toBe(
'dynamic/[id] not found' 'dynamic/[id] not found'
) )
const browserDynamicId = await next.browser('/dynamic/123')
expect(await browserDynamicId.elementByCss('#page').text()).toBe(
'dynamic [id]'
)
})
it('should escalate notFound to parent layout if no not-found boundary present in current layer', async () => {
const browserDynamic = await next.browser(
'/dynamic-layout-without-not-found'
)
expect(await browserDynamic.elementByCss('h1').text()).toBe(
'Dynamic with Layout'
)
// no not-found boundary in /dynamic-layout-without-not-found, escalate to parent layout to render root not-found
const browserDynamicId = await next.browser(
'/dynamic-layout-without-not-found/404'
)
expect(await browserDynamicId.elementByCss('h1').text()).toBe(
'Root Not Found'
)
const browserDynamic404 = await next.browser(
'/dynamic-layout-without-not-found/123'
)
expect(await browserDynamic404.elementByCss('#page').text()).toBe(
'dynamic-layout-without-not-found [id]'
)
}) })
if (isNextDev) { if (isNextDev) {
...@@ -74,10 +103,7 @@ createNextDescribe( ...@@ -74,10 +103,7 @@ createNextDescribe(
const browser = await next.browser('/') const browser = await next.browser('/')
await check(() => browser.elementByCss('h1').text(), 'My page') await check(() => browser.elementByCss('h1').text(), 'My page')
await next.renameFile('./app/page.js', './app/foo.js') await next.renameFile('./app/page.js', './app/foo.js')
await check( await check(() => browser.elementByCss('h1').text(), 'Root Not Found')
() => browser.elementByCss('h1').text(),
'This Is The Not Found Page'
)
await next.renameFile('./app/foo.js', './app/page.js') await next.renameFile('./app/foo.js', './app/page.js')
await check(() => browser.elementByCss('h1').text(), 'My page') await check(() => browser.elementByCss('h1').text(), 'My page')
}) })
...@@ -86,7 +112,7 @@ createNextDescribe( ...@@ -86,7 +112,7 @@ createNextDescribe(
if (!isNextDev && !isEdge) { if (!isNextDev && !isEdge) {
it('should create the 404 mapping and copy the file to pages', async () => { it('should create the 404 mapping and copy the file to pages', async () => {
const html = await next.readFile('.next/server/pages/404.html') const html = await next.readFile('.next/server/pages/404.html')
expect(html).toContain('This Is The Not Found Page') expect(html).toContain('Root Not Found')
expect( expect(
await next.readFile('.next/server/pages-manifest.json') await next.readFile('.next/server/pages-manifest.json')
).toContain('"pages/404.html"') ).toContain('"pages/404.html"')
......
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