-
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.
Fix Router Error Events in Shallow Routing by Skipping cancelHandler …
…Creation (#61771) ### Problem We've identified a bug within Next.js's pages router when utilizing the shallow routing API, specifically when invoking `router.push` with the `{ shallow: true }` option, like so: ```javascript router.push('/?counter=10', undefined, { shallow: true }); ``` Shallow routing is designed to update the URL without running data fetching methods such as getServerSideProps, getStaticProps, or getInitialProps. However, a side effect of this process is that it skips the clean-up of the cancelHandler. This leads to router error events being fired erroneously, causing confusion and potential stability issues, as the system behaves as if an error occurred when, in fact, none did. ### Solution This PR addresses the issue by modifying the shallow routing logic to also skip the creation of the cancelHandler. Given that shallow routing operations are synchronous and do not involve data fetching or other asynchronous tasks that might need to be canceled, the cancelHandler is unnecessary in this context. fixes #61772 --------- Co-authored-by: Shu Ding <g@shud.in>
- Loading branch information
Showing
3 changed files
with
60 additions
and
2 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
42 changes: 42 additions & 0 deletions
42
test/development/pages-dir/client-navigation/fixture/pages/nav/query-only-shallow.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,42 @@ | ||
import Link from 'next/link' | ||
import { useRouter } from 'next/router' | ||
import { useEffect, useState } from 'react' | ||
|
||
export default function Page() { | ||
const router = useRouter() | ||
|
||
const [routeState, setRouteResult] = useState({ | ||
completed: 0, | ||
errors: 0, | ||
}) | ||
useEffect(() => { | ||
const increaseErrorCount = () => | ||
setRouteResult((state) => ({ ...state, errors: state.errors + 1 })) | ||
const increaseRouteComplete = () => | ||
setRouteResult((state) => ({ ...state, completed: state.completed + 1 })) | ||
router.events.on('routeChangeError', increaseErrorCount) | ||
router.events.on('routeChangeComplete', increaseRouteComplete) | ||
|
||
return () => { | ||
router.events.off('routeChangeError', increaseErrorCount) | ||
router.events.off('routeChangeComplete', increaseRouteComplete) | ||
} | ||
}) | ||
|
||
return ( | ||
<> | ||
<div id="routeState">{JSON.stringify(routeState)}</div> | ||
<Link href="?prop=foo" id="link" shallow={true}> | ||
Click me | ||
</Link> | ||
<button | ||
id="router-replace" | ||
onClick={() => { | ||
router.replace('?prop=baz', undefined, { shallow: true }) | ||
}} | ||
> | ||
Push me | ||
</button> | ||
</> | ||
) | ||
} |
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