Skip to content

Commit

Permalink
fix: dont trigger onHashChange if path is not same
Browse files Browse the repository at this point in the history
  • Loading branch information
brc-dd committed Apr 8, 2024
1 parent 4d838ce commit 12ab05d
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/examples/utils.ts
Expand Up @@ -208,8 +208,15 @@ export function resolveNoBuildExample(
}

export function onHashChange(cb: () => void) {
window.addEventListener('hashchange', cb)
if (typeof document === 'undefined') return
const { pathname } = window.location
function check() {
if (window.location.pathname === pathname) {
cb()
}
}
window.addEventListener('hashchange', check)
onBeforeUnmount(() => {
window.removeEventListener('hashchange', cb)
window.removeEventListener('hashchange', check)
})
}

0 comments on commit 12ab05d

Please sign in to comment.