Skip to content

Commit 152b524

Browse files
authored
Fix(gatsby-plugin-catch-links): SVGAnimatedString may not be available in some browsers. (#26047)
* Fix issue where SVGAnimatedString may not be available in some browsers. * Add unit test for SVGAnimation support
1 parent bfd46f8 commit 152b524

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

packages/gatsby-plugin-catch-links/src/__tests__/catch-links.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,3 +490,41 @@ describe(`pathPrefix is handled if caught link to ${pathPrefix}/article navigate
490490
clickElement.dispatchEvent(clickEvent)
491491
})
492492
})
493+
494+
describe(`navigation is routed through browser without SVGAnimatedString support`, () => {
495+
let hrefHandler
496+
let eventDestroyer
497+
let oldSVGAnimatedString = SVGAnimatedString
498+
499+
beforeAll(() => {
500+
hrefHandler = jest.fn()
501+
eventDestroyer = catchLinks.default(window, {}, hrefHandler)
502+
delete global.SVGAnimatedString
503+
})
504+
505+
afterAll(() => {
506+
eventDestroyer()
507+
global.SVGAnimatedString = oldSVGAnimatedString
508+
})
509+
510+
test(`works without throwing an error`, () => {
511+
// create a click element to use for testing
512+
const clickElement = document.createElement(`a`)
513+
clickElement.setAttribute(`href`, `${window.location.href}/article`)
514+
document.body.appendChild(clickElement)
515+
516+
// create the click event we'll be using for testing
517+
const clickEvent = new MouseEvent(`click`, {
518+
bubbles: true,
519+
cancelable: true,
520+
view: window,
521+
})
522+
523+
// and trigger click
524+
clickElement.dispatchEvent(clickEvent)
525+
526+
expect(() =>
527+
catchLinks.routeThroughBrowserOrApp(jest.fn())(clickEvent)
528+
).not.toThrow()
529+
})
530+
})

packages/gatsby-plugin-catch-links/src/catch-links.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,10 @@ export const routeThroughBrowserOrApp = (
130130
destination.href = clickedAnchor.href
131131
}
132132

133-
if (clickedAnchor.href instanceof SVGAnimatedString) {
133+
if (
134+
`SVGAnimatedString` in window &&
135+
clickedAnchor.href instanceof SVGAnimatedString
136+
) {
134137
destination.href = clickedAnchor.href.animVal
135138
}
136139

0 commit comments

Comments
 (0)