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.

"shouldUpdateScroll" "getSavedScrollPosition" always returns NULL?

See original GitHub issue

Summary

When trying to get the scroll position with getSavedScrollPosition in gatsby-browser.js the output of console.log is always null. I’m not sure if this is correct?

I’m trying to exclude one page/route from the standard „start at top of the page“-behaviour.
The reason for this is that I have a long list of articles on this page. When the user clicks on a <Link> somewhere down on this list, he is taken to the top of the article (which is wanted in this case). But when he then clicks the close/back-link of the on-page-navigation he is taken back to the top of the listing-page and has to scroll down all the way again to where he left off. (When he uses the back-button of the browser, instead of the on-page-navigation, he is correctly taken back to right scroll position of the previous page.) I opted out of using browser-history-api because I also have next/prev-links on the article and want the user to always return to the listing-page when he is using the close/back-link.

Steps to reproduce

Go to this Gatsby-Starter-Sandbox, reload the preview. Open up the console below and navigate the page. (Beside editing gatsby-browser.js I only added some paragraphs and changed headlines on the pages)

Changing routerProps to prevRouterProps will return the right coordinates for currentPosition from SessionStorage. But not for queriedPosition.

Expected result

const queriedPosition = getSavedScrollPosition({ pathname: `/page-2/` })

should give an Array of the last saved coordinates from the given location. As described in the shouldUpdateScroll documentation which then can be used to scroll to the right position on the given route.

Actual result

const queriedPosition = getSavedScrollPosition({ pathname: `/page-2/` })`

returns null. (I also tried /page-2, and even page-2, or /)

Therefore every new page is scrolled to top on navigation, even when trying to go to the last scroll position of a queried route via window.scrollTo(...(queriedPosition || [0, 0])).

SessionStorage is populated with the right saved and actual @@scroll-values.

File contents (if changed)

gatsby-config.js: N/A package.json: N/A gatsby-node.js: N/A gatsby-browser.js:

exports.shouldUpdateScroll = ({
  routerProps: { location },
  // routerProps returning NULL.
  // -------------
  // Using prevRouterProps returns scroll coordinates of last route
  // comment out next line for demonstration
  // prevRouterProps: { location },
  getSavedScrollPosition
}) => {
  const currentPosition = getSavedScrollPosition(location)
  const queriedPosition = getSavedScrollPosition({ pathname: `/page-2/` })

  console.log("currentPosition result:", currentPosition)
  console.log("queriedPosition result:", queriedPosition)

  //window.scrollTo(...(queriedPosition || [0, 0]))

  return false
}

gatsby-ssr.js: N/A

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
adamschwartzcommented, May 23, 2020

@blainekasten Thank you!

I had the same issue and your suggestion worked for me.

It looks like the docs for shouldUpdateScroll may be incorrect then:

image

I suspect that’s how OP got their code as well. Hope this helps!

1reaction
magnusarinellcommented, Dec 11, 2020

I couldn’t get it working so I solved it manually doing:

const getLatestSavedScrollPosition = (pathname) => {
    let n = sessionStorage.length;
    let i = 0;

    const partialKey = `@@scroll|${pathname}|`

    let results = [];

    while (++i < n) {
        const key = sessionStorage.key(i);
        
        if (key.includes(partialKey)) {
            results.push(key)
        }
    }

    if (results.length === 0) {
        return 0;
    }

    results.sort();

    return sessionStorage.getItem(results[results.length - 1]);
}

Called from shouldUpdateScroll like this:

        const currentPosition = getLatestSavedScrollPosition(routerProps.location.pathname)

Edit: Code is now available at https://github.com/magnusarinell/istoriez.com

Read more comments on GitHub >

github_iconTop Results From Across the Web

Gatsby Browser APIs
Documentation about leveraging standard browser APIs within Gatsby.
Read more >
Gatsby Browser APIs | GatsbyJS 中文网
getSavedScrollPosition function. Takes a location and returns the coordinates of the last scroll position for that location, or null .
Read more >
How to fix Gatsby JS Link component retaining scroll position ...
I have a feeling it's something to do with my styles, as I started a new project and the Gatsby-cli had no issues....
Read more >
query-state-core - npm Package Health Analysis | Snyk
Looks like query-state-core is missing a security policy. ... warn when value type is not supported (do not warn when null was passed...
Read more >
Getting null while running JS script in browser
I am trying to run JS script on web page but it always returns null any idea what I am missing? I am...
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