MCPcopy
hub / github.com/TanStack/router / createBrowserHistory

Function createBrowserHistory

packages/history/src/index.ts:281–545  ·  view source on GitHub ↗
(opts?: {
  parseLocation?: () => HistoryLocation
  createHref?: (path: string) => string
  window?: any
})

Source from the content-addressed store, hash-verified

279 * @returns A history instance
280 */
281export 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

Callers 15

RouterCoreClass · 0.90
route.test.tsxFile · 0.90
not-found.test.tsxFile · 0.90
link.test.tsxFile · 0.90
route.test.tsxFile · 0.90
not-found.test.tsxFile · 0.90
link.test.tsxFile · 0.90

Calls 6

parseHrefFunction · 0.85
createRandomKeyFunction · 0.85
createHistoryFunction · 0.85
queueHistoryActionFunction · 0.85
onPushPopFunction · 0.85
forwardMethod · 0.65

Tested by

no test coverage detected