NuxtLink - scrollBehavior broken by back navigation
See original GitHub issueEnvironment
- Operating System:
Linux
- Node Version:
v16.14.2
- Nuxt Version:
3.0.0-rc.3
- Package Manager:
npm@7.17.0
- Builder:
vite
- User Config:
-
- Runtime Modules:
-
- Build Modules:
-
Reproduction
https://stackblitz.com/edit/nuxt-starter-xkacmx?file=app/router.options.ts
-
Scroll the main page a little bit.
-
go to “Long Page”
-
go back - the "savedPosition is correct.
-
now go to “Short Page”
-
go back - the home-page scrolled to the top.
Describe the bug
If the page, linked by NuxtLink has shorter content as the original page - by navigation back to original page the router not accepting the savedPosition
(the savedPosition showing correctly by console.log(savedPosition) ) . So the original page is then scrolling to the top.
Expected behavior would be that original-page is staying by “savedPosition”.
This Router settings where used:
import type { RouterConfig } from '@nuxt/schema';
// https://router.vuejs.org/api/#routeroptions
export default <RouterConfig>{
scrollBehavior(to, from, savedPosition) {
if (savedPosition) {
console.log(savedPosition);
return savedPosition;
} else {
return { top: 0, left: 0 };
}
},
};
Additional context
here is the proof that this is working with plain vue router - https://stackblitz.com/edit/vue-8fe51f?file=src/router.js but I am wondering what’s then wrong with NuxtLink ?
Logs
No response
Issue Analytics
- State:
- Created a year ago
- Comments:12 (2 by maintainers)
Top GitHub Comments
I have the same issue here. Although it persists in rc-3 it was already there in rc-1. I’m using a similar solution but it is kinda glitchy. The
scrollBehavior()
is called before the route changes causing the page to scroll to the top before it goes to another page, hence the glitchy experience.Great. Closing this. If you do encounter a problem with edge, please let me know and I’ll happily reopen.