Scroll Restoration Bug
Unanswered
English Angora posted this in #help-forum
English AngoraOP
Hello, on nextjs i have a normal project using tailwind, and a few other things. For a landing.
And it's happening a problem that's basically on back and forward in buttons of browser the page is not loading to previous state of scroll, it's just going to top.
I tried to put the experimental { scrollRestoration: true} and it worked but now the refresh of page in refresh button always go to top, instead of going to the previous location. I want to have the same behavior as a lot of other landings, that those buttons on browser make page go to previous location
And it's happening a problem that's basically on back and forward in buttons of browser the page is not loading to previous state of scroll, it's just going to top.
I tried to put the experimental { scrollRestoration: true} and it worked but now the refresh of page in refresh button always go to top, instead of going to the previous location. I want to have the same behavior as a lot of other landings, that those buttons on browser make page go to previous location
49 Replies
English AngoraOP
I use <Link> and router.push around the app
English AngoraOP
Help pls
English AngoraOP
If any can help here kinda stuck on this for a good time, sorry to bother
Southern yellowjacket
Please do not use the moderator ping for code help
It's only to be used for situations that require moderators to step in
English AngoraOP
Oh sorry, didn't know
Giant panda
Also pushing your thread is allowed only once per 24h.
@English Angora If any can help here kinda stuck on this for a good time, sorry to bother
English AngoraOP
Won't happen again
English AngoraOP
Pushing this thread again to check if anybody can help solve
and call the hook in your entry file e.g _app.js
English AngoraOP
I have a code kinda like that but that also makes the normal links of the page, work like refresh page. Where if you change page it goes to the position it was before. Also that one don't work on refresh like the one i'm using.
Basically i want to have the normal behavior of a website, that is. Back/forward goes to previous position, refresh page algo goes to previous position. Links on website goes to begin of the page they had route to.
I'm struggling with having the two last points, because i can't destinguish the refresh of the browser from the link to a page redirect
Basically i want to have the normal behavior of a website, that is. Back/forward goes to previous position, refresh page algo goes to previous position. Links on website goes to begin of the page they had route to.
I'm struggling with having the two last points, because i can't destinguish the refresh of the browser from the link to a page redirect
@English Angora I have a code kinda like that but that also makes the normal links of the page, work like refresh page. Where if you change page it goes to the position it was before. Also that one don't work on refresh like the one i'm using.
Basically i want to have the normal behavior of a website, that is. Back/forward goes to previous position, refresh page algo goes to previous position. Links on website goes to begin of the page they had route to.
I'm struggling with having the two last points, because i can't destinguish the refresh of the browser from the link to a page redirect
check this app out. what behaviour is missing? - https://sitesnap.design
also, do you have examples of websites that go back to previous position on refresh?
English AngoraOP
when i refresh it goes to top of page
It usually doesn't do that
Instead of where i was before
they're most likely using local/session storage
English AngoraOP
Not really using only <a> instead of <Link> does that behavior automatically
Without having this type of codes we are using
highly doubt that
English AngoraOP
Try it, already did it, and have done in some sites out of nextjs
It's normal, and i can send lot of examples
And to do what you have in your website, if it is nextjs just use the config experimental: {scrollRestoration: true} works the same, but refresh doesn't work as i want anyway
this is a example of what i want on refresh
@English Angora https://www.pixelmatters.com/work/quantic-website-design-development
this website is rendered on the client that's why it kind of works out of the box. try disabling Javascript and you'll see a blank screen.
normally, the browser would attempt to restore scroll position automatically
but if you're working with SSR content where the browser may not have the initial scroll position during initial load, then it may not work as expected. hence, why you may have to use custom localStorage/session logic
normally, the browser would attempt to restore scroll position automatically
but if you're working with SSR content where the browser may not have the initial scroll position during initial load, then it may not work as expected. hence, why you may have to use custom localStorage/session logic
English AngoraOP
I was trying to use localStorage logic too, but my problem is that i can't destinguish refresh from browser from a redirect on page with a link on navbar for example. The window events assume they are the same
ah i see
let's ask chatGPT
English AngoraOP
Last one i think, 13.4.5
it looks like the refresh thing works out of the box with Next 13 - https://codesandbox.io/p/sandbox/infallible-williamson-q7vlpt?file=%2Fpackage.json%3A8%2C27
how come it's not working for you
English AngoraOP
Hum, you sure? That's weird if it works, i don't have nothing abnormal in project, just simple html static props and server side on one page :x When i started adding <Links> stopped working
it works. just checked a project i was working on locally and it works too
English AngoraOP
Well, gonna need to check wth is happening
But it works with the back and forward too on those projects?
Both refresh, back/forward works as intended?
They go back to their positions
English AngoraOP
Yeah that's the problem, when you put back/forward working the refresh will turn off
Because you need to change scrollHistory to manual, and that will make the refresh not work
Gonna need to do a localStorage kinda hacky thing probably
yeah. and with the changes in how the
useRouter works, it may even be trickyi wish you all the best man 😄
English AngoraOP
Yeah it will, but maybe will find a way.
Thanks a lot for real
And you too, have a good night