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

Function createMemoryHistory

packages/history/src/index.ts:578–631  ·  view source on GitHub ↗
(
  opts: {
    initialEntries: Array<string>
    initialIndex?: number
  } = {
    initialEntries: ['/'],
  },
)

Source from the content-addressed store, hash-verified

576 * @link https://tanstack.com/router/latest/docs/framework/react/guide/history-types
577 */
578export function createMemoryHistory(
579 opts: {
580 initialEntries: Array<string>
581 initialIndex?: number
582 } = {
583 initialEntries: ['/'],
584 },
585): RouterHistory {
586 const entries = opts.initialEntries
587 let index = opts.initialIndex
588 ? Math.min(Math.max(opts.initialIndex, 0), entries.length - 1)
589 : entries.length - 1
590 const states = entries.map((_entry, index) =>
591 assignKeyAndIndex(index, undefined),
592 )
593
594 const getLocation = () => parseHref(entries[index]!, states[index])
595
596 let blockers: Array<NavigationBlocker> = []
597 const _getBlockers = () => blockers
598 const _setBlockers = (newBlockers: Array<NavigationBlocker>) =>
599 (blockers = newBlockers)
600
601 return createHistory({
602 getLocation,
603 getLength: () => entries.length,
604 pushState: (path, state) => {
605 // Removes all subsequent entries after the current index to start a new branch
606 if (index < entries.length - 1) {
607 entries.splice(index + 1)
608 states.splice(index + 1)
609 }
610 states.push(state)
611 entries.push(path)
612 index = Math.max(entries.length - 1, 0)
613 },
614 replaceState: (path, state) => {
615 states[index] = state
616 entries[index] = path
617 },
618 back: () => {
619 index = Math.max(index - 1, 0)
620 },
621 forward: () => {
622 index = Math.min(index + 1, entries.length - 1)
623 },
624 go: (n) => {
625 index = Math.min(Math.max(index + n, 0), entries.length - 1)
626 },
627 createHref: (path) => path,
628 getBlockers: _getBlockers,
629 setBlockers: _setBlockers,
630 })
631}
632
633/**
634 * Sanitize a path to prevent open redirect vulnerabilities.

Callers 15

createRequestHandlerFunction · 0.90
hydrate.test.tsFile · 0.90
setupFunction · 0.90
createFooRouterFunction · 0.90
setupWithPathParamsFunction · 0.90
load.test.tsFile · 0.90
setupFunction · 0.90
setupScenarioFunction · 0.90
setupFunction · 0.90
mask.test.tsFile · 0.90

Calls 4

assignKeyAndIndexFunction · 0.85
createHistoryFunction · 0.85
minMethod · 0.45
maxMethod · 0.45

Tested by 15

setupFunction · 0.72
createFooRouterFunction · 0.72
setupWithPathParamsFunction · 0.72
setupFunction · 0.72
setupScenarioFunction · 0.72
setupFunction · 0.72
createTestRouterFunction · 0.72
setupFunction · 0.72
createTestRouterFunction · 0.72