next.js: Router.push('/link') don't scroll top of the page when triggered
Hello,
maybe I am missing something, but when I use this function and redirect to another page scroll doesn’t reset to top but the page loads scrolled to middle, I know why this issue happens, but can I somehow fix it with some flag like JAVASCRIPT Router.pusht({pathname: '/link', scrollreset: true}) or something like this, didn’t find anything similar in documentation
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 10
- Comments: 34 (9 by maintainers)
Commits related to this issue
- Working around https://github.com/vercel/next.js/issues/3249 — committed to teachen-ch/voty by stefnnn 6 years ago
- fix(router): consistent scroll behavior for Link/Router#push This pull request makes `Router#push` and `Router#replace` function identically to `<Link />`, i.e. reset scroll when the new render is com... — committed to Timer/next.js by Timer 6 years ago
- fix(router): consistent scroll behavior for Link/Router#push (#20606) This pull request makes `Router#push` and `Router#replace` function identically to `<Link />`, i.e. reset scroll when the new ren... — committed to vercel/next.js by Timer 6 years ago
I agree there should be a
scrollresetoption. If you don’t want to do it incomponentDidMountyou can also doRouter.push('/link').then(() => window.scrollTo(0, 0));.To make it work globally you can use the built-in next.js router event listener:
Router.events.on('routeChangeComplete', () => { window.scrollTo(0, 0); });. Just place this in a component shared across all pages e.g. the header.Hi. Why would we have the
Linkcomponent default scroll to toptruebutRouter.pushnot?I think this issue should be revisited.
Sorry but tagging the contributors: @liweinan0423 @timneutkens @exogen @goldenshun
Router.push('/link').then(() => window.scrollTo(0, 0));this is the right way to do it, feel free to add it to the readme @gragland ❤️FWIW, here’s my take on a workaround that was shared in a related issue.
Use it somewhere at the top of your component tree (
_app.jsworks).This is fixed on
next@canary!whether it should scroll to top or not is decided by the user. I think you can do it in
componentDidMount@Timer @timneutkens Also, got bitten by the difference in behavior of
LinkandRouter.push. FWIW, +1 to adding to theoptionsofRouter.pushas that would keep it backward compatible.@timneutkens Is there a reason why the default scroll behavior for the imperative
Router.pushand<Link>are different? Seems like an obvious bug to me that they behave differently.@markjackson02 OMHO, the breaking change is that it’s not working that way yet! The wrong behavior’s already made. So I think it would be a good idea to put it the way that it should. But I agree with you, contributors will probably not give attention here.
Here’s a simplified version of @nfantone’s
useRouterScrollhook above without any bells and whistles. 😅I just want to chime in and say I found this behavior extremely surprising. Been building production sites with Next.js for almost a year now and only just now discovered that
Router.pushwasn’t returning to the top – I always thoughtRouter.pushbehaved exactly like<Link>but was just the programmatic way to do it.And in terms of matching default behavior like
<a>vs.<Link>, the non-SPA equivalent ofRouter.pushwould seem to be settingwindow.locationwhich also returns you to the top of the page, no? It would make more sense as the default behavior to me.It would be nice if there was an option on
router.push()to force scroll to top, but I don’t think it should be default as it would be a breaking change for everyone already assuming it does not do this.@Timer Hi, FYI this is not documented in https://nextjs.org/docs/api-reference/next/router#routerpush - Would be good to add there.
@markjackson02 I don’t see how this would be a breaking change since this is the default way browser would have handle this. I do not agree with the fact that we have two different approaches for the same functionality.
Is NOT fixed with
10.0.8-canary.15, atleast not when using a new basePath in next.config.jsRouter.back() doesn’t return promise, but has the same issue, what to do, manually scrolling up?
Woaw… You mean I need to review every single page to add
.then()! By the way, I’m using next-routes, so is there a way to add this globally, just to add this code once, likeaddRequestTransformif you need to add some headers before calling an API.An old-school workaround from me. Just get the particular component in the view. Here’s how. I use it with the router.push(“/myaccount”) for my peace of mind! I’m using react device detect. For some reason, in mobile browsers, it is not going to the top of the page after clicking router.push. The desktop browser is working fine. So, I wrapped my mobile menu bar Whatever happens, my component will be smoothly scrolled to the top! 😅
if you don’t mind using
router.pushafter it being instantiated withuseRouter()you can simply do:{scroll: false}is what prevents scroll to top.You can use this to route and scroll to the top of the page:
Btw, this isn’t as nice of a user experience as the
Linkbehavior, since you scroll to the top of the page immediately instead of when the next page loads.It would definitely be a breaking change. People have been building apps with the way it is now and expect it to function that way.
I’m not against them both having the same default functionality but I don’t think it’s worth the migration effort.
Anyway, this issue is closed so we should open a new issue with whatever we want to do.
Thanks @gralagland your solution seems not so ugly, but it would be nice to have such option to keep DRY principe. The only fast solution for now is to make live template to write this 😅