Skip to content

Commit

Permalink
small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout committed Oct 15, 2024
1 parent c0d14ac commit 32cf141
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
24 changes: 14 additions & 10 deletions vike/client/client-routing-runtime/initOnPopState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ 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 { previousState } = globalObject
const currentState = getState()
globalObject.previousState = currentState
const previous = globalObject.previousState
const current = getState()
globalObject.previousState = current

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

const isUserLandPushStateNavigation = currentState.historyState?.triggeredBy === 'user'
const isUserLandPushStateNavigation = current.state?.triggeredBy === 'user'

const isHashNavigation = currentState.urlWithoutHash === previousState.urlWithoutHash
const isHashNavigation = removeHash(current.url) === removeHash(previous.url)

const isBackwardNavigation =
!currentState.historyState?.timestamp || !previousState.historyState?.timestamp
!current.state?.timestamp || !previous.state?.timestamp
? null
: currentState.historyState.timestamp < previousState.historyState.timestamp
: current.state.timestamp < previous.state.timestamp

// - `history.state === null` when:
// - Click on `<a href="#some-hash" />` (note that Vike's `initOnLinkClick()` handler skips hash links)
Expand Down Expand Up @@ -68,11 +68,15 @@ function initOnPopState() {

function getState() {
return {
urlWithoutHash: getCurrentUrl({ withoutHash: true }),
historyState: getHistoryState()
url: getCurrentUrl(),
state: getHistoryState()
}
}

function removeHash(url: `/${string}`) {
return url.split('#')[0]!
}

function updateState() {
globalObject.previousState = getState()
}
4 changes: 2 additions & 2 deletions vike/utils/getCurrentUrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export { getCurrentUrl }
import { parseUrl } from './parseUrl.js'
import { assert } from './assert.js'

function getCurrentUrl(options?: { withoutHash: true }): string {
function getCurrentUrl(options?: { withoutHash: true }): `/${string}` {
const url = window.location.href
const { searchOriginal, hashOriginal, pathname } = parseUrl(url, '/')
let urlCurrent: string
Expand All @@ -13,5 +13,5 @@ function getCurrentUrl(options?: { withoutHash: true }): string {
urlCurrent = `${pathname}${searchOriginal || ''}${hashOriginal || ''}`
}
assert(urlCurrent.startsWith('/'))
return urlCurrent
return urlCurrent as `/${string}`
}

0 comments on commit 32cf141

Please sign in to comment.