Skip to content

Commit 010b3e5

Browse files
authoredJan 29, 2023
fix: take <a> in SVG into account (#1850)
1 parent e0e8c66 commit 010b3e5

File tree

2 files changed

+49
-29
lines changed

2 files changed

+49
-29
lines changed
 

‎src/client/app/composables/preFetch.ts

+32-24
Original file line numberDiff line numberDiff line change
@@ -73,32 +73,40 @@ export function usePrefetch() {
7373
})
7474

7575
rIC(() => {
76-
document.querySelectorAll<HTMLAnchorElement>('#app a').forEach((link) => {
77-
const { target, hostname, pathname } = link
78-
const extMatch = pathname.match(/\.\w+$/)
79-
if (extMatch && extMatch[0] !== '.html') {
80-
return
81-
}
76+
document
77+
.querySelectorAll<HTMLAnchorElement | SVGAElement>('#app a')
78+
.forEach((link) => {
79+
const { target } = link
80+
const { hostname, pathname } = new URL(
81+
link.href instanceof SVGAnimatedString
82+
? link.href.animVal
83+
: link.href,
84+
link.baseURI
85+
)
86+
const extMatch = pathname.match(/\.\w+$/)
87+
if (extMatch && extMatch[0] !== '.html') {
88+
return
89+
}
8290

83-
if (
84-
// only prefetch same tab navigation, since a new tab will load
85-
// the lean js chunk instead.
86-
target !== `_blank` &&
87-
// only prefetch inbound links
88-
hostname === location.hostname
89-
) {
90-
if (pathname !== location.pathname) {
91-
observer!.observe(link)
92-
} else {
93-
// No need to prefetch chunk for the current page, but also mark
94-
// it as already fetched. This is because the initial page uses its
95-
// lean chunk, and if we don't mark it, navigation to another page
96-
// with a link back to the first page will fetch its full chunk
97-
// which isn't needed.
98-
hasFetched.add(pathname)
91+
if (
92+
// only prefetch same tab navigation, since a new tab will load
93+
// the lean js chunk instead.
94+
target !== `_blank` &&
95+
// only prefetch inbound links
96+
hostname === location.hostname
97+
) {
98+
if (pathname !== location.pathname) {
99+
observer!.observe(link)
100+
} else {
101+
// No need to prefetch chunk for the current page, but also mark
102+
// it as already fetched. This is because the initial page uses its
103+
// lean chunk, and if we don't mark it, navigation to another page
104+
// with a link back to the first page will fetch its full chunk
105+
// which isn't needed.
106+
hasFetched.add(pathname)
107+
}
99108
}
100-
}
101-
})
109+
})
102110
})
103111
}
104112

‎src/client/app/router.ts

+17-5
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,21 @@ export function createRouter(
153153
const button = (e.target as Element).closest('button')
154154
if (button) return
155155

156-
const link = (e.target as Element).closest('a')
157-
if (link && !link.closest('.vp-raw') && !link.download) {
158-
const { href, origin, pathname, hash, search, target } = link
156+
const link = (e.target as Element | SVGElement).closest<
157+
HTMLAnchorElement | SVGAElement
158+
>('a')
159+
if (
160+
link &&
161+
!link.closest('.vp-raw') &&
162+
(link instanceof SVGElement || !link.download)
163+
) {
164+
const { target } = link
165+
const { href, origin, pathname, hash, search } = new URL(
166+
link.href instanceof SVGAnimatedString
167+
? link.href.animVal
168+
: link.href,
169+
link.baseURI
170+
)
159171
const currentUrl = window.location
160172
const extMatch = pathname.match(/\.\w+$/)
161173
// only intercept inbound links
@@ -217,8 +229,8 @@ export function useRoute(): Route {
217229
return useRouter().route
218230
}
219231

220-
function scrollTo(el: HTMLElement, hash: string, smooth = false) {
221-
let target: HTMLElement | null = null
232+
function scrollTo(el: HTMLElement | SVGElement, hash: string, smooth = false) {
233+
let target: HTMLElement | SVGElement | null = null
222234

223235
try {
224236
target = el.classList.contains('header-anchor')

0 commit comments

Comments
 (0)
Please sign in to comment.