trevorr / react-scroll-manager

Scroll position manager for React applications
ISC License
36 stars 13 forks source link

First page load with hash doesn't work #11

Closed soerenbf closed 4 years ago

soerenbf commented 4 years ago

I guess the history.listen event isn't fired on load, but only successive navigations? Anyway, maybe we could check for location.hash on the initial restoreWindow call on ScrollManager?

I'll see if I can get around to submitting a PR, and you can have a look @trevorr ?

trevorr commented 4 years ago

Could you describe a little more about the scenario that isn't working? Maybe it's obvious, but I haven't looked at this code in awhile.

soerenbf commented 4 years ago

Of course! The app I'm working on is server side rendered, thus the entire DOM for the first page is available on first paint in the browser. What then happens is:

  1. WindowScroller mounts
  2. Calls _restoreWindow on ScrollManager
  3. Restores position (or 0,0), thereby replacing the scroll position set by the browser from location.hash

If you have a look at the PR #12 , the change is pretty simple, and will only have an effect hash is set on location.

trevorr commented 4 years ago

Thanks for the explanation. I've never used SSR myself, but that makes sense. I was worried about how this might affect deferred scrolling of hash links to dynamically rendered content, but after reviewing the code, it doesn't attempt to do that on initial mount anyway. I've merged the change and will get a new release out soon.

trevorr commented 4 years ago

Version 1.0.3 includes this fix.

soerenbf commented 4 years ago

Awesome! Thanks!