Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 36 additions & 9 deletions .vitepress/lib/code-groups/restoreCodeGroupPreferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,19 @@
}

const restoreHashScroll = () => {
if (hashScrollPending) {
history.replaceState(null, '', window.location.pathname + window.location.search + initialHash)
requestAnimationFrame(() => {
scrollToHash(initialHash)
hashScrollPending = false
})
}
if (!hashScrollPending) return

// Put the hash back into the URL as soon as we start handling it, so it
// survives even if the target element hasn't been rendered yet.
history.replaceState(null, '', window.location.pathname + window.location.search + initialHash)

// Don't finish until the target actually exists; the page content may still
// be hydrating. Until then, keep hashScrollPending so later retries can scroll.
const target = document.getElementById(decodeURIComponent(initialHash).slice(1))
if (!target) return

hashScrollPending = false
requestAnimationFrame(() => scrollToHash(initialHash))
}

const initialCodeGroupCount = applyToAllCodeGroups()
Expand Down Expand Up @@ -137,13 +143,34 @@
}
}

// Guarantee the hash is restored and scrolled to, even on pages that have no
// code groups (where the MutationObserver above never fires) and regardless of
// readyState when this inline head script runs. Retries each frame until the
// target has hydrated, then stops.
const pollRestore = () => {
const deadline = Date.now() + 3000
const tick = () => {
applyToAllCodeGroups()
restoreHashScroll()
if (hashScrollPending && Date.now() < deadline) {
requestAnimationFrame(tick)
} else {
stopObserving()
}
}
requestAnimationFrame(tick)
}

if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', () => {
applyToAllCodeGroups()
restoreHashScroll()
stopObserving()
if (hashScrollPending) pollRestore()
else stopObserving()
})
} else if (!hashScrollPending) {
} else if (hashScrollPending) {
pollRestore()
} else {
stopObserving()
}
})()
Loading