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

Commit
2 years ago
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: Tim Neutkens <tim@timneutkens.nl>
Author
Parents
Loading