Skip to content

Commit efc5e1b

Browse files
authored
fix: tolerant invalid hash (#399)
1 parent 4b76617 commit efc5e1b

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

src/client/app/router.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,14 @@ export function createRouter(
8484
if (inBrowser) {
8585
nextTick(() => {
8686
if (targetLoc.hash && !scrollPosition) {
87-
const target = document.querySelector(
88-
decodeURIComponent(targetLoc.hash)
89-
) as HTMLElement
87+
let target: HTMLElement | null = null
88+
try {
89+
target = document.querySelector(
90+
decodeURIComponent(targetLoc.hash)
91+
) as HTMLElement
92+
} catch (e) {
93+
console.warn(e)
94+
}
9095
if (target) {
9196
scrollTo(target, targetLoc.hash)
9297
return
@@ -176,9 +181,16 @@ export function useRoute(): Route {
176181
}
177182

178183
function scrollTo(el: HTMLElement, hash: string, smooth = false) {
179-
const target = el.classList.contains('.header-anchor')
180-
? el
181-
: document.querySelector(decodeURIComponent(hash))
184+
let target: Element | null = null
185+
186+
try {
187+
target = el.classList.contains('.header-anchor')
188+
? el
189+
: document.querySelector(decodeURIComponent(hash))
190+
} catch (e) {
191+
console.warn(e)
192+
}
193+
182194
if (target) {
183195
const targetTop = (target as HTMLElement).offsetTop
184196
// only smooth scroll if distance is smaller than screen height.

0 commit comments

Comments
 (0)