Gatsby: Anchor Links, part 2

Created on 11 Aug 2016  ·  16Comments  ·  Source: gatsbyjs/gatsby

Is there a plan to support or document how to make anchor links work?

I know https://github.com/gatsbyjs/gatsby/issues/302 is closed for now, but it's not clear how to work-around.

I have a fork of gatsby-starter-default that reproduces the error here: https://github.com/ivanoats/gatsby-starter-default/blob/master/pages/html.html

stale?

Most helpful comment

As far as anchorate is concerned, you can achieve what it does with this basically in gatsby-browser.js:

exports.onRouteUpdate = (location) => {
  if (location.hash) {
    setTimeout(() => {
      document.querySelector(`${location.hash}`).scrollIntoView();
    }, 0);
  }
};

All 16 comments

Works great! Should we add it to starters, README, or both?

Don't think they need to be in starters (other than perhaps the documentation one) but yeah, this would be great to add to the README.

1) Cool, I will make a PR for the README.

2) Starters: OK, hear me out on this. Anchors are such a basic part of HTML that we need to explain if something is broken with HTML out of the box. Maybe I've been using HTML for too long and that's why I feel that way, but that's what I think.

hmmm... that is a good point actually. I added react-router-scroll for the
same reason. We do have to emulate features that the browser normally
provides by default. Changed positions — perhaps even make this turned on
by default in Gatsby?

On Wed, Aug 10, 2016 at 9:41 PM Ivan Storck [email protected]
wrote:

1) Cool, I will make a PR for the README.

2) Starters: OK, hear me out on this. Anchors are such a basic part of
HTML that we need to explain if something is broken with HTML out of the
box. Maybe I've been using HTML for too long and that's why I feel that
way, but that's what I think.


You are receiving this because you commented.

Reply to this email directly, view it on GitHub
https://github.com/gatsbyjs/gatsby/issues/386#issuecomment-239072931,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAEVh_nmVkdTdRMrPd3TEyifrgOvE36Dks5qeqgGgaJpZM4Jho9v
.

Maybe taking this one step further, it might be cool to be able to opt-in to have anchors auto-generated for all h1 and h2 tags based on the text for that heading.

@rojobuffalo you can use something like markdown-it-anchor for that

@ivanoats are you sure it is working 100% of the time? i tested it in my project and the result is almost the same as without using anchorate.

The correct way to fix this would involve stopping a re render in the first place. anchorate gets called only after the route is changed

Pretty sure. I haven't had any issues from manual testers. I would be interested in an automated test.

Sent from my iPhone

On Sep 24, 2016, at 3:13 AM, Siddharth Jain [email protected] wrote:

@ivanoats are you sure it is working 100% of the time? i tested it in my project and the result is almost the same as without using anchorate.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.

As far as anchorate is concerned, you can achieve what it does with this basically in gatsby-browser.js:

exports.onRouteUpdate = (location) => {
  if (location.hash) {
    setTimeout(() => {
      document.querySelector(`${location.hash}`).scrollIntoView();
    }, 0);
  }
};

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

This issue has been automatically closed due to inactivity.

native HTML anchor links are broken, even using import { Link } from "gatsby"; ...
Only this worked for me *:

import { navigate } from "@reach/router";

<div className="custom-link" onClick={() => {navigate("#section_id")}}>Section</div>

*But my website works with and without www. This approach only works for my website without www.

navigate doesn't work for me either. I get redirected to my index page.

Was this page helpful?
0 / 5 - 0 ratings