Unverified Commit 73065beb authored by Joe Freeman's avatar Joe Freeman Committed by GitHub

fix(link): do not reload when Link is in svg (#41320)Co-authored-by: Tim…

fix(link): do not reload when Link is in svg (#41320)Co-authored-by: Tim Neutkens <tim@timneutkens.nl>

<!--
Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that
you follow the checklist sections below.
Choose the right checklist for the change that you're making:
-->

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

I believe this fixes `<Link>`s that appear inside an `<svg>`. For
example:

```typescript
<svg width={200} height={200}>
  <Link href="/about">
    <a>
      <text x={0} y={20}>About</text>
    </a>
  </Link>
</svg>
```

There's a comment in `next/link` (["anchors inside an svg have a
lowercase
nodeName"](https://github.com/vercel/next.js/blob/bef709bc74760aadedfeb2fd4a4434e213cb024f/packages/next/client/link.tsx#L163))
that implies `Link`s are supposed to work inside an `svg`, but at the
moment, I'm finding that clicking the link causes a full page reload.

This seems to be because Next.js considers the link to be a 'modified'
event (as per `isModifiedEvent`). In the case where the event's
`currentTarget` is an `SVGAElement` (rather than a `HTMLAnchorElement`),
the `event.currentTarget.target` is actually an
[`SVGAnimatedString`](https://developer.mozilla.org/en-US/docs/Web/API/SVGAnimatedString).
This looks a bit like `{"animVal": "", "baseVal": ""}`, so the `(target
&& target !== '_self')` check is truthy.

Using
[`getAttribute`](https://developer.mozilla.org/en-US/docs/Web/API/Element/getAttribute)
instead seems to consistently give a string value in either (SVG or
HTML) case.

I've attempted to add a test, but I haven't worked out how to run it
yet...

---------
Co-authored-by: 's avatarTim Neutkens <tim@timneutkens.nl>
parent 778a2a74
...@@ -166,7 +166,8 @@ function prefetch( ...@@ -166,7 +166,8 @@ function prefetch(
} }
function isModifiedEvent(event: React.MouseEvent): boolean { function isModifiedEvent(event: React.MouseEvent): boolean {
const { target } = event.currentTarget as HTMLAnchorElement const eventTarget = event.currentTarget as HTMLAnchorElement | SVGAElement
const target = eventTarget.getAttribute('target')
return ( return (
(target && target !== '_self') || (target && target !== '_self') ||
event.metaKey || event.metaKey ||
......
...@@ -234,6 +234,18 @@ describe('Client Navigation', () => { ...@@ -234,6 +234,18 @@ describe('Client Navigation', () => {
expect(counterText).toBe('Counter: 1') expect(counterText).toBe('Counter: 1')
await browser.close() await browser.close()
}) })
it('should not reload when link in svg is clicked', async () => {
const browser = await webdriver(context.appPort, '/nav')
await browser.eval('window.hello = true')
await browser
.elementByCss('#in-svg-link')
.click()
.waitForElementByCss('.nav-about')
expect(await browser.eval('window.hello')).toBe(true)
await browser.close()
})
}) })
describe('with unexpected <a/> nested tag', () => { describe('with unexpected <a/> nested tag', () => {
...@@ -1741,7 +1753,7 @@ describe('Client Navigation', () => { ...@@ -1741,7 +1753,7 @@ describe('Client Navigation', () => {
await browser.eval(`(function() { await browser.eval(`(function() {
window.routeErrors = [] window.routeErrors = []
window.next.router.events.on('routeChangeError', function (err) { window.next.router.events.on('routeChangeError', function (err) {
window.routeErrors.push(err) window.routeErrors.push(err)
}) })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment