-
Notifications
You must be signed in to change notification settings - Fork 10.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
shouldUpdateScroll returning Array not working #26169
Comments
Hiya! This issue has gone quiet. Spooky quiet. 👻 We get a lot of issues, so we currently close issues after 60 days of inactivity. It’s been at least 20 days since the last update here. Thanks for being a part of the Gatsby community! 💪💜 |
We're still seeing this issue! We'd hoped that #28555 would address it, but sadly we're still having problems with our scroll situation. |
Here is my workaround (inside exports.shouldUpdateScroll = ({ routerProps }) => {
const isHash = routerProps.location.hash;
const gatsbyWrapper = document.getElementById('gatsby-focus-wrapper');
/* Do not scroll top if the route contain a hash */
gatsbyWrapper && !isHash && (gatsbyWrapper.scrollTop = 0);
return isHash && isHash.replace('#', '');
}; Hope it can help you to bypass the issue |
Hiya! This issue has gone quiet. Spooky quiet. 👻 We get a lot of issues, so we currently close issues after 60 days of inactivity. It’s been at least 20 days since the last update here. Thanks for being a part of the Gatsby community! 💪💜 |
Hey again! It’s been 60 days since anything happened on this issue, so our friendly neighborhood robot (that’s me!) is going to close it. Thanks again for being part of the Gatsby community! 💪💜 |
Description
In the documentation of
shouldUpdateScroll
it says that:However, returning an array won't actually work.
Steps to reproduce
I've dug into this for a while and I think (probably, since I'm not very familiar to gatsby) I've found the actual cause of the problem. Instead, I'll simply write down what I've found during my analysis. If I was wrong, please point me out.
It all started in
root.js
:gatsby/packages/gatsby/cache-dir/root.js
Lines 68 to 72 in 205847b
It imports
shouldUpdateScroll
fromnavigate.js
:gatsby/packages/gatsby/cache-dir/navigation.js
Lines 125 to 151 in 83926c8
It uses
apiRunner
, which I suppose (due to the lack of knowledge about the internals) should collectshouldUpdateScroll
from all plugins, which, in my case, is only provided bygatsby-remark-autolink-headers
.This function is then passed to
ScrollHandler
, which uses it in itsshouldUpdateScroll
instance method:gatsby/packages/gatsby-react-router-scroll/src/scroll-handler.tsx
Lines 91 to 102 in 40724e8
So who uses
this.shouldUpdateScroll
? The answer iswindowScroll
andscrollToHash
:gatsby/packages/gatsby-react-router-scroll/src/scroll-handler.tsx
Lines 71 to 89 in 40724e8
The problem is that both method didn't actually use the return value of
shouldUpdateScroll
, so if the return value is an array, it is simply ignored.Relationship to other issues
My motive is that we wanted to click on markdown headers and update the hash, and found out that sometimes, clicking on a header makes it jump to the screen top, and sometimes it doesn't. That is similar to the behaviour described in #25778.
First I thought that maybe
gatsby-remark-autolink-headers
got something wrong, but it didn't. The actual cause is, again, rooted withingatsby-react-router-scroll
. So incomponentDidUpdate
:gatsby/packages/gatsby-react-router-scroll/src/scroll-handler.tsx
Lines 56 to 69 in 40724e8
This is supposed to be called when the path changes, and tries to restore the user's last scroll position on the new page, however it also gets called when the page hash updates, when it still tries to do the same thing.
That said, clicking on an in-page anchor does two things:
hashchange
event.scroll
event.If the former comes first, the router calls
componentDidUpdate
, which uses the last scroll position and callswindow.scroll
, which somehow 'cancels out' the latter one (I presume that's because the browser sends only onescroll
event per frame), so in the end, nothing changes.If the latter comes first, the page first scrolls, updating last scroll position, and
componentDidUpdate
callswindow.scroll
with the new scroll position, so the page jumps to the desired position.That might solve the myth of #25778, although in my case another weird thing is that my browser (Chrome 84.0.4147.89) seems to stick to an order for an uncertain amount of time, and then switches to another.
The simple solution is to return
false
fromshouldUpdateScroll
when nothing but the hash changes, and let the browser do everything for you, but I'm thinking about preventing the auto-jump behavior of the browser and passing control toshouldUpdateScroll
, however had yet no luck of finding a solution.Hope that'd be of help.
The text was updated successfully, but these errors were encountered: