Skip to content

Commit

Permalink
minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout committed Oct 15, 2024
1 parent 7d9f442 commit 3fa963d
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions vike/client/client-routing-runtime/initOnPopState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { enhanceHistoryState, getHistoryState } from './history.js'
import { renderPageClientSide } from './renderPageClientSide.js'
import { type ScrollTarget, setScrollPosition } from './setScrollPosition.js'

const globalObject = getGlobalObject('initOnPopState.ts', { previousState: getState() })
const globalObject = getGlobalObject('initOnPopState.ts', { previous: getInfo() })

function initOnPopState() {
// - The popstate event is trigged upon:
Expand All @@ -19,9 +19,9 @@ function initOnPopState() {
// - `location.hash = 'some-hash'`
// - The `event` argument of `window.addEventListener('popstate', (event) => /*...*/)` is useless: the History API doesn't provide the previous state (the popped state), see https://stackoverflow.com/questions/48055323/is-history-state-always-the-same-as-popstate-event-state
window.addEventListener('popstate', async (): Promise<undefined> => {
const previous = globalObject.previousState
const current = getState()
globalObject.previousState = current
const { previous } = globalObject
const current = getInfo()
globalObject.previous = current

const scrollTarget: ScrollTarget = current.state?.scrollPosition || undefined

Expand All @@ -43,7 +43,7 @@ function initOnPopState() {
assert(isHashNavigation)
// The browser already scrolled to `#${hash}` => the current scroll position is the right one => we save it with `enhanceHistoryState()`.
enhanceHistoryState()
globalObject.previousState = getState()
globalObject.previous = getInfo()
}

// We have to scroll ourselves because we use `window.history.scrollRestoration = 'manual'`. So far this seems to work. Alternatives in case it doesn't work:
Expand All @@ -66,7 +66,7 @@ function initOnPopState() {
})
}

function getState() {
function getInfo() {
return {
url: getCurrentUrl(),
state: getHistoryState()
Expand All @@ -78,5 +78,5 @@ function removeHash(url: `/${string}`) {
}

function updateState() {
globalObject.previousState = getState()
globalObject.previous = getInfo()
}

0 comments on commit 3fa963d

Please sign in to comment.