Skip to content

Commit babe5bf

Browse files
committed
refactor: simplify + use client-side nav
1 parent d150ec7 commit babe5bf

1 file changed

Lines changed: 14 additions & 18 deletions

File tree

app/pages/[...package].vue

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -475,24 +475,20 @@ defineOgImageComponent('Package', {
475475
})
476476
477477
// We're using only @click because it catches touch events and enter hits
478-
function handleClick(evt: MouseEvent) {
479-
const target = evt?.target ? (evt.target as HTMLElement).closest('a') : undefined
480-
if (target) {
481-
const npmjsMatch = target
482-
.getAttribute('href')
483-
?.match(/^(?:https?:\/\/)?(?:www\.)?npmjs\.(?:com|org)\/(.+)/)
484-
if (npmjsMatch && npmjsMatch?.[1]) {
485-
const urlPath = npmjsMatch[1]
486-
const hasMatchedRoutes = router.resolve(urlPath)?.matched?.reduce(
487-
// omit matching the wildcard route
488-
(result: boolean, route) => result || route.path !== '/:package(.*)*',
489-
false,
490-
)
491-
if (hasMatchedRoutes) {
492-
evt.preventDefault()
493-
window.open(`https://npmx.dev/${urlPath}`, '_blank')
494-
}
495-
}
478+
function handleClick(event: MouseEvent) {
479+
const target = (event?.target as HTMLElement | undefined)?.closest('a')
480+
if (!target) return
481+
482+
const href = target.getAttribute('href')
483+
if (!href) return
484+
485+
const match = href.match(/^(?:https?:\/\/)?(?:www\.)?npmjs\.(?:com|org)\/(.+)/)
486+
if (!match || !match[1]) return
487+
488+
const route = router.resolve(match[1])
489+
if (route) {
490+
event.preventDefault()
491+
router.push(route)
496492
}
497493
}
498494
</script>

0 commit comments

Comments
 (0)