diff --git a/src/client/app/index.ts b/src/client/app/index.ts index db24c27e..2e4c965a 100644 --- a/src/client/app/index.ts +++ b/src/client/app/index.ts @@ -9,7 +9,7 @@ import { } from 'vue' import RawTheme from '@theme/index' import { inBrowser, pathToFile } from './utils.js' -import { type Router, RouterSymbol, createRouter } from './router.js' +import { type Router, RouterSymbol, createRouter, scrollTo } from './router.js' import { siteDataRef, useData } from './data.js' import { useUpdateHead } from './composables/head.js' import { usePrefetch } from './composables/preFetch.js' @@ -149,6 +149,14 @@ if (inBrowser) { // dynamically update head tags useUpdateHead(router.route, data.site) app.mount('#app') + + // scroll to hash on new tab during dev + if (import.meta.env.DEV && location.hash) { + const target = document.querySelector(location.hash) + if (target) { + scrollTo(target, location.hash) + } + } }) }) } diff --git a/src/client/app/router.ts b/src/client/app/router.ts index a89b3d08..4b565d19 100644 --- a/src/client/app/router.ts +++ b/src/client/app/router.ts @@ -229,8 +229,8 @@ export function useRoute(): Route { return useRouter().route } -function scrollTo(el: HTMLElement | SVGElement, hash: string, smooth = false) { - let target: HTMLElement | SVGElement | null = null +export function scrollTo(el: Element, hash: string, smooth = false) { + let target: Element | null = null try { target = el.classList.contains('header-anchor')