Skip to content

Commit

Permalink
replace const declarations with var for better browser support
Browse files Browse the repository at this point in the history
  • Loading branch information
vigbeamer committed Aug 6, 2024
1 parent de3d0a7 commit 0c66e41
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ jobs:
registry-url: https://registry.npmjs.org
- name: Install dependencies
run: npm install
- name: Run tests
run: npm run test
- name: Build Userflow.js
run: npm run build
- name: Publish
Expand Down
44 changes: 23 additions & 21 deletions src/userflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,35 +207,37 @@ interface Deferred {
// empty object instead of `window`.
var w: WindowWithUserflow = typeof window === 'undefined' ? ({} as any) : window
var userflow = w.userflow
const history = w.history

// indicates if the history API's pushState and replaceState are patched with custom event emitters
w.__userflowStatePatched = true

// patch the history API's
// pushState method to emit userflow:pushstate and
// replaceState method to emit userflow:replacestate
const originalPushState = history.pushState
const originalReplaceState = history.replaceState
var history = w.history

function overrideHistoryMethods(method: () => void, eventName: string) {
return function () {
const event = new CustomEvent(eventName)
const args = Array.prototype.slice.call(arguments)
const ret = method.apply(history, args as any)
var event = new CustomEvent(eventName)
var args = Array.prototype.slice.call(arguments)
var ret = method.apply(history, args as any)
w.dispatchEvent(event)
return ret
}
}

history.pushState = overrideHistoryMethods(
originalPushState as any,
'userflow:pushstate'
)
history.replaceState = overrideHistoryMethods(
originalReplaceState as any,
'userflow:replacestate'
)
// patch the history API's
// pushState method to emit userflow:pushstate and
// replaceState method to emit userflow:replacestate
if (history) {
// indicates if the history API's pushState and replaceState are patched with custom event emitters
w.__userflowStatePatched = true

var originalPushState = history.pushState
var originalReplaceState = history.replaceState

history.pushState = overrideHistoryMethods(
originalPushState as any,
'userflow:pushstate'
)
history.replaceState = overrideHistoryMethods(
originalReplaceState as any,
'userflow:replacestate'
)
}

if (!userflow) {
//
Expand Down

0 comments on commit 0c66e41

Please sign in to comment.