Gatsby: [gatsby-plugin-catch-links] Links fail when domain name is included in link

Created on 16 Jan 2018  路  6Comments  路  Source: gatsbyjs/gatsby

When you have an anchor tag in markdown, links are not prefixed (https://github.com/gatsbyjs/gatsby/issues/3316); therefore opening the link in a new tab fails. To get around this, you can pass in the absolute url instead of just a relative url.

However, when you just click on the link regularly, it will append the entire absolute url to the existing url. The reason is because of this line: https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-plugin-catch-links/src/catch-links.js#L61. The entire href attribute gets passed in when the host of the href matches the host of the page you are on.

Would it be possible to change this line to

cb(anchor.pathname)

This would solve the issue but I'm not sure if it'll be problematic for others.

bug

Most helpful comment

We should detect if the link is pointed to a different domain and then let the browser handle the link click normally.

A PR for this would be great!

All 6 comments

Does the pathname exist on the anchor? If it does that sounds fine.

yeah, usually for these links, you'd pass in <a href="/some/path">. so it's working now because anchor.getAttribute('href') returns that property verbatim. but it won't work if you do <a href="https://mydomain.com/some/path">. This will result in pushing mydomain.com/some/path to the end of the current url. However, if you use anchor.pathname, you will get /some/path in both examples, which will result in correct navigation behavior.

We should detect if the link is pointed to a different domain and then let the browser handle the link click normally.

A PR for this would be great!

Any updates?

@alem0lars There's a PR already open for this issue - #7779

Once it is merged the fix will be available for Gatsby v1, this issue has already been fixed for Gatsby v2. If you want to give that a try, we have nicely written migration guide.

Fixed in #7779

Was this page helpful?
0 / 5 - 0 ratings

Related issues

benstr picture benstr  路  3Comments

jimfilippou picture jimfilippou  路  3Comments

Oppenheimer1 picture Oppenheimer1  路  3Comments

andykais picture andykais  路  3Comments

rossPatton picture rossPatton  路  3Comments