Skip to content

Commit

Permalink
fix: ingest external history changes
Browse files Browse the repository at this point in the history
  • Loading branch information
tannerlinsley committed Aug 4, 2023
1 parent a7d5975 commit 90d13cd
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion packages/router/src/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface RouterLocation extends ParsedPath {

type BlockerFn = (retry: () => void, cancel: () => void) => void

const pushStateEvent = 'pushstate'
const popStateEvent = 'popstate'
const beforeUnloadEvent = 'beforeunload'

Expand Down Expand Up @@ -71,7 +72,7 @@ function createHistory(opts: {
queue.shift()?.()
}

onUpdate()
// onUpdate()
}

const queueTask = (task: () => void) => {
Expand All @@ -81,6 +82,7 @@ function createHistory(opts: {

const onUpdate = () => {
location = opts.getLocation()
console.log('onUpdate', location)
listeners.forEach((listener) => listener())
}

Expand Down Expand Up @@ -161,8 +163,26 @@ export function createBrowserHistory(opts?: {
return createHistory({
getLocation,
listener: (onUpdate) => {
window.addEventListener(pushStateEvent, onUpdate)
window.addEventListener(popStateEvent, onUpdate)

var pushState = window.history.pushState
window.history.pushState = function () {
let res = pushState.apply(history, arguments as any)
onUpdate()
return res
}
var replaceState = window.history.replaceState
window.history.replaceState = function () {
let res = replaceState.apply(history, arguments as any)
onUpdate()
return res
}

return () => {
window.history.pushState = pushState
window.history.replaceState = replaceState
window.removeEventListener(pushStateEvent, onUpdate)
window.removeEventListener(popStateEvent, onUpdate)
}
},
Expand Down

0 comments on commit 90d13cd

Please sign in to comment.