(opts?: {
parseLocation?: () => HistoryLocation
createHref?: (path: string) => string
window?: any
})
| 279 | * @returns A history instance |
| 280 | */ |
| 281 | export function createBrowserHistory(opts?: { |
| 282 | parseLocation?: () => HistoryLocation |
| 283 | createHref?: (path: string) => string |
| 284 | window?: any |
| 285 | }): RouterHistory { |
| 286 | const win = |
| 287 | opts?.window ?? |
| 288 | (typeof document !== 'undefined' ? window : (undefined as any)) |
| 289 | |
| 290 | const originalPushState = win.history.pushState |
| 291 | const originalReplaceState = win.history.replaceState |
| 292 | |
| 293 | let blockers: Array<NavigationBlocker> = [] |
| 294 | const _getBlockers = () => blockers |
| 295 | const _setBlockers = (newBlockers: Array<NavigationBlocker>) => |
| 296 | (blockers = newBlockers) |
| 297 | |
| 298 | const createHref = opts?.createHref ?? ((path) => path) |
| 299 | const parseLocation = |
| 300 | opts?.parseLocation ?? |
| 301 | (() => |
| 302 | parseHref( |
| 303 | `${win.location.pathname}${win.location.search}${win.location.hash}`, |
| 304 | win.history.state, |
| 305 | )) |
| 306 | |
| 307 | // Ensure there is always a key to start |
| 308 | if (!win.history.state?.__TSR_key && !win.history.state?.key) { |
| 309 | const addedKey = createRandomKey() |
| 310 | win.history.replaceState( |
| 311 | { |
| 312 | [stateIndexKey]: 0, |
| 313 | key: addedKey, // TODO: Remove in v2 - use __TSR_key instead |
| 314 | __TSR_key: addedKey, |
| 315 | }, |
| 316 | '', |
| 317 | ) |
| 318 | } |
| 319 | |
| 320 | let currentLocation = parseLocation() |
| 321 | let rollbackLocation: HistoryLocation | undefined |
| 322 | |
| 323 | let nextPopIsGo = false |
| 324 | let ignoreNextPop = false |
| 325 | let skipBlockerNextPop = false |
| 326 | let ignoreNextBeforeUnload = false |
| 327 | |
| 328 | const getLocation = () => currentLocation |
| 329 | |
| 330 | let next: |
| 331 | | undefined |
| 332 | | { |
| 333 | // This is the latest location that we were attempting to push/replace |
| 334 | href: string |
| 335 | // This is the latest state that we were attempting to push/replace |
| 336 | state: any |
| 337 | // This is the latest type that we were attempting to push/replace |
| 338 | isPush: boolean |
no test coverage detected