Unverified Commit 30521f20 authored by Jiwon Choi's avatar Jiwon Choi Committed by GitHub

fix(next): global not-found not working on multi-root layouts (#63053)

## Why?

For multi-root layouts (route groups on the root with their root
layouts, no layout file on the root), it is not possible to use global
`not-found` since the `layout` is missing on the root.

```sh
.
└── app/
    ├── (main)/
    │   └── layout.js
    ├── (sub)/
    │   └── layout.js
    └── not-found.js --> ERR: missing layout
```

Current Behavior:

```sh
not-found.js doesn't have a root layout. To fix this error, make sure every page has a root layout.
```

## What?

Let multi-root layouts also benefit from the global `not-found`.

## How?

Wrap root `not-found` with default layout if root layout does not exist.
Although this solution is not `multi-root` specific, it won't produce
critical issues since a root `layout` is required for other cases.

Fixes #55191 #54980 #59180
parent 97b4a99b
...@@ -200,9 +200,10 @@ async function createTreeCodeFromPath( ...@@ -200,9 +200,10 @@ async function createTreeCodeFromPath(
const isDefaultNotFound = isAppBuiltinNotFoundPage(pagePath) const isDefaultNotFound = isAppBuiltinNotFoundPage(pagePath)
const appDirPrefix = isDefaultNotFound ? APP_DIR_ALIAS : splittedPath[0] const appDirPrefix = isDefaultNotFound ? APP_DIR_ALIAS : splittedPath[0]
const hasRootNotFound = await resolver( const rootNotFound = await resolver(
`${appDirPrefix}/${FILE_TYPES['not-found']}` `${appDirPrefix}/${FILE_TYPES['not-found']}`
) )
const hasRootNotFound = Boolean(rootNotFound)
const pages: string[] = [] const pages: string[] = []
let rootLayout: string | undefined let rootLayout: string | undefined
...@@ -382,7 +383,8 @@ async function createTreeCodeFromPath( ...@@ -382,7 +383,8 @@ async function createTreeCodeFromPath(
)?.[1] )?.[1]
rootLayout = layoutPath rootLayout = layoutPath
if (isDefaultNotFound && !layoutPath && !rootLayout) { const isRootNotFound = hasRootNotFound && isRootLayer && isNotFoundRoute
if ((isRootNotFound || isDefaultNotFound) && !layoutPath) {
rootLayout = defaultLayoutPath rootLayout = defaultLayoutPath
definedFilePaths.push(['layout', rootLayout]) definedFilePaths.push(['layout', rootLayout])
} }
......
export default function NotFound() {
return <p>not found</p>
}
import { notFound } from 'next/navigation'
export default function Page() {
notFound()
return <p>hello world</p>
}
import { nextTestSetup } from 'e2e-utils'
describe('root-not-found-missing-root-layout', () => {
const { next, isTurbopack } = nextTestSetup({
files: __dirname,
})
// Skip test for turbo dev for now since generating a missing root layout is not supported yet.
// See https://github.com/vercel/next.js/pull/63053#issuecomment-1987101666
;(isTurbopack ? it.skip : it)(
'should not conflict with generated layout on dev server',
async () => {
const browser = await next.browser('/')
// eslint-disable-next-line jest/no-standalone-expect
expect(await browser.elementByCss('p').text()).toBe('not found')
}
)
})
export default function MainRoot({ children }: { children: React.ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
export default function MaiNotFound() {
return <h1>Main Not Found</h1>
}
import { notFound } from 'next/navigation'
export default function Main() {
notFound()
return <h1>Main</h1>
}
export default function SubRoot({ children }: { children: React.ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
export default function SubNotFound() {
return <h1>Sub Not Found</h1>
}
import { notFound } from 'next/navigation'
export default function Sub() {
notFound()
return <h1>Sub</h1>
}
export default function RootNotFound() {
return <h1>Root Not Found</h1>
}
import { nextTestSetup } from 'e2e-utils'
describe('not-found-multi-root-layout', () => {
const { next } = nextTestSetup({
files: __dirname,
})
it('should render main not-found', async () => {
const browser = await next.browser('/')
expect(await browser.elementByCss('h1').text()).toBe('Main Not Found')
})
it('should render sub not-found', async () => {
const browser = await next.browser('/sub')
expect(await browser.elementByCss('h1').text()).toBe('Sub Not Found')
})
it('should render root not-found for uncaught routes', async () => {
const browser = await next.browser('/404')
expect(await browser.elementByCss('h1').text()).toBe('Root Not Found')
})
})
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