Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix group routes custom root not-found #54931

Merged
merged 4 commits into from
Sep 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions packages/next/src/build/webpack/loaders/next-app-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ async function createTreeCodeFromPath(
const isNotFoundRoute = page === '/_not-found'
const isDefaultNotFound = isAppBuiltinNotFoundPage(pagePath)
const appDirPrefix = isDefaultNotFound ? APP_DIR_ALIAS : splittedPath[0]
const hasRootNotFound = await resolver(
`${appDirPrefix}/${FILE_TYPES['not-found']}`
)
const pages: string[] = []

let rootLayout: string | undefined
Expand Down Expand Up @@ -321,15 +324,18 @@ async function createTreeCodeFromPath(
)

// Add default not found error as root not found if not present
const hasRootNotFound = definedFilePaths.some(
const hasNotFoundFile = definedFilePaths.some(
([type]) => type === 'not-found'
)
// If the first layer is a group route, we treat it as root layer
const isFirstLayerGroupRoute =
segments.length === 1 &&
subSegmentPath.filter((seg) => isGroupSegment(seg)).length === 1
if ((isRootLayer || isFirstLayerGroupRoute) && !hasRootNotFound) {
definedFilePaths.push(['not-found', defaultNotFoundPath])
if ((isRootLayer || isFirstLayerGroupRoute) && !hasNotFoundFile) {
// If you already have a root not found, don't insert default not-found to group routes root
if (!(hasRootNotFound && isFirstLayerGroupRoute)) {
definedFilePaths.push(['not-found', defaultNotFoundPath])
}
}

if (!rootLayout) {
Expand Down
1 change: 0 additions & 1 deletion test/e2e/app-dir/not-found-default/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ createNextDescribe(

await browser.loadPage(next.url + '/group-dynamic/404')
expect(await browser.elementByCss('.next-error-h1').text()).toBe('404')
// Using default layout
expect(await browser.elementByCss('html').getAttribute('class')).toBe(
'group-root-layout'
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { notFound } from 'next/navigation'

export const dynamic = 'force-dynamic'

export default function Page({ params }) {
if (params.id === '404') {
notFound()
}

return <p>{`group-dynamic [id]`}</p>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default function RootLayout({ children }) {
return (
<html>
<head />
<body>
<h1>Root layout</h1>
{children}
</body>
</html>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function NotFound() {
return <p>Not found placeholder</p>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Home() {
return <p>Home</p>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { createNextDescribe } from 'e2e-utils'

createNextDescribe(
'app dir - group routes with root not-found',
{
files: __dirname,
skipDeployment: true,
},
({ next }) => {
it('should render default 404 with root layout for non-existent page', async () => {
const browser = await next.browser('/non-existent')
expect(await browser.elementByCss('p').text()).toBe(
'Not found placeholder'
)
expect(await browser.elementByCss('h1').text()).toBe('Root layout')
})

it('should render root not found for group routes if hit 404', async () => {
const browser = await next.browser('/group-dynamic/123')
expect(await browser.elementByCss('p').text()).toBe('group-dynamic [id]')

await browser.loadPage(next.url + '/group-dynamic/404')
expect(await browser.elementByCss('p').text()).toBe(
'Not found placeholder'
)
expect(await browser.elementByCss('h1').text()).toBe('Root layout')
})
}
)
Loading