Skip to content
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

Add example for gatsby-browser.js API shouldUpdateScroll #3857

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ Websites built with Gatsby:
* [ERC dEX](https://ercdex.com)
* [iContract Blog](https://blog.icontract.co.uk)
* [Dona Rita](https://donarita.co.uk) ([source](https://github.com/peduarte/dona-rita-website))
* [Andy Slezak](https://www.aslezak.com) ([source](https://github.com/amslezak))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you do this in a separate PR? Thanks!


## Docs

Expand Down
5 changes: 5 additions & 0 deletions packages/gatsby/src/utils/api-browser-docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ exports.onRouteUpdate = true
* @param {object} $0
* @param {object} $0.prevRouterProps The previous state of the router before the route change.
* @param {object} $0.pathname The new pathname
* @example
* exports.shouldUpdateScroll = (prevRouterProps, pathname) => {
* window.scrollTo(0, 0) // upon navigation, scroll to top of page
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious what the use case for doing this would be?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For my particular use-case, I wanted it so when navlinks clicked always start at the top of the page. By default, they start off at the last scrolled position of the page. This example overrides that behavior.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you try upgrading to the latest Gatsby? There was a bug that was causing problems with scrolling that I fixed in #3775

It sounds like you were just running into that bug.

I'd love an example for this API but it should be a realistic one — not one that works around a bug :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood! I'll remove my workaround, and then test. If I come up with a better example, I will absolutely submit. Thanks for the update, Kyle.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just confirming you fixed the issue I was having with your patch.

* return false
* };
*/
exports.shouldUpdateScroll = true

Expand Down