Unverified Commit 4007b6bc authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Fix navigation from global-not-found to app routes (#100592)

Links from `global-not-found` to app routes can leave users stuck on the
404 page because the router does not recognize that leaving the global
404 changes the root layout.

Treat navigation away from `global-not-found` as a root layout change
and perform a full-page navigation. Add an e2e regression test covering
a link back to the home page.

<!-- NEXT_JS_LLM -->
parent 3b658c96
...@@ -363,13 +363,14 @@ function updateRenderTreeOnNavigation( ...@@ -363,13 +363,14 @@ function updateRenderTreeOnNavigation(
// case, because it acts like a root layout, but in the router tree, it // case, because it acts like a root layout, but in the router tree, it
// is rendered in the same position as app/layout.tsx. // is rendered in the same position as app/layout.tsx.
// //
// Any navigation to the global Not Found route should trigger a // Any navigation to or from the global Not Found route should trigger a
// full-page navigation. // full-page navigation.
// //
// TODO: We should probably model this by changing the key of the root // TODO: We should probably model this by changing the key of the root
// segment when this happens. Then the root layout check would work // segment when this happens. Then the root layout check would work
// as expected, without a special case. // as expected, without a special case.
newSegment === NOT_FOUND_SEGMENT_KEY newSegment === NOT_FOUND_SEGMENT_KEY ||
oldRenderTree.segment === NOT_FOUND_SEGMENT_KEY
) { ) {
return null return null
} }
......
import { Client } from './client' import { Client } from './client'
import Link from 'next/link'
export default function GlobalNotFound() { export default function GlobalNotFound() {
return ( return (
...@@ -7,6 +8,7 @@ export default function GlobalNotFound() { ...@@ -7,6 +8,7 @@ export default function GlobalNotFound() {
<body> <body>
<h1 id="global-error-title">global-not-found</h1> <h1 id="global-error-title">global-not-found</h1>
<Client /> <Client />
<Link href="/">Return home</Link>
</body> </body>
</html> </html>
) )
......
...@@ -28,6 +28,18 @@ describe('global-not-found - basic', () => { ...@@ -28,6 +28,18 @@ describe('global-not-found - basic', () => {
expect(notFoundHtmlProp).toBe('true') expect(notFoundHtmlProp).toBe('true')
}) })
it('should navigate from global-not-found to the app root layout', async () => {
const browser = await next.browser('/does-not-exist')
await browser.elementByCss('a[href="/"]').click()
expect(await browser.elementByCss('p').text()).toBe('hello world')
expect(await browser.elementByCss('html').getAttribute('lang')).toBe('en')
expect(
await browser.elementByCss('html').getAttribute('data-global-not-found')
).toBeNull()
})
it('should render not-found boundary when calling notFound() in a page', async () => { it('should render not-found boundary when calling notFound() in a page', async () => {
const browser = await next.browser('/call-not-found') const browser = await next.browser('/call-not-found')
// Still using the root layout // Still using the root layout
......
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