-
Notifications
You must be signed in to change notification settings - Fork 27k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reset not-found and error boundary when navigating (#49855)
## What? Currently the error boundary state does not reset when a navigation happens because the `key` does not change. Because of that navigating using next/link when the error boundary is in the open state ends up not rendering the contents of the new navigation, instead it keeps the error boundary contents. ## How? This PR uses a pattern that is recommended in the React docs (setState during render) to reset the error boundary if the `usePathname` value has changed, it is used as a proxy for a new navigation. Fixes #47862 Fixes NEXT-963 Fixes #49736 Fixes #49732 Fixes #49599 <!-- Thanks for opening a PR! Your contribution is much appreciated. To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below. Choose the right checklist for the change(s) that you're making: ## For Contributors ### Improving Documentation or adding/fixing Examples - The "examples guidelines" are followed from our contributing doc https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md - Make sure the linting passes by running `pnpm build && pnpm lint`. See https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md ### Fixing a bug - Related issues linked using `fixes #number` - Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ### Adding a feature - Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. (A discussion must be opened, see https://github.com/vercel/next.js/discussions/new?category=ideas) - Related issues/discussions are linked using `fixes #number` - e2e tests added (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs - Documentation added - Telemetry added. In case of a feature if it's used or not. - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ## For Maintainers - Minimal description (aim for explaining to someone not on the team to understand the PR) - When linking to a Slack thread, you might want to share details of the conclusion - Link both the Linear (Fixes NEXT-xxx) and the GitHub issues - Add review comments if necessary to explain to the reviewer the logic behind a change ### What? ### Why? ### How? Closes NEXT- Fixes # -->
- Loading branch information
1 parent
e5eab8b
commit babe503
Showing
15 changed files
with
198 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
test/e2e/app-dir/error-boundary-and-not-found-linking/app/error.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use client' | ||
import Link from 'next/link' | ||
|
||
export default function ErrorComponent() { | ||
return ( | ||
<> | ||
<h1 id="error-component">Error Happened!</h1> | ||
<Link href="/result" id="to-result"> | ||
To Result | ||
</Link> | ||
</> | ||
) | ||
} |
7 changes: 7 additions & 0 deletions
7
test/e2e/app-dir/error-boundary-and-not-found-linking/app/layout.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default function Root({ children }: { children: React.ReactNode }) { | ||
return ( | ||
<html> | ||
<body>{children}</body> | ||
</html> | ||
) | ||
} |
12 changes: 12 additions & 0 deletions
12
test/e2e/app-dir/error-boundary-and-not-found-linking/app/not-found.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import Link from 'next/link' | ||
|
||
export default function NotFound() { | ||
return ( | ||
<> | ||
<h1 id="not-found-component">Not Found!</h1> | ||
<Link href="/result" id="to-result"> | ||
To Result | ||
</Link> | ||
</> | ||
) | ||
} |
3 changes: 3 additions & 0 deletions
3
test/e2e/app-dir/error-boundary-and-not-found-linking/app/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function Page() { | ||
return <h1 id="homepage">Home</h1> | ||
} |
3 changes: 3 additions & 0 deletions
3
test/e2e/app-dir/error-boundary-and-not-found-linking/app/result/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function Page() { | ||
return <h1 id="result-page">Result Page!</h1> | ||
} |
7 changes: 7 additions & 0 deletions
7
test/e2e/app-dir/error-boundary-and-not-found-linking/app/trigger-404/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { notFound } from 'next/navigation' | ||
|
||
export const dynamic = 'force-dynamic' | ||
|
||
export default function Page() { | ||
notFound() | ||
} |
5 changes: 5 additions & 0 deletions
5
test/e2e/app-dir/error-boundary-and-not-found-linking/app/trigger-error/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export const dynamic = 'force-dynamic' | ||
|
||
export default function Page() { | ||
throw new Error('This is an error') | ||
} |
39 changes: 39 additions & 0 deletions
39
...app-dir/error-boundary-and-not-found-linking/error-boundary-and-not-found-linking.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { createNextDescribe } from 'e2e-utils' | ||
|
||
createNextDescribe( | ||
'not-found-linking', | ||
{ | ||
files: __dirname, | ||
}, | ||
({ next }) => { | ||
it('should allow navigation on not-found', async () => { | ||
const browser = await next.browser('/trigger-404') | ||
expect(await browser.elementByCss('#not-found-component').text()).toBe( | ||
'Not Found!' | ||
) | ||
|
||
expect( | ||
await browser | ||
.elementByCss('#to-result') | ||
.click() | ||
.waitForElementByCss('#result-page') | ||
.text() | ||
).toBe('Result Page!') | ||
}) | ||
|
||
it('should allow navigation on error', async () => { | ||
const browser = await next.browser('/trigger-error') | ||
expect(await browser.elementByCss('#error-component').text()).toBe( | ||
'Error Happened!' | ||
) | ||
|
||
expect( | ||
await browser | ||
.elementByCss('#to-result') | ||
.click() | ||
.waitForElementByCss('#result-page') | ||
.text() | ||
).toBe('Result Page!') | ||
}) | ||
} | ||
) |
10 changes: 10 additions & 0 deletions
10
test/e2e/app-dir/error-boundary-and-not-found-linking/next.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* @type {import('next').NextConfig} | ||
*/ | ||
const nextConfig = { | ||
typescript: { | ||
ignoreBuildErrors: true, | ||
}, | ||
} | ||
|
||
module.exports = nextConfig |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,15 @@ | ||
import Link from 'next/link' | ||
import styles from './style.module.css' | ||
|
||
export default function NotFound() { | ||
return ( | ||
<h1 id="not-found-component" className={styles.red}> | ||
Not Found! | ||
</h1> | ||
<> | ||
<h1 id="not-found-component" className={styles.red}> | ||
Not Found! | ||
</h1> | ||
<Link href="/not-found/result" id="to-result"> | ||
To Result | ||
</Link> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function Page() { | ||
return <h1 id="result-page">Result Page!</h1> | ||
} |
1 change: 0 additions & 1 deletion
1
test/e2e/app-dir/navigation/app/not-found/servercomponent/page.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { notFound } from 'next/navigation' | ||
|
||
export const dynamic = 'force-dynamic' | ||
|
||
export default function Page() { | ||
notFound() | ||
return <></> | ||
} |