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.

Router/PageLoader: Scroll Position is maintained on navigate when PageLoader does not "reload" content

See original GitHub issue

As part of Issue https://github.com/redwoodjs/redwood/pull/1284 to correct the “reload flash” when navigating via Router Links, a decision needs to be made as to how to handle the window’s Scroll Postition.

With change to the Router, the window position is maintained.

This means:

  • Page A - Scroll 2-3 vertical screen heights
  • Navigate to Page B (that also has multiple page screen height)
  • You are now on Page B but at the Position of A (ie, not top)

rw_scroll_position_issue

There is discussion with React Router w/ re: to Scroll Position and Restoration here:

https://reactrouter.com/web/guides/scroll-restoration

as well as part of browser history spec:

https://majido.github.io/scroll-restoration-proposal/history-based-api.html#web-idl

In React Router, they have a component that scrolls to top by setting the window.scrollTo(0, 0):

class ScrollToTop extends React.Component {
  componentDidUpdate(prevProps) {
    if (
      this.props.location.pathname !== prevProps.location.pathname
    ) {
      window.scrollTo(0, 0);
    }
  }

  render() {
    return null;
  }
}

Perhaps this rule (or option) could be added to the PageLoader – perhaps even as a Page or Route option to either a) always scroll to top or b) only scroll to top it the option is set.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
dthyressoncommented, Feb 10, 2021

since I’m doing focus management as well

@jtoar I looked at an older app and realized we had done something really similar to the window.scrollTo for focus:

const Page = ({ children, location }) => {
...
  const node = useRef();

  useEffect(() => {
    node.current.focus();
  }, [location.key]);

I cannot remember exactly what location was … but what I do recall seeing from time to time was some some of timing 😃 issue when setting focus happened perhaps when the input wasn’t 100% ready (this was a year or so ago for this app).

Perhaps, the timing and some defensive programming to ensure the element is ready can help.

1reaction
jtoarcommented, Feb 10, 2021

Awesome discussion and great resources! Just wanted to chime in and say that I can take the lead on this since I’m doing focus management as well.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ActiveRouteLoader navigating to top on page load
Router/PageLoader : Scroll Position is maintained on navigate when PageLoader does not "reload" content. opened 05:16PM - 15 Oct 20 UTC.
Read more >
Refresh Page and Keep Scroll Position - javascript
Usually reload() restores the scroll position afterward, but forced mode can scroll back to the top of the page, as if window.scrollY ===...
Read more >
Maintain vertical scroll position after reloading page? - Twine
Hi! I'm using 2.0.11 / Harlowe. I want to make a game with one very long central passage that contains hundreds of links....
Read more >
Maintain the scroll position on page reload - Telerik
Hi, I am working on an ASP.Net website which is using the latest Telerik version Q3 2012 for Rad Ajax Controls. There are...
Read more >
The scroll position is not maintained - Visual Basic
For example, you must do this if the auto-scrollable Panel control contains a collection of controls that have a specific order. Typically, ...
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