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.

Anchor Links / Hash in URL is ignored on initial page load

See original GitHub issue

Description

I am having a SPA built with gatsby. I am using <a href="/#sectionX">section x</a> links for navigation in the header. This is working just fine.
Unfortunately, reloading the page (having the anchor link in the URL) does not scroll to the expected section but stays unscrolled at the top of the page.

Steps to reproduce

Take any gatsby starter, add anchor link, reload page.

Expected result

Page should automatically scroll to desired section as provided in the URL on initial page load.

Actual result

Hash is ignored and page does not scroll

Environment

System: OS: Windows 10 10.0.17763 CPU: (8) x64 Intel® Core™ i7-4770 CPU @ 3.40GHz Binaries: Node: 12.13.0 - C:\Program Files\nodejs\node.EXE npm: 6.12.0 - C:\Program Files\nodejs\npm.CMD Languages: Python: 2.7.17 Browsers: Edge: 44.17763.831.0

Issue Analytics

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

github_iconTop GitHub Comments

9reactions
danspratlingcommented, Feb 2, 2020

Looks like gatsby takes control of the scroll position but you can use the gatsby-browser.js file to add a function to re-enable anchor links.

exports.onRouteUpdate = ({ location }) => scrollToAnchor(location)

/**
 *
 * @desc - a function to jump to the correct scroll position
 * @param {Object} location -
 * @param {Number} [mainNavHeight] - the height of any persistent nav -> document.querySelector(`nav`)
 */
function scrollToAnchor(location, mainNavHeight = 0) {
  // Check for location so build does not fail
  if (location && location.hash) {
    const item = document.querySelector(`${location.hash}`).offsetTop

    window.scrollTo({
      top: item - mainNavHeight,
      behavior: "smooth",
    })
  }

  return true
}

I’m not sure if the default browser behaviour for anchor links being disabled is intentional or not though. Would be worth getting some feedback from someone on the internal team.

3reactions
blainekastencommented, Jun 19, 2020

Fixed in latest!

Read more comments on GitHub >

github_iconTop Results From Across the Web

567218 - anchor ignored on page load(but it works if clicked ...
Now put cursor on the end of the URL/HASH again and press ENTER. The browser now sees no change in the HASH and...
Read more >
Reloading a page; anchor link is being ignored and previous ...
I have a form that does an ajax call, and on success, reloads the (same) page and jumps to the submitted data toward...
Read more >
anchors not working correctly on web pages - Google Chrome ...
I have links to a page that go to a specific anchor (using the <a name> for the specific section and # after...
Read more >
Making the anchor links work in SPA applications
Making the anchor links work in SPA applications. A solution for react-router and other history based routing #hash navigation. The problem is simple:...
Read more >
How to load a #hash fragment to an anchor name in react ...
This behavior occurs for several reasons, one reason is because the anchor name offset is executed after the page loads the first DOM,...
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