Closed jaysella closed 5 years ago
As the article you linked states, nested links are not valid html.
Also as the article states:
Obviously, a validator is a tool not showing anything but the formal specifications compliance. In our case, the usage of links inside objects is entirely reasonable and won’t break anything for anyone if we’d make things in a proper way. ... There are a lot of cases where this is a crucial requirement, and right now all we have are workarounds and “hacks” like this one.
My question relates to how gatsby-link treats <object>
's / nested elements differently than default html <a>
tags. On the docs page, it is stated:
In any situation where you want to link between pages on the same site, use the
Link
component instead of ana
tag.
This, however, is misleading since as pointed out here, gatsby-link treats nested elements differently. It appears gatsby-link is much stricter in adhering to the specs. Is this true for the gatsby-link component?
I am not trying to argue the validity of the workaround but rather trying to understand the differences between the standard a
and Gatsby's Link
which are noted to be interchangeable for internal links.
gatsby-link
registers an onClick
handler here:
https://github.com/gatsbyjs/gatsby/blob/ec8371ea8d7dbc118afe95c655dd1add293d8783/packages/gatsby-link/src/index.js#L145-L167
So one way to do this would be to programmatically navigate on the anchor as well:
<Link to="/page-2">
<span>Foo</span>
<a
href="https://google.com"
onClick={e => {
e.preventDefault()
window.location = e.target.href
}}
>
Bar
</a>
<span>Baz</span>
</Link>
Thank you, Stefan. I am closing this issue.
Description
When using gatsby-link as the parent element and nesting another link within an
<object>
, the nested link does not work unless right-clicking and "opening in new tab". See the referenced article below on nesting links with<object>
.Steps to reproduce
import { Link } from 'gatsby'
<object>
:Expected result
In the above example, Foo and Bot should link to "/page-2" and Bar should link to "//google.com":
FooBarBot
See Roman Komarov's article on nesting links using an
<object>
.Actual result
Foo, Bot, AND Bar all link to "/page-2" when clicking them. However, right-clicking on Bar and opening the link in a new tab does work as expected, directing to "//google.com"
The link preview in the bottom-left corner of Chrome does show the correct links, but the parent gatsby-link takes over.
Environment