Allow scroll-to-top with shallow routing
See original GitHub issueWhat version of Next.js are you using?
10.0.6-canary.9
What version of Node.js are you using?
14.15.4
What browser are you using?
Firefox 84
What operating system are you using?
Ubuntu 20.04
How are you deploying your application?
Vercel
Describe the Bug
Recently (i think 10.0.6-canary.8) the behavior for shallow routing was changed to not scroll-to-top anymore. While this makes sense as a default behavior, it should still be possible to force scrolling by providing <Link shallow scroll>
or router.push(path, undefined, { shallow: true, scroll: true )
.
Expected Behavior
<Link shallow scroll>
and router.push(path, undefined, { shallow: true, scroll: true )
should scroll to top.
To Reproduce
// pages/index.js
import Link from "next/link";
export default function Home() {
return (
<main>
<h1>Scroll down</h1>
<div style={{ marginTop: "120vh", padding: "60px" }}>
<Link href={{ query: { filter: "new" } }} shallow scroll>
<a>Click me</a>
</Link>
</div>
</main>
);
}
Issue Analytics
- State:
- Created 3 years ago
- Reactions:8
- Comments:12 (6 by maintainers)
Top Results From Across the Web
Next.js - router.push without scrolling to the top - Stack Overflow
router.push has a scroll option, it is true by default. You can turn it off like this: ... and preventing scroll to top,...
Read more >Shallow Routing - Next.js
Shallow routing allows you to change the URL without running data fetching methods again, that includes getServerSideProps , getStaticProps , and ...
Read more >Introduction to Next.js Link component with examples | refine
Here's a good example showing how to use shallow routing in your web pages. Conclusion. The Next.js Link component simplifies client-side ...
Read more >Scroll Restoration - React Router v5
In earlier versions of React Router we provided out-of-the-box support for ... This is straightforward to handle with a <ScrollToTop> component that will ......
Read more >Add query param to same url without scrolling to the top - Reddit
The shallow routing only works if it stays on the exact same page. ... I had a global scroll to top function because...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I experienced this today as well. Although there are cases where with shallow routing one might not want the page to scroll up, I think there are some where it’s convenient. For example, using shallow routing to change pages via query string params (for frontend pagination without refetching data).
The docs currently say that the default behavior is to obey scroll to top, but this is not happening with
shallow
set totrue
at the moment.IMO the scroll option should just take precedence over opinionated default behavior when it’s explicitly set.