-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
<Link>
components don't work well with browser history navigation
#882
Comments
This was noted by @jameshadfield a while back on Slack. I did some searching and it seems like this is a common problem with client-side navigation, but no clear solution. I'm thinking to apply the workaround of replacing |
@victorlin Here is a little experiment:
I suspect it's this magic:
and also maybe this:
When using a client-side router, any fiddling with history API will very likely break the router. I'd zap this magical solution and search for a package on NPM (if the usual anchor (It might make sense to also review all other overly smart solutions, e.g. involving |
Thanks for the pointer @ivan-aksamentov, I confirmed the first snippet is the culprit. Removing it in #887 While researching this issue, my understanding is that
I tried replacing |
@victorlin
The Next.js If you find that the package cannot be removed, then you could try to replace direct History API manipulation in it with equivalent Next.js router imperative methods - this will ensure that the router is also notified about the changes. For a proper smooth scrolling in modern browsers, I believe that html {
scroll-behavior: smooth;
} should do without any packages. Never heard of built-in smooth scrolling in Next.js (but there might be). I think vercel/next.js@8664ffe you quoted above is a fix for a bug in their router which prevented If a more complex imperative smooth scroll is needed, then there are many hook libraries having this functionality (example). Underneath they are typically using something like this, if need to scroll to element's ref: ref.current.scrollIntoView({ behavior: 'smooth' }) or, if need to scroll to coordinates: window.scrollTo({
top: 100,
left: 100,
behavior: "smooth",
}); (Can you implement your own smooth scroll based on these simple functions? Sure. Will it work? 80% of time, yes. For the remaining 20% you'd spent the next 5 years finding bugs, fixing them, then chasing random breakages in Safari, Edge and mobile browsers. If there's at least 1000 programmers actively using your package and reporting issues back, then you could be done in just 2 years :)) P.S. You likely know that very well already, but I'll mention it just in case. Note that there are differences between the older "pages" router and the newer "app" router. For example, above you linked docs from app router, but my understanding that current setup is using The differences between "pages" and "app" setups, other than the obvious replacement of |
You're probably right. I'll write up another issue to consider this: #888
@tsibley noted the difference in #824 (comment) but I forgot and didn't realize I was looking at the wrong docs. Thanks for noting! |
Description
Clicking on a
<Link>
then using the browser to navigate to the previous page updates the URL but page contents remain unchanged.How to reproduce
Steps to reproduce the current behavior:
Possible solutions
References
Noted on Slack
The text was updated successfully, but these errors were encountered: