question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NuxtLink - scrollBehavior broken by back navigation

See original GitHub issue

Environment


  • 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:closed
  • Created a year ago
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
gerbenvanlaarcommented, May 18, 2022

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.

0reactions
danielroecommented, Oct 27, 2022

Great. Closing this. If you do encounter a problem with edge, please let me know and I’ll happily reopen.

Read more comments on GitHub >

github_iconTop Results From Across the Web

anchors do not work · Issue #5359 · nuxt/nuxt.js
click anchored link; scroll back up; click same anchored link ... a route navigation hasn't happened so scrollBehavior is never called.
Read more >
ScrollBehavior doesn't work properly in nuxt js
scrollBehavior.js for scroll position, but when I press browser back button, savedPosition doesn't call because fetch api has not completed.
Read more >
Nuxt.js | How to retain Scroll Position when returning to page ...
And then change Nuxt's scrollBehavior to when the scrollPos object exists in the ... and back-navigation from other pages to use the browser...
Read more >
r/Nuxt - Why doesn't my page scroll to top every time I click ...
I'm using Nuxt3 RC and <NuxtLink> ... to retain scroll position when using the back button and scroll to the top when navigating...
Read more >
Vue Router Scroll Behavior - Hello There!
Full native-like scroll behavior for Vue or Nuxt apps. ... Sometimes, when using Vue Router, we can get lost while navigating inside our...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found