@tanstack/history 1.0.1 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -60,13 +60,15 @@ function createHistory(opts) {
60
60
  push: (path, state) => {
61
61
  state = assignKey(state);
62
62
  tryNavigation(() => {
63
- opts.pushState(path, state, onUpdate);
63
+ opts.pushState(path, state);
64
+ onUpdate();
64
65
  });
65
66
  },
66
67
  replace: (path, state) => {
67
68
  state = assignKey(state);
68
69
  tryNavigation(() => {
69
- opts.replaceState(path, state, onUpdate);
70
+ opts.replaceState(path, state);
71
+ onUpdate();
70
72
  });
71
73
  },
72
74
  go: index => {
@@ -171,7 +173,7 @@ function createBrowserHistory(opts) {
171
173
  };
172
174
 
173
175
  // This function queues up a call to update the browser history
174
- const queueHistoryAction = (type, destHref, state, onUpdate) => {
176
+ const queueHistoryAction = (type, destHref, state) => {
175
177
  const href = createHref(destHref);
176
178
  if (!scheduled) {
177
179
  rollbackLocation = currentLocation;
@@ -186,9 +188,6 @@ function createBrowserHistory(opts) {
186
188
  state,
187
189
  isPush: next?.isPush || type === 'push'
188
190
  };
189
-
190
- // Notify subscribers
191
- onUpdate();
192
191
  if (!scheduled) {
193
192
  // Schedule an update to the browser history
194
193
  scheduled = Promise.resolve().then(() => flush());
@@ -202,8 +201,8 @@ function createBrowserHistory(opts) {
202
201
  var originalReplaceState = win.history.replaceState;
203
202
  const history = createHistory({
204
203
  getLocation,
205
- pushState: (href, state, onUpdate) => queueHistoryAction('push', href, state, onUpdate),
206
- replaceState: (href, state, onUpdate) => queueHistoryAction('replace', href, state, onUpdate),
204
+ pushState: (href, state) => queueHistoryAction('push', href, state),
205
+ replaceState: (href, state) => queueHistoryAction('replace', href, state),
207
206
  back: () => win.history.back(),
208
207
  forward: () => win.history.forward(),
209
208
  go: n => win.history.go(n),
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../../src/index.ts"],"sourcesContent":["// While the public API was clearly inspired by the \"history\" npm package,\n// This implementation attempts to be more lightweight by\n// making assumptions about the way TanStack Router works\n\nexport interface RouterHistory {\n location: HistoryLocation\n subscribe: (cb: () => void) => () => void\n push: (path: string, state?: any) => void\n replace: (path: string, state?: any) => void\n go: (index: number) => void\n back: () => void\n forward: () => void\n createHref: (href: string) => string\n block: (blocker: BlockerFn) => () => void\n flush: () => void\n destroy: () => void\n notify: () => void\n}\n\nexport interface HistoryLocation extends ParsedPath {\n state: HistoryState\n}\n\nexport interface ParsedPath {\n href: string\n pathname: string\n search: string\n hash: string\n}\n\nexport interface HistoryState {\n key: string\n}\n\ntype ShouldAllowNavigation = any\n\nexport type BlockerFn = () =>\n | Promise<ShouldAllowNavigation>\n | ShouldAllowNavigation\n\nconst pushStateEvent = 'pushstate'\nconst popStateEvent = 'popstate'\nconst beforeUnloadEvent = 'beforeunload'\n\nconst beforeUnloadListener = (event: Event) => {\n event.preventDefault()\n // @ts-ignore\n return (event.returnValue = '')\n}\n\nconst stopBlocking = () => {\n removeEventListener(beforeUnloadEvent, beforeUnloadListener, {\n capture: true,\n })\n}\n\nexport function createHistory(opts: {\n getLocation: () => HistoryLocation\n pushState: (path: string, state: any, onUpdate: () => void) => void\n replaceState: (path: string, state: any, onUpdate: () => void) => void\n go: (n: number) => void\n back: () => void\n forward: () => void\n createHref: (path: string) => string\n flush?: () => void\n destroy?: () => void\n onBlocked?: (onUpdate: () => void) => void\n}): RouterHistory {\n let location = opts.getLocation()\n let subscribers = new Set<() => void>()\n let blockers: BlockerFn[] = []\n\n const onUpdate = () => {\n location = opts.getLocation()\n subscribers.forEach((subscriber) => subscriber())\n }\n\n const tryNavigation = async (task: () => void) => {\n if (typeof document !== 'undefined' && blockers.length) {\n for (let blocker of blockers) {\n const allowed = await blocker()\n if (!allowed) {\n opts.onBlocked?.(onUpdate)\n return\n }\n }\n }\n\n task()\n }\n\n return {\n get location() {\n return location\n },\n subscribe: (cb: () => void) => {\n subscribers.add(cb)\n\n return () => {\n subscribers.delete(cb)\n }\n },\n push: (path: string, state: any) => {\n state = assignKey(state)\n tryNavigation(() => {\n opts.pushState(path, state, onUpdate)\n })\n },\n replace: (path: string, state: any) => {\n state = assignKey(state)\n tryNavigation(() => {\n opts.replaceState(path, state, onUpdate)\n })\n },\n go: (index) => {\n tryNavigation(() => {\n opts.go(index)\n })\n },\n back: () => {\n tryNavigation(() => {\n opts.back()\n })\n },\n forward: () => {\n tryNavigation(() => {\n opts.forward()\n })\n },\n createHref: (str) => opts.createHref(str),\n block: (blocker) => {\n blockers.push(blocker)\n\n if (blockers.length === 1) {\n addEventListener(beforeUnloadEvent, beforeUnloadListener, {\n capture: true,\n })\n }\n\n return () => {\n blockers = blockers.filter((b) => b !== blocker)\n\n if (!blockers.length) {\n stopBlocking()\n }\n }\n },\n flush: () => opts.flush?.(),\n destroy: () => opts.destroy?.(),\n notify: onUpdate,\n }\n}\n\nfunction assignKey(state: HistoryState) {\n if (!state) {\n state = {} as HistoryState\n }\n return {\n ...state,\n key: createRandomKey(),\n }\n}\n\n/**\n * Creates a history object that can be used to interact with the browser's\n * navigation. This is a lightweight API wrapping the browser's native methods.\n * It is designed to work with TanStack Router, but could be used as a standalone API as well.\n * IMPORTANT: This API implements history throttling via a microtask to prevent\n * excessive calls to the history API. In some browsers, calling history.pushState or\n * history.replaceState in quick succession can cause the browser to ignore subsequent\n * calls. This API smooths out those differences and ensures that your application\n * state will *eventually* match the browser state. In most cases, this is not a problem,\n * but if you need to ensure that the browser state is up to date, you can use the\n * `history.flush` method to immediately flush all pending state changes to the browser URL.\n * @param opts\n * @param opts.getHref A function that returns the current href (path + search + hash)\n * @param opts.createHref A function that takes a path and returns a href (path + search + hash)\n * @returns A history instance\n */\nexport function createBrowserHistory(opts?: {\n parseLocation?: () => HistoryLocation\n createHref?: (path: string) => string\n window?: any\n}): RouterHistory {\n const win =\n opts?.window ??\n (typeof document !== 'undefined' ? window : (undefined as any))\n\n const createHref = opts?.createHref ?? ((path) => path)\n const parseLocation =\n opts?.parseLocation ??\n (() =>\n parseHref(\n `${win.location.pathname}${win.location.search}${win.location.hash}`,\n win.history.state,\n ))\n\n let currentLocation = parseLocation()\n let rollbackLocation: HistoryLocation | undefined\n\n const getLocation = () => currentLocation\n\n let next:\n | undefined\n | {\n // This is the latest location that we were attempting to push/replace\n href: string\n // This is the latest state that we were attempting to push/replace\n state: any\n // This is the latest type that we were attempting to push/replace\n isPush: boolean\n }\n\n // Because we are proactively updating the location\n // in memory before actually updating the browser history,\n // we need to track when we are doing this so we don't\n // notify subscribers twice on the last update.\n let tracking = true\n\n // We need to track the current scheduled update to prevent\n // multiple updates from being scheduled at the same time.\n let scheduled: Promise<void> | undefined\n\n // This function is a wrapper to prevent any of the callback's\n // side effects from causing a subscriber notification\n const untrack = (fn: () => void) => {\n tracking = false\n fn()\n tracking = true\n }\n\n // This function flushes the next update to the browser history\n const flush = () => {\n // Do not notify subscribers about this push/replace call\n untrack(() => {\n if (!next) return\n win.history[next.isPush ? 'pushState' : 'replaceState'](\n next.state,\n '',\n next.href,\n )\n // Reset the nextIsPush flag and clear the scheduled update\n next = undefined\n scheduled = undefined\n rollbackLocation = undefined\n })\n }\n\n // This function queues up a call to update the browser history\n const queueHistoryAction = (\n type: 'push' | 'replace',\n destHref: string,\n state: any,\n onUpdate: () => void,\n ) => {\n const href = createHref(destHref)\n\n if (!scheduled) {\n rollbackLocation = currentLocation\n }\n\n // Update the location in memory\n currentLocation = parseHref(destHref, state)\n\n // Keep track of the next location we need to flush to the URL\n next = {\n href,\n state,\n isPush: next?.isPush || type === 'push',\n }\n\n // Notify subscribers\n onUpdate()\n\n if (!scheduled) {\n // Schedule an update to the browser history\n scheduled = Promise.resolve().then(() => flush())\n }\n }\n\n const onPushPop = () => {\n currentLocation = parseLocation()\n history.notify()\n }\n\n var originalPushState = win.history.pushState\n var originalReplaceState = win.history.replaceState\n\n const history = createHistory({\n getLocation,\n pushState: (href, state, onUpdate) =>\n queueHistoryAction('push', href, state, onUpdate),\n replaceState: (href, state, onUpdate) =>\n queueHistoryAction('replace', href, state, onUpdate),\n back: () => win.history.back(),\n forward: () => win.history.forward(),\n go: (n) => win.history.go(n),\n createHref: (href) => createHref(href),\n flush,\n destroy: () => {\n win.history.pushState = originalPushState\n win.history.replaceState = originalReplaceState\n win.removeEventListener(pushStateEvent, onPushPop)\n win.removeEventListener(popStateEvent, onPushPop)\n },\n onBlocked: (onUpdate) => {\n // If a navigation is blocked, we need to rollback the location\n // that we optimistically updated in memory.\n if (rollbackLocation && currentLocation !== rollbackLocation) {\n currentLocation = rollbackLocation\n // Notify subscribers\n onUpdate()\n }\n },\n })\n\n win.addEventListener(pushStateEvent, onPushPop)\n win.addEventListener(popStateEvent, onPushPop)\n\n win.history.pushState = function () {\n let res = originalPushState.apply(win.history, arguments as any)\n if (tracking) history.notify()\n return res\n }\n\n win.history.replaceState = function () {\n let res = originalReplaceState.apply(win.history, arguments as any)\n if (tracking) history.notify()\n return res\n }\n\n return history\n}\n\nexport function createHashHistory(opts?: { window?: any }): RouterHistory {\n const win =\n opts?.window ??\n (typeof document !== 'undefined' ? window : (undefined as any))\n return createBrowserHistory({\n window: win,\n parseLocation: () => {\n const hashHref = win.location.hash.split('#').slice(1).join('#') ?? '/'\n return parseHref(hashHref, win.history.state)\n },\n createHref: (href) =>\n `${win.location.pathname}${win.location.search}#${href}`,\n })\n}\n\nexport function createMemoryHistory(\n opts: {\n initialEntries: string[]\n initialIndex?: number\n } = {\n initialEntries: ['/'],\n },\n): RouterHistory {\n const entries = opts.initialEntries\n let index = opts.initialIndex ?? entries.length - 1\n let currentState = {\n key: createRandomKey(),\n } as HistoryState\n\n const getLocation = () => parseHref(entries[index]!, currentState)\n\n return createHistory({\n getLocation,\n pushState: (path, state) => {\n currentState = state\n entries.push(path)\n index++\n },\n replaceState: (path, state) => {\n currentState = state\n entries[index] = path\n },\n back: () => {\n index--\n },\n forward: () => {\n index = Math.min(index + 1, entries.length - 1)\n },\n go: (n) => {\n index = Math.min(Math.max(index + n, 0), entries.length - 1)\n },\n createHref: (path) => path,\n })\n}\n\nfunction parseHref(href: string, state: HistoryState): HistoryLocation {\n let hashIndex = href.indexOf('#')\n let searchIndex = href.indexOf('?')\n\n return {\n href,\n pathname: href.substring(\n 0,\n hashIndex > 0\n ? searchIndex > 0\n ? Math.min(hashIndex, searchIndex)\n : hashIndex\n : searchIndex > 0\n ? searchIndex\n : href.length,\n ),\n hash: hashIndex > -1 ? href.substring(hashIndex) : '',\n search:\n searchIndex > -1\n ? href.slice(searchIndex, hashIndex === -1 ? undefined : hashIndex)\n : '',\n state: state || {},\n }\n}\n\n// Thanks co-pilot!\nfunction createRandomKey() {\n return (Math.random() + 1).toString(36).substring(7)\n}\n"],"names":["pushStateEvent","popStateEvent","beforeUnloadEvent","beforeUnloadListener","event","preventDefault","returnValue","stopBlocking","removeEventListener","capture","createHistory","opts","location","getLocation","subscribers","Set","blockers","onUpdate","forEach","subscriber","tryNavigation","task","document","length","blocker","allowed","onBlocked","subscribe","cb","add","delete","push","path","state","assignKey","pushState","replace","replaceState","go","index","back","forward","createHref","str","block","addEventListener","filter","b","flush","destroy","notify","key","createRandomKey","createBrowserHistory","win","window","undefined","parseLocation","parseHref","pathname","search","hash","history","currentLocation","rollbackLocation","next","tracking","scheduled","untrack","fn","isPush","href","queueHistoryAction","type","destHref","Promise","resolve","then","onPushPop","originalPushState","originalReplaceState","n","res","apply","arguments","createHashHistory","hashHref","split","slice","join","createMemoryHistory","initialEntries","entries","initialIndex","currentState","Math","min","max","hashIndex","indexOf","searchIndex","substring","random","toString"],"mappings":";;;;;;;;;;;;AAAA;AACA;AACA;;AAsCA,MAAMA,cAAc,GAAG,WAAW,CAAA;AAClC,MAAMC,aAAa,GAAG,UAAU,CAAA;AAChC,MAAMC,iBAAiB,GAAG,cAAc,CAAA;AAExC,MAAMC,oBAAoB,GAAIC,KAAY,IAAK;EAC7CA,KAAK,CAACC,cAAc,EAAE,CAAA;AACtB;AACA,EAAA,OAAQD,KAAK,CAACE,WAAW,GA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C,CAAA;EAEnC,OAAO;IACL5B,IAAI;AACJZ,IAAAA,QAAQ,EAAEY,IAAI,CAAC8B,SAAS,CACtB,CAAC,EACDH,SAAS,GAAG,CAAC,GACTE,WAAW,GAAG,CAAC,GACbL,IAAI,CAACC,GAAG,CAACE,SAAS,EAAEE,WAAW,CAAC,GAChCF,SAAS,GACXE,WAAW,GAAG,CAAC,GACbA,WAAW,GACX7B,IAAI,CAAChD,MACb,CAAC;AACDsC,IAAAA,IAAI,EAAEqC,SAAS,GAAG,CAAC,CAAC,GAAG3B,IAAI,CAAC8B,SAAS,CAACH,SAAS,CAAC,GAAG,EAAE;IACrDtC,MAAM,EACJwC,WAAW,GAAG,CAAC,CAAC,GACZ7B,IAAI,CAACiB,KAAK,CAACY,WAAW,EAAEF,SAAS,KAAK,CAAC,CAAC,GAAG1C,SAAS,GAAG0C,SAAS,CAAC,GACjE,EAAE;IACRjE,KAAK,EAAEA,KAAK,IAAI,EAAC;GAClB,CAAA;AACH,CAAA;;AAEA;AACA,SAASmB,eAAeA,GAAG;AACzB,EAAA,OAAO,CAAC2C,IAAI,CAACO,MAAM,EAAE,GAAG,CAAC,EAAEC,QAAQ,CAAC,EAAE,CAAC,CAACF,SAAS,CAAC,CAAC,CAAC,CAAA;AACtD;;;;;;;"}
1
+ {"version":3,"file":"index.js","sources":["../../src/index.ts"],"sourcesContent":["// While the public API was clearly inspired by the \"history\" npm package,\n// This implementation attempts to be more lightweight by\n// making assumptions about the way TanStack Router works\n\nexport interface RouterHistory {\n location: HistoryLocation\n subscribe: (cb: () => void) => () => void\n push: (path: string, state?: any) => void\n replace: (path: string, state?: any) => void\n go: (index: number) => void\n back: () => void\n forward: () => void\n createHref: (href: string) => string\n block: (blocker: BlockerFn) => () => void\n flush: () => void\n destroy: () => void\n notify: () => void\n}\n\nexport interface HistoryLocation extends ParsedPath {\n state: HistoryState\n}\n\nexport interface ParsedPath {\n href: string\n pathname: string\n search: string\n hash: string\n}\n\nexport interface HistoryState {\n key: string\n}\n\ntype ShouldAllowNavigation = any\n\nexport type BlockerFn = () =>\n | Promise<ShouldAllowNavigation>\n | ShouldAllowNavigation\n\nconst pushStateEvent = 'pushstate'\nconst popStateEvent = 'popstate'\nconst beforeUnloadEvent = 'beforeunload'\n\nconst beforeUnloadListener = (event: Event) => {\n event.preventDefault()\n // @ts-ignore\n return (event.returnValue = '')\n}\n\nconst stopBlocking = () => {\n removeEventListener(beforeUnloadEvent, beforeUnloadListener, {\n capture: true,\n })\n}\n\nexport function createHistory(opts: {\n getLocation: () => HistoryLocation\n pushState: (path: string, state: any) => void\n replaceState: (path: string, state: any) => void\n go: (n: number) => void\n back: () => void\n forward: () => void\n createHref: (path: string) => string\n flush?: () => void\n destroy?: () => void\n onBlocked?: (onUpdate: () => void) => void\n}): RouterHistory {\n let location = opts.getLocation()\n let subscribers = new Set<() => void>()\n let blockers: BlockerFn[] = []\n\n const onUpdate = () => {\n location = opts.getLocation()\n subscribers.forEach((subscriber) => subscriber())\n }\n\n const tryNavigation = async (task: () => void) => {\n if (typeof document !== 'undefined' && blockers.length) {\n for (let blocker of blockers) {\n const allowed = await blocker()\n if (!allowed) {\n opts.onBlocked?.(onUpdate)\n return\n }\n }\n }\n\n task()\n }\n\n return {\n get location() {\n return location\n },\n subscribe: (cb: () => void) => {\n subscribers.add(cb)\n\n return () => {\n subscribers.delete(cb)\n }\n },\n push: (path: string, state: any) => {\n state = assignKey(state)\n tryNavigation(() => {\n opts.pushState(path, state)\n onUpdate()\n })\n },\n replace: (path: string, state: any) => {\n state = assignKey(state)\n tryNavigation(() => {\n opts.replaceState(path, state)\n onUpdate()\n })\n },\n go: (index) => {\n tryNavigation(() => {\n opts.go(index)\n })\n },\n back: () => {\n tryNavigation(() => {\n opts.back()\n })\n },\n forward: () => {\n tryNavigation(() => {\n opts.forward()\n })\n },\n createHref: (str) => opts.createHref(str),\n block: (blocker) => {\n blockers.push(blocker)\n\n if (blockers.length === 1) {\n addEventListener(beforeUnloadEvent, beforeUnloadListener, {\n capture: true,\n })\n }\n\n return () => {\n blockers = blockers.filter((b) => b !== blocker)\n\n if (!blockers.length) {\n stopBlocking()\n }\n }\n },\n flush: () => opts.flush?.(),\n destroy: () => opts.destroy?.(),\n notify: onUpdate,\n }\n}\n\nfunction assignKey(state: HistoryState) {\n if (!state) {\n state = {} as HistoryState\n }\n return {\n ...state,\n key: createRandomKey(),\n }\n}\n\n/**\n * Creates a history object that can be used to interact with the browser's\n * navigation. This is a lightweight API wrapping the browser's native methods.\n * It is designed to work with TanStack Router, but could be used as a standalone API as well.\n * IMPORTANT: This API implements history throttling via a microtask to prevent\n * excessive calls to the history API. In some browsers, calling history.pushState or\n * history.replaceState in quick succession can cause the browser to ignore subsequent\n * calls. This API smooths out those differences and ensures that your application\n * state will *eventually* match the browser state. In most cases, this is not a problem,\n * but if you need to ensure that the browser state is up to date, you can use the\n * `history.flush` method to immediately flush all pending state changes to the browser URL.\n * @param opts\n * @param opts.getHref A function that returns the current href (path + search + hash)\n * @param opts.createHref A function that takes a path and returns a href (path + search + hash)\n * @returns A history instance\n */\nexport function createBrowserHistory(opts?: {\n parseLocation?: () => HistoryLocation\n createHref?: (path: string) => string\n window?: any\n}): RouterHistory {\n const win =\n opts?.window ??\n (typeof document !== 'undefined' ? window : (undefined as any))\n\n const createHref = opts?.createHref ?? ((path) => path)\n const parseLocation =\n opts?.parseLocation ??\n (() =>\n parseHref(\n `${win.location.pathname}${win.location.search}${win.location.hash}`,\n win.history.state,\n ))\n\n let currentLocation = parseLocation()\n let rollbackLocation: HistoryLocation | undefined\n\n const getLocation = () => currentLocation\n\n let next:\n | undefined\n | {\n // This is the latest location that we were attempting to push/replace\n href: string\n // This is the latest state that we were attempting to push/replace\n state: any\n // This is the latest type that we were attempting to push/replace\n isPush: boolean\n }\n\n // Because we are proactively updating the location\n // in memory before actually updating the browser history,\n // we need to track when we are doing this so we don't\n // notify subscribers twice on the last update.\n let tracking = true\n\n // We need to track the current scheduled update to prevent\n // multiple updates from being scheduled at the same time.\n let scheduled: Promise<void> | undefined\n\n // This function is a wrapper to prevent any of the callback's\n // side effects from causing a subscriber notification\n const untrack = (fn: () => void) => {\n tracking = false\n fn()\n tracking = true\n }\n\n // This function flushes the next update to the browser history\n const flush = () => {\n // Do not notify subscribers about this push/replace call\n untrack(() => {\n if (!next) return\n win.history[next.isPush ? 'pushState' : 'replaceState'](\n next.state,\n '',\n next.href,\n )\n // Reset the nextIsPush flag and clear the scheduled update\n next = undefined\n scheduled = undefined\n rollbackLocation = undefined\n })\n }\n\n // This function queues up a call to update the browser history\n const queueHistoryAction = (\n type: 'push' | 'replace',\n destHref: string,\n state: any,\n ) => {\n const href = createHref(destHref)\n\n if (!scheduled) {\n rollbackLocation = currentLocation\n }\n\n // Update the location in memory\n currentLocation = parseHref(destHref, state)\n\n // Keep track of the next location we need to flush to the URL\n next = {\n href,\n state,\n isPush: next?.isPush || type === 'push',\n }\n\n if (!scheduled) {\n // Schedule an update to the browser history\n scheduled = Promise.resolve().then(() => flush())\n }\n }\n\n const onPushPop = () => {\n currentLocation = parseLocation()\n history.notify()\n }\n\n var originalPushState = win.history.pushState\n var originalReplaceState = win.history.replaceState\n\n const history = createHistory({\n getLocation,\n pushState: (href, state) => queueHistoryAction('push', href, state),\n replaceState: (href, state) => queueHistoryAction('replace', href, state),\n back: () => win.history.back(),\n forward: () => win.history.forward(),\n go: (n) => win.history.go(n),\n createHref: (href) => createHref(href),\n flush,\n destroy: () => {\n win.history.pushState = originalPushState\n win.history.replaceState = originalReplaceState\n win.removeEventListener(pushStateEvent, onPushPop)\n win.removeEventListener(popStateEvent, onPushPop)\n },\n onBlocked: (onUpdate) => {\n // If a navigation is blocked, we need to rollback the location\n // that we optimistically updated in memory.\n if (rollbackLocation && currentLocation !== rollbackLocation) {\n currentLocation = rollbackLocation\n // Notify subscribers\n onUpdate()\n }\n },\n })\n\n win.addEventListener(pushStateEvent, onPushPop)\n win.addEventListener(popStateEvent, onPushPop)\n\n win.history.pushState = function () {\n let res = originalPushState.apply(win.history, arguments as any)\n if (tracking) history.notify()\n return res\n }\n\n win.history.replaceState = function () {\n let res = originalReplaceState.apply(win.history, arguments as any)\n if (tracking) history.notify()\n return res\n }\n\n return history\n}\n\nexport function createHashHistory(opts?: { window?: any }): RouterHistory {\n const win =\n opts?.window ??\n (typeof document !== 'undefined' ? window : (undefined as any))\n return createBrowserHistory({\n window: win,\n parseLocation: () => {\n const hashHref = win.location.hash.split('#').slice(1).join('#') ?? '/'\n return parseHref(hashHref, win.history.state)\n },\n createHref: (href) =>\n `${win.location.pathname}${win.location.search}#${href}`,\n })\n}\n\nexport function createMemoryHistory(\n opts: {\n initialEntries: string[]\n initialIndex?: number\n } = {\n initialEntries: ['/'],\n },\n): RouterHistory {\n const entries = opts.initialEntries\n let index = opts.initialIndex ?? entries.length - 1\n let currentState = {\n key: createRandomKey(),\n } as HistoryState\n\n const getLocation = () => parseHref(entries[index]!, currentState)\n\n return createHistory({\n getLocation,\n\n pushState: (path, state) => {\n currentState = state\n entries.push(path)\n index++\n },\n replaceState: (path, state) => {\n currentState = state\n entries[index] = path\n },\n back: () => {\n index--\n },\n forward: () => {\n index = Math.min(index + 1, entries.length - 1)\n },\n go: (n) => {\n index = Math.min(Math.max(index + n, 0), entries.length - 1)\n },\n createHref: (path) => path,\n })\n}\n\nfunction parseHref(href: string, state: HistoryState): HistoryLocation {\n let hashIndex = href.indexOf('#')\n let searchIndex = href.indexOf('?')\n\n return {\n href,\n pathname: href.substring(\n 0,\n hashIndex > 0\n ? searchIndex > 0\n ? Math.min(hashIndex, searchIndex)\n : hashIndex\n : searchIndex > 0\n ? searchIndex\n : href.length,\n ),\n hash: hashIndex > -1 ? href.substring(hashIndex) : '',\n search:\n searchIndex > -1\n ? href.slice(searchIndex, hashIndex === -1 ? undefined : hashIndex)\n : '',\n state: state || {},\n }\n}\n\n// Thanks co-pilot!\nfunction createRandomKey() {\n return (Math.random() + 1).toString(36).substring(7)\n}\n"],"names":["pushStateEvent","popStateEvent","beforeUnloadEvent","beforeUnloadListener","event","preventDefault","returnValue","stopBlocking","removeEventListener","capture","createHistory","opts","location","getLocation","subscribers","Set","blockers","onUpdate","forEach","subscriber","tryNavigation","task","document","length","blocker","allowed","onBlocked","subscribe","cb","add","delete","push","path","state","assignKey","pushState","replace","replaceState","go","index","back","forward","createHref","str","block","addEventListener","filter","b","flush","destroy","notify","key","createRandomKey","createBrowserHistory","win","window","undefined","parseLocation","parseHref","pathname","search","hash","history","currentLocation","rollbackLocation","next","tracking","scheduled","untrack","fn","isPush","href","queueHistoryAction","type","destHref","Promise","resolve","then","onPushPop","originalPushState","originalReplaceState","n","res","apply","arguments","createHashHistory","hashHref","split","slice","join","createMemoryHistory","initialEntries","entries","initialIndex","currentState","Math","min","max","hashIndex","indexOf","searchIndex","substring","random","toString"],"mappings":";;;;;;;;;;;;AAAA;AACA;AACA;;AAsCA,MAAMA,cAAc,GAAG,WAAW,CAAA;AAClC,MAAMC,aAAa,GAAG,UAAU,CAAA;AAChC,MAAMC,iBAAiB,GAAG,cAAc,CAAA;AAExC,MAAMC,oBAAoB,GAAIC,KAAY,IAAK;EAC7CA,KAAK,CAACC,cAAc,EAAE,CAAA;AACtB;AACA,EAAA,OAAQD,KAAK,CAACE,WAAW,GAAG,EAAE,CAAA;AAChC,CAAC,CAAA;AAED,MAAMC,YAAY,GAAGA,MAAM;AACzBC,EAAAA,mBAAmB,CAACN,iBAAiB,EAAEC,oBAAoB,EAAE;AAC3DM,IAAAA,OAAO,EAAE,IAAA;AACX,GAAC,CAAC,CAAA;AACJ,CAAC,CAAA;AAEM,SAASC,aAAaA,CAACC,IAW7B,EAAiB;AAChB,EAAA,IAAIC,QAAQ,GAAGD,IAAI,CAACE,WAAW,EAAE,CAAA;AACjC,EAAA,IAAIC,WAAW,GAAG,IAAIC,GAAG,EAAc,CAAA;EACvC,IAAIC,QAAqB,GAAG,EAAE,CAAA;EAE9B,MAAMC,QAAQ,GAAGA,MAAM;AACrBL,IAAAA,QAAQ,GA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AAG0C,SAAS,CAAC,GACjE,EAAE;IACRjE,KAAK,EAAEA,KAAK,IAAI,EAAC;GAClB,CAAA;AACH,CAAA;;AAEA;AACA,SAASmB,eAAeA,GAAG;AACzB,EAAA,OAAO,CAAC2C,IAAI,CAACO,MAAM,EAAE,GAAG,CAAC,EAAEC,QAAQ,CAAC,EAAE,CAAC,CAACF,SAAS,CAAC,CAAC,CAAC,CAAA;AACtD;;;;;;;"}
@@ -58,13 +58,15 @@ function createHistory(opts) {
58
58
  push: (path, state) => {
59
59
  state = assignKey(state);
60
60
  tryNavigation(() => {
61
- opts.pushState(path, state, onUpdate);
61
+ opts.pushState(path, state);
62
+ onUpdate();
62
63
  });
63
64
  },
64
65
  replace: (path, state) => {
65
66
  state = assignKey(state);
66
67
  tryNavigation(() => {
67
- opts.replaceState(path, state, onUpdate);
68
+ opts.replaceState(path, state);
69
+ onUpdate();
68
70
  });
69
71
  },
70
72
  go: index => {
@@ -169,7 +171,7 @@ function createBrowserHistory(opts) {
169
171
  };
170
172
 
171
173
  // This function queues up a call to update the browser history
172
- const queueHistoryAction = (type, destHref, state, onUpdate) => {
174
+ const queueHistoryAction = (type, destHref, state) => {
173
175
  const href = createHref(destHref);
174
176
  if (!scheduled) {
175
177
  rollbackLocation = currentLocation;
@@ -184,9 +186,6 @@ function createBrowserHistory(opts) {
184
186
  state,
185
187
  isPush: next?.isPush || type === 'push'
186
188
  };
187
-
188
- // Notify subscribers
189
- onUpdate();
190
189
  if (!scheduled) {
191
190
  // Schedule an update to the browser history
192
191
  scheduled = Promise.resolve().then(() => flush());
@@ -200,8 +199,8 @@ function createBrowserHistory(opts) {
200
199
  var originalReplaceState = win.history.replaceState;
201
200
  const history = createHistory({
202
201
  getLocation,
203
- pushState: (href, state, onUpdate) => queueHistoryAction('push', href, state, onUpdate),
204
- replaceState: (href, state, onUpdate) => queueHistoryAction('replace', href, state, onUpdate),
202
+ pushState: (href, state) => queueHistoryAction('push', href, state),
203
+ replaceState: (href, state) => queueHistoryAction('replace', href, state),
205
204
  back: () => win.history.back(),
206
205
  forward: () => win.history.forward(),
207
206
  go: n => win.history.go(n),
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../../src/index.ts"],"sourcesContent":["// While the public API was clearly inspired by the \"history\" npm package,\n// This implementation attempts to be more lightweight by\n// making assumptions about the way TanStack Router works\n\nexport interface RouterHistory {\n location: HistoryLocation\n subscribe: (cb: () => void) => () => void\n push: (path: string, state?: any) => void\n replace: (path: string, state?: any) => void\n go: (index: number) => void\n back: () => void\n forward: () => void\n createHref: (href: string) => string\n block: (blocker: BlockerFn) => () => void\n flush: () => void\n destroy: () => void\n notify: () => void\n}\n\nexport interface HistoryLocation extends ParsedPath {\n state: HistoryState\n}\n\nexport interface ParsedPath {\n href: string\n pathname: string\n search: string\n hash: string\n}\n\nexport interface HistoryState {\n key: string\n}\n\ntype ShouldAllowNavigation = any\n\nexport type BlockerFn = () =>\n | Promise<ShouldAllowNavigation>\n | ShouldAllowNavigation\n\nconst pushStateEvent = 'pushstate'\nconst popStateEvent = 'popstate'\nconst beforeUnloadEvent = 'beforeunload'\n\nconst beforeUnloadListener = (event: Event) => {\n event.preventDefault()\n // @ts-ignore\n return (event.returnValue = '')\n}\n\nconst stopBlocking = () => {\n removeEventListener(beforeUnloadEvent, beforeUnloadListener, {\n capture: true,\n })\n}\n\nexport function createHistory(opts: {\n getLocation: () => HistoryLocation\n pushState: (path: string, state: any, onUpdate: () => void) => void\n replaceState: (path: string, state: any, onUpdate: () => void) => void\n go: (n: number) => void\n back: () => void\n forward: () => void\n createHref: (path: string) => string\n flush?: () => void\n destroy?: () => void\n onBlocked?: (onUpdate: () => void) => void\n}): RouterHistory {\n let location = opts.getLocation()\n let subscribers = new Set<() => void>()\n let blockers: BlockerFn[] = []\n\n const onUpdate = () => {\n location = opts.getLocation()\n subscribers.forEach((subscriber) => subscriber())\n }\n\n const tryNavigation = async (task: () => void) => {\n if (typeof document !== 'undefined' && blockers.length) {\n for (let blocker of blockers) {\n const allowed = await blocker()\n if (!allowed) {\n opts.onBlocked?.(onUpdate)\n return\n }\n }\n }\n\n task()\n }\n\n return {\n get location() {\n return location\n },\n subscribe: (cb: () => void) => {\n subscribers.add(cb)\n\n return () => {\n subscribers.delete(cb)\n }\n },\n push: (path: string, state: any) => {\n state = assignKey(state)\n tryNavigation(() => {\n opts.pushState(path, state, onUpdate)\n })\n },\n replace: (path: string, state: any) => {\n state = assignKey(state)\n tryNavigation(() => {\n opts.replaceState(path, state, onUpdate)\n })\n },\n go: (index) => {\n tryNavigation(() => {\n opts.go(index)\n })\n },\n back: () => {\n tryNavigation(() => {\n opts.back()\n })\n },\n forward: () => {\n tryNavigation(() => {\n opts.forward()\n })\n },\n createHref: (str) => opts.createHref(str),\n block: (blocker) => {\n blockers.push(blocker)\n\n if (blockers.length === 1) {\n addEventListener(beforeUnloadEvent, beforeUnloadListener, {\n capture: true,\n })\n }\n\n return () => {\n blockers = blockers.filter((b) => b !== blocker)\n\n if (!blockers.length) {\n stopBlocking()\n }\n }\n },\n flush: () => opts.flush?.(),\n destroy: () => opts.destroy?.(),\n notify: onUpdate,\n }\n}\n\nfunction assignKey(state: HistoryState) {\n if (!state) {\n state = {} as HistoryState\n }\n return {\n ...state,\n key: createRandomKey(),\n }\n}\n\n/**\n * Creates a history object that can be used to interact with the browser's\n * navigation. This is a lightweight API wrapping the browser's native methods.\n * It is designed to work with TanStack Router, but could be used as a standalone API as well.\n * IMPORTANT: This API implements history throttling via a microtask to prevent\n * excessive calls to the history API. In some browsers, calling history.pushState or\n * history.replaceState in quick succession can cause the browser to ignore subsequent\n * calls. This API smooths out those differences and ensures that your application\n * state will *eventually* match the browser state. In most cases, this is not a problem,\n * but if you need to ensure that the browser state is up to date, you can use the\n * `history.flush` method to immediately flush all pending state changes to the browser URL.\n * @param opts\n * @param opts.getHref A function that returns the current href (path + search + hash)\n * @param opts.createHref A function that takes a path and returns a href (path + search + hash)\n * @returns A history instance\n */\nexport function createBrowserHistory(opts?: {\n parseLocation?: () => HistoryLocation\n createHref?: (path: string) => string\n window?: any\n}): RouterHistory {\n const win =\n opts?.window ??\n (typeof document !== 'undefined' ? window : (undefined as any))\n\n const createHref = opts?.createHref ?? ((path) => path)\n const parseLocation =\n opts?.parseLocation ??\n (() =>\n parseHref(\n `${win.location.pathname}${win.location.search}${win.location.hash}`,\n win.history.state,\n ))\n\n let currentLocation = parseLocation()\n let rollbackLocation: HistoryLocation | undefined\n\n const getLocation = () => currentLocation\n\n let next:\n | undefined\n | {\n // This is the latest location that we were attempting to push/replace\n href: string\n // This is the latest state that we were attempting to push/replace\n state: any\n // This is the latest type that we were attempting to push/replace\n isPush: boolean\n }\n\n // Because we are proactively updating the location\n // in memory before actually updating the browser history,\n // we need to track when we are doing this so we don't\n // notify subscribers twice on the last update.\n let tracking = true\n\n // We need to track the current scheduled update to prevent\n // multiple updates from being scheduled at the same time.\n let scheduled: Promise<void> | undefined\n\n // This function is a wrapper to prevent any of the callback's\n // side effects from causing a subscriber notification\n const untrack = (fn: () => void) => {\n tracking = false\n fn()\n tracking = true\n }\n\n // This function flushes the next update to the browser history\n const flush = () => {\n // Do not notify subscribers about this push/replace call\n untrack(() => {\n if (!next) return\n win.history[next.isPush ? 'pushState' : 'replaceState'](\n next.state,\n '',\n next.href,\n )\n // Reset the nextIsPush flag and clear the scheduled update\n next = undefined\n scheduled = undefined\n rollbackLocation = undefined\n })\n }\n\n // This function queues up a call to update the browser history\n const queueHistoryAction = (\n type: 'push' | 'replace',\n destHref: string,\n state: any,\n onUpdate: () => void,\n ) => {\n const href = createHref(destHref)\n\n if (!scheduled) {\n rollbackLocation = currentLocation\n }\n\n // Update the location in memory\n currentLocation = parseHref(destHref, state)\n\n // Keep track of the next location we need to flush to the URL\n next = {\n href,\n state,\n isPush: next?.isPush || type === 'push',\n }\n\n // Notify subscribers\n onUpdate()\n\n if (!scheduled) {\n // Schedule an update to the browser history\n scheduled = Promise.resolve().then(() => flush())\n }\n }\n\n const onPushPop = () => {\n currentLocation = parseLocation()\n history.notify()\n }\n\n var originalPushState = win.history.pushState\n var originalReplaceState = win.history.replaceState\n\n const history = createHistory({\n getLocation,\n pushState: (href, state, onUpdate) =>\n queueHistoryAction('push', href, state, onUpdate),\n replaceState: (href, state, onUpdate) =>\n queueHistoryAction('replace', href, state, onUpdate),\n back: () => win.history.back(),\n forward: () => win.history.forward(),\n go: (n) => win.history.go(n),\n createHref: (href) => createHref(href),\n flush,\n destroy: () => {\n win.history.pushState = originalPushState\n win.history.replaceState = originalReplaceState\n win.removeEventListener(pushStateEvent, onPushPop)\n win.removeEventListener(popStateEvent, onPushPop)\n },\n onBlocked: (onUpdate) => {\n // If a navigation is blocked, we need to rollback the location\n // that we optimistically updated in memory.\n if (rollbackLocation && currentLocation !== rollbackLocation) {\n currentLocation = rollbackLocation\n // Notify subscribers\n onUpdate()\n }\n },\n })\n\n win.addEventListener(pushStateEvent, onPushPop)\n win.addEventListener(popStateEvent, onPushPop)\n\n win.history.pushState = function () {\n let res = originalPushState.apply(win.history, arguments as any)\n if (tracking) history.notify()\n return res\n }\n\n win.history.replaceState = function () {\n let res = originalReplaceState.apply(win.history, arguments as any)\n if (tracking) history.notify()\n return res\n }\n\n return history\n}\n\nexport function createHashHistory(opts?: { window?: any }): RouterHistory {\n const win =\n opts?.window ??\n (typeof document !== 'undefined' ? window : (undefined as any))\n return createBrowserHistory({\n window: win,\n parseLocation: () => {\n const hashHref = win.location.hash.split('#').slice(1).join('#') ?? '/'\n return parseHref(hashHref, win.history.state)\n },\n createHref: (href) =>\n `${win.location.pathname}${win.location.search}#${href}`,\n })\n}\n\nexport function createMemoryHistory(\n opts: {\n initialEntries: string[]\n initialIndex?: number\n } = {\n initialEntries: ['/'],\n },\n): RouterHistory {\n const entries = opts.initialEntries\n let index = opts.initialIndex ?? entries.length - 1\n let currentState = {\n key: createRandomKey(),\n } as HistoryState\n\n const getLocation = () => parseHref(entries[index]!, currentState)\n\n return createHistory({\n getLocation,\n pushState: (path, state) => {\n currentState = state\n entries.push(path)\n index++\n },\n replaceState: (path, state) => {\n currentState = state\n entries[index] = path\n },\n back: () => {\n index--\n },\n forward: () => {\n index = Math.min(index + 1, entries.length - 1)\n },\n go: (n) => {\n index = Math.min(Math.max(index + n, 0), entries.length - 1)\n },\n createHref: (path) => path,\n })\n}\n\nfunction parseHref(href: string, state: HistoryState): HistoryLocation {\n let hashIndex = href.indexOf('#')\n let searchIndex = href.indexOf('?')\n\n return {\n href,\n pathname: href.substring(\n 0,\n hashIndex > 0\n ? searchIndex > 0\n ? Math.min(hashIndex, searchIndex)\n : hashIndex\n : searchIndex > 0\n ? searchIndex\n : href.length,\n ),\n hash: hashIndex > -1 ? href.substring(hashIndex) : '',\n search:\n searchIndex > -1\n ? href.slice(searchIndex, hashIndex === -1 ? undefined : hashIndex)\n : '',\n state: state || {},\n }\n}\n\n// Thanks co-pilot!\nfunction createRandomKey() {\n return (Math.random() + 1).toString(36).substring(7)\n}\n"],"names":["pushStateEvent","popStateEvent","beforeUnloadEvent","beforeUnloadListener","event","preventDefault","returnValue","stopBlocking","removeEventListener","capture","createHistory","opts","location","getLocation","subscribers","Set","blockers","onUpdate","forEach","subscriber","tryNavigation","task","document","length","blocker","allowed","onBlocked","subscribe","cb","add","delete","push","path","state","assignKey","pushState","replace","replaceState","go","index","back","forward","createHref","str","block","addEventListener","filter","b","flush","destroy","notify","key","createRandomKey","createBrowserHistory","win","window","undefined","parseLocation","parseHref","pathname","search","hash","history","currentLocation","rollbackLocation","next","tracking","scheduled","untrack","fn","isPush","href","queueHistoryAction","type","destHref","Promise","resolve","then","onPushPop","originalPushState","originalReplaceState","n","res","apply","arguments","createHashHistory","hashHref","split","slice","join","createMemoryHistory","initialEntries","entries","initialIndex","currentState","Math","min","max","hashIndex","indexOf","searchIndex","substring","random","toString"],"mappings":";;;;;;;;;;AAAA;AACA;AACA;;AAsCA,MAAMA,cAAc,GAAG,WAAW,CAAA;AAClC,MAAMC,aAAa,GAAG,UAAU,CAAA;AA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AmB,EAAmB;AACrE,EAAA,IAAIiE,SAAS,GAAG3B,IAAI,CAAC4B,OAAO,CAAC,GAAG,CAAC,CAAA;AACjC,EAAA,IAAIC,WAAW,GAAG7B,IAAI,CAAC4B,OAAO,CAAC,GAAG,CAAC,CAAA;EAEnC,OAAO;IACL5B,IAAI;AACJZ,IAAAA,QAAQ,EAAEY,IAAI,CAAC8B,SAAS,CACtB,CAAC,EACDH,SAAS,GAAG,CAAC,GACTE,WAAW,GAAG,CAAC,GACbL,IAAI,CAACC,GAAG,CAACE,SAAS,EAAEE,WAAW,CAAC,GAChCF,SAAS,GACXE,WAAW,GAAG,CAAC,GACbA,WAAW,GACX7B,IAAI,CAAChD,MACb,CAAC;AACDsC,IAAAA,IAAI,EAAEqC,SAAS,GAAG,CAAC,CAAC,GAAG3B,IAAI,CAAC8B,SAAS,CAACH,SAAS,CAAC,GAAG,EAAE;IACrDtC,MAAM,EACJwC,WAAW,GAAG,CAAC,CAAC,GACZ7B,IAAI,CAACiB,KAAK,CAACY,WAAW,EAAEF,SAAS,KAAK,CAAC,CAAC,GAAG1C,SAAS,GAAG0C,SAAS,CAAC,GACjE,EAAE;IACRjE,KAAK,EAAEA,KAAK,IAAI,EAAC;GAClB,CAAA;AACH,CAAA;;AAEA;AACA,SAASmB,eAAeA,GAAG;AACzB,EAAA,OAAO,CAAC2C,IAAI,CAACO,MAAM,EAAE,GAAG,CAAC,EAAEC,QAAQ,CAAC,EAAE,CAAC,CAACF,SAAS,CAAC,CAAC,CAAC,CAAA;AACtD;;;;"}
1
+ {"version":3,"file":"index.js","sources":["../../src/index.ts"],"sourcesContent":["// While the public API was clearly inspired by the \"history\" npm package,\n// This implementation attempts to be more lightweight by\n// making assumptions about the way TanStack Router works\n\nexport interface RouterHistory {\n location: HistoryLocation\n subscribe: (cb: () => void) => () => void\n push: (path: string, state?: any) => void\n replace: (path: string, state?: any) => void\n go: (index: number) => void\n back: () => void\n forward: () => void\n createHref: (href: string) => string\n block: (blocker: BlockerFn) => () => void\n flush: () => void\n destroy: () => void\n notify: () => void\n}\n\nexport interface HistoryLocation extends ParsedPath {\n state: HistoryState\n}\n\nexport interface ParsedPath {\n href: string\n pathname: string\n search: string\n hash: string\n}\n\nexport interface HistoryState {\n key: string\n}\n\ntype ShouldAllowNavigation = any\n\nexport type BlockerFn = () =>\n | Promise<ShouldAllowNavigation>\n | ShouldAllowNavigation\n\nconst pushStateEvent = 'pushstate'\nconst popStateEvent = 'popstate'\nconst beforeUnloadEvent = 'beforeunload'\n\nconst beforeUnloadListener = (event: Event) => {\n event.preventDefault()\n // @ts-ignore\n return (event.returnValue = '')\n}\n\nconst stopBlocking = () => {\n removeEventListener(beforeUnloadEvent, beforeUnloadListener, {\n capture: true,\n })\n}\n\nexport function createHistory(opts: {\n getLocation: () => HistoryLocation\n pushState: (path: string, state: any) => void\n replaceState: (path: string, state: any) => void\n go: (n: number) => void\n back: () => void\n forward: () => void\n createHref: (path: string) => string\n flush?: () => void\n destroy?: () => void\n onBlocked?: (onUpdate: () => void) => void\n}): RouterHistory {\n let location = opts.getLocation()\n let subscribers = new Set<() => void>()\n let blockers: BlockerFn[] = []\n\n const onUpdate = () => {\n location = opts.getLocation()\n subscribers.forEach((subscriber) => subscriber())\n }\n\n const tryNavigation = async (task: () => void) => {\n if (typeof document !== 'undefined' && blockers.length) {\n for (let blocker of blockers) {\n const allowed = await blocker()\n if (!allowed) {\n opts.onBlocked?.(onUpdate)\n return\n }\n }\n }\n\n task()\n }\n\n return {\n get location() {\n return location\n },\n subscribe: (cb: () => void) => {\n subscribers.add(cb)\n\n return () => {\n subscribers.delete(cb)\n }\n },\n push: (path: string, state: any) => {\n state = assignKey(state)\n tryNavigation(() => {\n opts.pushState(path, state)\n onUpdate()\n })\n },\n replace: (path: string, state: any) => {\n state = assignKey(state)\n tryNavigation(() => {\n opts.replaceState(path, state)\n onUpdate()\n })\n },\n go: (index) => {\n tryNavigation(() => {\n opts.go(index)\n })\n },\n back: () => {\n tryNavigation(() => {\n opts.back()\n })\n },\n forward: () => {\n tryNavigation(() => {\n opts.forward()\n })\n },\n createHref: (str) => opts.createHref(str),\n block: (blocker) => {\n blockers.push(blocker)\n\n if (blockers.length === 1) {\n addEventListener(beforeUnloadEvent, beforeUnloadListener, {\n capture: true,\n })\n }\n\n return () => {\n blockers = blockers.filter((b) => b !== blocker)\n\n if (!blockers.length) {\n stopBlocking()\n }\n }\n },\n flush: () => opts.flush?.(),\n destroy: () => opts.destroy?.(),\n notify: onUpdate,\n }\n}\n\nfunction assignKey(state: HistoryState) {\n if (!state) {\n state = {} as HistoryState\n }\n return {\n ...state,\n key: createRandomKey(),\n }\n}\n\n/**\n * Creates a history object that can be used to interact with the browser's\n * navigation. This is a lightweight API wrapping the browser's native methods.\n * It is designed to work with TanStack Router, but could be used as a standalone API as well.\n * IMPORTANT: This API implements history throttling via a microtask to prevent\n * excessive calls to the history API. In some browsers, calling history.pushState or\n * history.replaceState in quick succession can cause the browser to ignore subsequent\n * calls. This API smooths out those differences and ensures that your application\n * state will *eventually* match the browser state. In most cases, this is not a problem,\n * but if you need to ensure that the browser state is up to date, you can use the\n * `history.flush` method to immediately flush all pending state changes to the browser URL.\n * @param opts\n * @param opts.getHref A function that returns the current href (path + search + hash)\n * @param opts.createHref A function that takes a path and returns a href (path + search + hash)\n * @returns A history instance\n */\nexport function createBrowserHistory(opts?: {\n parseLocation?: () => HistoryLocation\n createHref?: (path: string) => string\n window?: any\n}): RouterHistory {\n const win =\n opts?.window ??\n (typeof document !== 'undefined' ? window : (undefined as any))\n\n const createHref = opts?.createHref ?? ((path) => path)\n const parseLocation =\n opts?.parseLocation ??\n (() =>\n parseHref(\n `${win.location.pathname}${win.location.search}${win.location.hash}`,\n win.history.state,\n ))\n\n let currentLocation = parseLocation()\n let rollbackLocation: HistoryLocation | undefined\n\n const getLocation = () => currentLocation\n\n let next:\n | undefined\n | {\n // This is the latest location that we were attempting to push/replace\n href: string\n // This is the latest state that we were attempting to push/replace\n state: any\n // This is the latest type that we were attempting to push/replace\n isPush: boolean\n }\n\n // Because we are proactively updating the location\n // in memory before actually updating the browser history,\n // we need to track when we are doing this so we don't\n // notify subscribers twice on the last update.\n let tracking = true\n\n // We need to track the current scheduled update to prevent\n // multiple updates from being scheduled at the same time.\n let scheduled: Promise<void> | undefined\n\n // This function is a wrapper to prevent any of the callback's\n // side effects from causing a subscriber notification\n const untrack = (fn: () => void) => {\n tracking = false\n fn()\n tracking = true\n }\n\n // This function flushes the next update to the browser history\n const flush = () => {\n // Do not notify subscribers about this push/replace call\n untrack(() => {\n if (!next) return\n win.history[next.isPush ? 'pushState' : 'replaceState'](\n next.state,\n '',\n next.href,\n )\n // Reset the nextIsPush flag and clear the scheduled update\n next = undefined\n scheduled = undefined\n rollbackLocation = undefined\n })\n }\n\n // This function queues up a call to update the browser history\n const queueHistoryAction = (\n type: 'push' | 'replace',\n destHref: string,\n state: any,\n ) => {\n const href = createHref(destHref)\n\n if (!scheduled) {\n rollbackLocation = currentLocation\n }\n\n // Update the location in memory\n currentLocation = parseHref(destHref, state)\n\n // Keep track of the next location we need to flush to the URL\n next = {\n href,\n state,\n isPush: next?.isPush || type === 'push',\n }\n\n if (!scheduled) {\n // Schedule an update to the browser history\n scheduled = Promise.resolve().then(() => flush())\n }\n }\n\n const onPushPop = () => {\n currentLocation = parseLocation()\n history.notify()\n }\n\n var originalPushState = win.history.pushState\n var originalReplaceState = win.history.replaceState\n\n const history = createHistory({\n getLocation,\n pushState: (href, state) => queueHistoryAction('push', href, state),\n replaceState: (href, state) => queueHistoryAction('replace', href, state),\n back: () => win.history.back(),\n forward: () => win.history.forward(),\n go: (n) => win.history.go(n),\n createHref: (href) => createHref(href),\n flush,\n destroy: () => {\n win.history.pushState = originalPushState\n win.history.replaceState = originalReplaceState\n win.removeEventListener(pushStateEvent, onPushPop)\n win.removeEventListener(popStateEvent, onPushPop)\n },\n onBlocked: (onUpdate) => {\n // If a navigation is blocked, we need to rollback the location\n // that we optimistically updated in memory.\n if (rollbackLocation && currentLocation !== rollbackLocation) {\n currentLocation = rollbackLocation\n // Notify subscribers\n onUpdate()\n }\n },\n })\n\n win.addEventListener(pushStateEvent, onPushPop)\n win.addEventListener(popStateEvent, onPushPop)\n\n win.history.pushState = function () {\n let res = originalPushState.apply(win.history, arguments as any)\n if (tracking) history.notify()\n return res\n }\n\n win.history.replaceState = function () {\n let res = originalReplaceState.apply(win.history, arguments as any)\n if (tracking) history.notify()\n return res\n }\n\n return history\n}\n\nexport function createHashHistory(opts?: { window?: any }): RouterHistory {\n const win =\n opts?.window ??\n (typeof document !== 'undefined' ? window : (undefined as any))\n return createBrowserHistory({\n window: win,\n parseLocation: () => {\n const hashHref = win.location.hash.split('#').slice(1).join('#') ?? '/'\n return parseHref(hashHref, win.history.state)\n },\n createHref: (href) =>\n `${win.location.pathname}${win.location.search}#${href}`,\n })\n}\n\nexport function createMemoryHistory(\n opts: {\n initialEntries: string[]\n initialIndex?: number\n } = {\n initialEntries: ['/'],\n },\n): RouterHistory {\n const entries = opts.initialEntries\n let index = opts.initialIndex ?? entries.length - 1\n let currentState = {\n key: createRandomKey(),\n } as HistoryState\n\n const getLocation = () => parseHref(entries[index]!, currentState)\n\n return createHistory({\n getLocation,\n\n pushState: (path, state) => {\n currentState = state\n entries.push(path)\n index++\n },\n replaceState: (path, state) => {\n currentState = state\n entries[index] = path\n },\n back: () => {\n index--\n },\n forward: () => {\n index = Math.min(index + 1, entries.length - 1)\n },\n go: (n) => {\n index = Math.min(Math.max(index + n, 0), entries.length - 1)\n },\n createHref: (path) => path,\n })\n}\n\nfunction parseHref(href: string, state: HistoryState): HistoryLocation {\n let hashIndex = href.indexOf('#')\n let searchIndex = href.indexOf('?')\n\n return {\n href,\n pathname: href.substring(\n 0,\n hashIndex > 0\n ? searchIndex > 0\n ? Math.min(hashIndex, searchIndex)\n : hashIndex\n : searchIndex > 0\n ? searchIndex\n : href.length,\n ),\n hash: hashIndex > -1 ? href.substring(hashIndex) : '',\n search:\n searchIndex > -1\n ? href.slice(searchIndex, hashIndex === -1 ? undefined : hashIndex)\n : '',\n state: state || {},\n }\n}\n\n// Thanks co-pilot!\nfunction createRandomKey() {\n return (Math.random() + 1).toString(36).substring(7)\n}\n"],"names":["pushStateEvent","popStateEvent","beforeUnloadEvent","beforeUnloadListener","event","preventDefault","returnValue","stopBlocking","removeEventListener","capture","createHistory","opts","location","getLocation","subscribers","Set","blockers","onUpdate","forEach","subscriber","tryNavigation","task","document","length","blocker","allowed","onBlocked","subscribe","cb","add","delete","push","path","state","assignKey","pushState","replace","replaceState","go","index","back","forward","createHref","str","block","addEventListener","filter","b","flush","destroy","notify","key","createRandomKey","createBrowserHistory","win","window","undefined","parseLocation","parseHref","pathname","search","hash","history","currentLocation","rollbackLocation","next","tracking","scheduled","untrack","fn","isPush","href","queueHistoryAction","type","destHref","Promise","resolve","then","onPushPop","originalPushState","originalReplaceState","n","res","apply","arguments","createHashHistory","hashHref","split","slice","join","createMemoryHistory","initialEntries","entries","initialIndex","currentState","Math","min","max","hashIndex","indexOf","searchIndex","substring","random","toString"],"mappings":";;;;;;;;;;AAAA;AACA;AACA;;AAsCA,MAAMA,cAAc,GAAG,WAAW,CAAA;AAClC,MAAMC,aAAa,GAAG,UAAU,CAAA;AAChC,MAAMC,iBAAiB,GAAG,cAAc,CAAA;AAExC,MAAMC,oBAAoB,GAAIC,KAAY,IAAK;EAC7CA,KAAK,CAACC,cAAc,EAAE,CAAA;AACtB;AACA,EAAA,OAAQD,KAAK,CAACE,WAAW,GAAG,EAAE,CAAA;AAChC,CAAC,CAAA;AAED,MAAMC,YAAY,GAAGA,MAAM;AACzBC,EAAAA,mBAAmB,CAACN,iBAAiB,EAAEC,oBAAoB,EAAE;AAC3DM,IAAAA,OAAO,EAAE,IAAA;AACX,GAAC,CAAC,CAAA;AACJ,CAAC,CAAA;AAEM,SAASC,aAAaA,CAACC,IAW7B,EAAiB;AAChB,EAAA,IAAIC,QAAQ,GAAGD,IAAI,CAACE,WAAW,EAAE,CAAA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AAS,CAAC,GAAG,EAAE;IACrDtC,MAAM,EACJwC,WAAW,GAAG,CAAC,CAAC,GACZ7B,IAAI,CAACiB,KAAK,CAACY,WAAW,EAAEF,SAAS,KAAK,CAAC,CAAC,GAAG1C,SAAS,GAAG0C,SAAS,CAAC,GACjE,EAAE;IACRjE,KAAK,EAAEA,KAAK,IAAI,EAAC;GAClB,CAAA;AACH,CAAA;;AAEA;AACA,SAASmB,eAAeA,GAAG;AACzB,EAAA,OAAO,CAAC2C,IAAI,CAACO,MAAM,EAAE,GAAG,CAAC,EAAEC,QAAQ,CAAC,EAAE,CAAC,CAACF,SAAS,CAAC,CAAC,CAAC,CAAA;AACtD;;;;"}
@@ -4818,7 +4818,7 @@ var drawChart = (function (exports) {
4818
4818
  </script>
4819
4819
  <script>
4820
4820
  /*<!--*/
4821
- const data = {"version":2,"tree":{"name":"root","children":[{"name":"index.production.js","children":[{"name":"packages/history/src/index.ts","uid":"08b8-1"}]}],"isRoot":true},"nodeParts":{"08b8-1":{"renderedLength":9731,"gzipLength":2821,"brotliLength":0,"metaUid":"08b8-0"}},"nodeMetas":{"08b8-0":{"id":"/packages/history/src/index.ts","moduleParts":{"index.production.js":"08b8-1"},"imported":[],"importedBy":[],"isEntry":true}},"env":{"rollup":"4.6.1"},"options":{"gzip":true,"brotli":false,"sourcemap":false}};
4821
+ const data = {"version":2,"tree":{"name":"root","children":[{"name":"index.production.js","children":[{"name":"packages/history/src/index.ts","uid":"b65f-1"}]}],"isRoot":true},"nodeParts":{"b65f-1":{"renderedLength":9658,"gzipLength":2809,"brotliLength":0,"metaUid":"b65f-0"}},"nodeMetas":{"b65f-0":{"id":"/packages/history/src/index.ts","moduleParts":{"index.production.js":"b65f-1"},"imported":[],"importedBy":[],"isEntry":true}},"env":{"rollup":"4.6.1"},"options":{"gzip":true,"brotli":false,"sourcemap":false}};
4822
4822
 
4823
4823
  const run = () => {
4824
4824
  const width = window.innerWidth;
@@ -8,7 +8,7 @@
8
8
  "children": [
9
9
  {
10
10
  "name": "packages/history/src/index.ts",
11
- "uid": "08b8-3"
11
+ "uid": "b65f-3"
12
12
  }
13
13
  ]
14
14
  }
@@ -16,18 +16,18 @@
16
16
  "isRoot": true
17
17
  },
18
18
  "nodeParts": {
19
- "08b8-3": {
20
- "renderedLength": 9731,
21
- "gzipLength": 2821,
19
+ "b65f-3": {
20
+ "renderedLength": 9658,
21
+ "gzipLength": 2809,
22
22
  "brotliLength": 0,
23
- "metaUid": "08b8-2"
23
+ "metaUid": "b65f-2"
24
24
  }
25
25
  },
26
26
  "nodeMetas": {
27
- "08b8-2": {
27
+ "b65f-2": {
28
28
  "id": "/packages/history/src/index.ts",
29
29
  "moduleParts": {
30
- "index.production.js": "08b8-3"
30
+ "index.production.js": "b65f-3"
31
31
  },
32
32
  "imported": [],
33
33
  "importedBy": [],
@@ -38,8 +38,8 @@ type ShouldAllowNavigation = any;
38
38
  type BlockerFn = () => Promise<ShouldAllowNavigation> | ShouldAllowNavigation;
39
39
  declare function createHistory(opts: {
40
40
  getLocation: () => HistoryLocation;
41
- pushState: (path: string, state: any, onUpdate: () => void) => void;
42
- replaceState: (path: string, state: any, onUpdate: () => void) => void;
41
+ pushState: (path: string, state: any) => void;
42
+ replaceState: (path: string, state: any) => void;
43
43
  go: (n: number) => void;
44
44
  back: () => void;
45
45
  forward: () => void;
@@ -64,13 +64,15 @@
64
64
  push: (path, state) => {
65
65
  state = assignKey(state);
66
66
  tryNavigation(() => {
67
- opts.pushState(path, state, onUpdate);
67
+ opts.pushState(path, state);
68
+ onUpdate();
68
69
  });
69
70
  },
70
71
  replace: (path, state) => {
71
72
  state = assignKey(state);
72
73
  tryNavigation(() => {
73
- opts.replaceState(path, state, onUpdate);
74
+ opts.replaceState(path, state);
75
+ onUpdate();
74
76
  });
75
77
  },
76
78
  go: index => {
@@ -175,7 +177,7 @@
175
177
  };
176
178
 
177
179
  // This function queues up a call to update the browser history
178
- const queueHistoryAction = (type, destHref, state, onUpdate) => {
180
+ const queueHistoryAction = (type, destHref, state) => {
179
181
  const href = createHref(destHref);
180
182
  if (!scheduled) {
181
183
  rollbackLocation = currentLocation;
@@ -190,9 +192,6 @@
190
192
  state,
191
193
  isPush: next?.isPush || type === 'push'
192
194
  };
193
-
194
- // Notify subscribers
195
- onUpdate();
196
195
  if (!scheduled) {
197
196
  // Schedule an update to the browser history
198
197
  scheduled = Promise.resolve().then(() => flush());
@@ -206,8 +205,8 @@
206
205
  var originalReplaceState = win.history.replaceState;
207
206
  const history = createHistory({
208
207
  getLocation,
209
- pushState: (href, state, onUpdate) => queueHistoryAction('push', href, state, onUpdate),
210
- replaceState: (href, state, onUpdate) => queueHistoryAction('replace', href, state, onUpdate),
208
+ pushState: (href, state) => queueHistoryAction('push', href, state),
209
+ replaceState: (href, state) => queueHistoryAction('replace', href, state),
211
210
  back: () => win.history.back(),
212
211
  forward: () => win.history.forward(),
213
212
  go: n => win.history.go(n),
@@ -1 +1 @@
1
- {"version":3,"file":"index.development.js","sources":["../../src/index.ts"],"sourcesContent":["// While the public API was clearly inspired by the \"history\" npm package,\n// This implementation attempts to be more lightweight by\n// making assumptions about the way TanStack Router works\n\nexport interface RouterHistory {\n location: HistoryLocation\n subscribe: (cb: () => void) => () => void\n push: (path: string, state?: any) => void\n replace: (path: string, state?: any) => void\n go: (index: number) => void\n back: () => void\n forward: () => void\n createHref: (href: string) => string\n block: (blocker: BlockerFn) => () => void\n flush: () => void\n destroy: () => void\n notify: () => void\n}\n\nexport interface HistoryLocation extends ParsedPath {\n state: HistoryState\n}\n\nexport interface ParsedPath {\n href: string\n pathname: string\n search: string\n hash: string\n}\n\nexport interface HistoryState {\n key: string\n}\n\ntype ShouldAllowNavigation = any\n\nexport type BlockerFn = () =>\n | Promise<ShouldAllowNavigation>\n | ShouldAllowNavigation\n\nconst pushStateEvent = 'pushstate'\nconst popStateEvent = 'popstate'\nconst beforeUnloadEvent = 'beforeunload'\n\nconst beforeUnloadListener = (event: Event) => {\n event.preventDefault()\n // @ts-ignore\n return (event.returnValue = '')\n}\n\nconst stopBlocking = () => {\n removeEventListener(beforeUnloadEvent, beforeUnloadListener, {\n capture: true,\n })\n}\n\nexport function createHistory(opts: {\n getLocation: () => HistoryLocation\n pushState: (path: string, state: any, onUpdate: () => void) => void\n replaceState: (path: string, state: any, onUpdate: () => void) => void\n go: (n: number) => void\n back: () => void\n forward: () => void\n createHref: (path: string) => string\n flush?: () => void\n destroy?: () => void\n onBlocked?: (onUpdate: () => void) => void\n}): RouterHistory {\n let location = opts.getLocation()\n let subscribers = new Set<() => void>()\n let blockers: BlockerFn[] = []\n\n const onUpdate = () => {\n location = opts.getLocation()\n subscribers.forEach((subscriber) => subscriber())\n }\n\n const tryNavigation = async (task: () => void) => {\n if (typeof document !== 'undefined' && blockers.length) {\n for (let blocker of blockers) {\n const allowed = await blocker()\n if (!allowed) {\n opts.onBlocked?.(onUpdate)\n return\n }\n }\n }\n\n task()\n }\n\n return {\n get location() {\n return location\n },\n subscribe: (cb: () => void) => {\n subscribers.add(cb)\n\n return () => {\n subscribers.delete(cb)\n }\n },\n push: (path: string, state: any) => {\n state = assignKey(state)\n tryNavigation(() => {\n opts.pushState(path, state, onUpdate)\n })\n },\n replace: (path: string, state: any) => {\n state = assignKey(state)\n tryNavigation(() => {\n opts.replaceState(path, state, onUpdate)\n })\n },\n go: (index) => {\n tryNavigation(() => {\n opts.go(index)\n })\n },\n back: () => {\n tryNavigation(() => {\n opts.back()\n })\n },\n forward: () => {\n tryNavigation(() => {\n opts.forward()\n })\n },\n createHref: (str) => opts.createHref(str),\n block: (blocker) => {\n blockers.push(blocker)\n\n if (blockers.length === 1) {\n addEventListener(beforeUnloadEvent, beforeUnloadListener, {\n capture: true,\n })\n }\n\n return () => {\n blockers = blockers.filter((b) => b !== blocker)\n\n if (!blockers.length) {\n stopBlocking()\n }\n }\n },\n flush: () => opts.flush?.(),\n destroy: () => opts.destroy?.(),\n notify: onUpdate,\n }\n}\n\nfunction assignKey(state: HistoryState) {\n if (!state) {\n state = {} as HistoryState\n }\n return {\n ...state,\n key: createRandomKey(),\n }\n}\n\n/**\n * Creates a history object that can be used to interact with the browser's\n * navigation. This is a lightweight API wrapping the browser's native methods.\n * It is designed to work with TanStack Router, but could be used as a standalone API as well.\n * IMPORTANT: This API implements history throttling via a microtask to prevent\n * excessive calls to the history API. In some browsers, calling history.pushState or\n * history.replaceState in quick succession can cause the browser to ignore subsequent\n * calls. This API smooths out those differences and ensures that your application\n * state will *eventually* match the browser state. In most cases, this is not a problem,\n * but if you need to ensure that the browser state is up to date, you can use the\n * `history.flush` method to immediately flush all pending state changes to the browser URL.\n * @param opts\n * @param opts.getHref A function that returns the current href (path + search + hash)\n * @param opts.createHref A function that takes a path and returns a href (path + search + hash)\n * @returns A history instance\n */\nexport function createBrowserHistory(opts?: {\n parseLocation?: () => HistoryLocation\n createHref?: (path: string) => string\n window?: any\n}): RouterHistory {\n const win =\n opts?.window ??\n (typeof document !== 'undefined' ? window : (undefined as any))\n\n const createHref = opts?.createHref ?? ((path) => path)\n const parseLocation =\n opts?.parseLocation ??\n (() =>\n parseHref(\n `${win.location.pathname}${win.location.search}${win.location.hash}`,\n win.history.state,\n ))\n\n let currentLocation = parseLocation()\n let rollbackLocation: HistoryLocation | undefined\n\n const getLocation = () => currentLocation\n\n let next:\n | undefined\n | {\n // This is the latest location that we were attempting to push/replace\n href: string\n // This is the latest state that we were attempting to push/replace\n state: any\n // This is the latest type that we were attempting to push/replace\n isPush: boolean\n }\n\n // Because we are proactively updating the location\n // in memory before actually updating the browser history,\n // we need to track when we are doing this so we don't\n // notify subscribers twice on the last update.\n let tracking = true\n\n // We need to track the current scheduled update to prevent\n // multiple updates from being scheduled at the same time.\n let scheduled: Promise<void> | undefined\n\n // This function is a wrapper to prevent any of the callback's\n // side effects from causing a subscriber notification\n const untrack = (fn: () => void) => {\n tracking = false\n fn()\n tracking = true\n }\n\n // This function flushes the next update to the browser history\n const flush = () => {\n // Do not notify subscribers about this push/replace call\n untrack(() => {\n if (!next) return\n win.history[next.isPush ? 'pushState' : 'replaceState'](\n next.state,\n '',\n next.href,\n )\n // Reset the nextIsPush flag and clear the scheduled update\n next = undefined\n scheduled = undefined\n rollbackLocation = undefined\n })\n }\n\n // This function queues up a call to update the browser history\n const queueHistoryAction = (\n type: 'push' | 'replace',\n destHref: string,\n state: any,\n onUpdate: () => void,\n ) => {\n const href = createHref(destHref)\n\n if (!scheduled) {\n rollbackLocation = currentLocation\n }\n\n // Update the location in memory\n currentLocation = parseHref(destHref, state)\n\n // Keep track of the next location we need to flush to the URL\n next = {\n href,\n state,\n isPush: next?.isPush || type === 'push',\n }\n\n // Notify subscribers\n onUpdate()\n\n if (!scheduled) {\n // Schedule an update to the browser history\n scheduled = Promise.resolve().then(() => flush())\n }\n }\n\n const onPushPop = () => {\n currentLocation = parseLocation()\n history.notify()\n }\n\n var originalPushState = win.history.pushState\n var originalReplaceState = win.history.replaceState\n\n const history = createHistory({\n getLocation,\n pushState: (href, state, onUpdate) =>\n queueHistoryAction('push', href, state, onUpdate),\n replaceState: (href, state, onUpdate) =>\n queueHistoryAction('replace', href, state, onUpdate),\n back: () => win.history.back(),\n forward: () => win.history.forward(),\n go: (n) => win.history.go(n),\n createHref: (href) => createHref(href),\n flush,\n destroy: () => {\n win.history.pushState = originalPushState\n win.history.replaceState = originalReplaceState\n win.removeEventListener(pushStateEvent, onPushPop)\n win.removeEventListener(popStateEvent, onPushPop)\n },\n onBlocked: (onUpdate) => {\n // If a navigation is blocked, we need to rollback the location\n // that we optimistically updated in memory.\n if (rollbackLocation && currentLocation !== rollbackLocation) {\n currentLocation = rollbackLocation\n // Notify subscribers\n onUpdate()\n }\n },\n })\n\n win.addEventListener(pushStateEvent, onPushPop)\n win.addEventListener(popStateEvent, onPushPop)\n\n win.history.pushState = function () {\n let res = originalPushState.apply(win.history, arguments as any)\n if (tracking) history.notify()\n return res\n }\n\n win.history.replaceState = function () {\n let res = originalReplaceState.apply(win.history, arguments as any)\n if (tracking) history.notify()\n return res\n }\n\n return history\n}\n\nexport function createHashHistory(opts?: { window?: any }): RouterHistory {\n const win =\n opts?.window ??\n (typeof document !== 'undefined' ? window : (undefined as any))\n return createBrowserHistory({\n window: win,\n parseLocation: () => {\n const hashHref = win.location.hash.split('#').slice(1).join('#') ?? '/'\n return parseHref(hashHref, win.history.state)\n },\n createHref: (href) =>\n `${win.location.pathname}${win.location.search}#${href}`,\n })\n}\n\nexport function createMemoryHistory(\n opts: {\n initialEntries: string[]\n initialIndex?: number\n } = {\n initialEntries: ['/'],\n },\n): RouterHistory {\n const entries = opts.initialEntries\n let index = opts.initialIndex ?? entries.length - 1\n let currentState = {\n key: createRandomKey(),\n } as HistoryState\n\n const getLocation = () => parseHref(entries[index]!, currentState)\n\n return createHistory({\n getLocation,\n pushState: (path, state) => {\n currentState = state\n entries.push(path)\n index++\n },\n replaceState: (path, state) => {\n currentState = state\n entries[index] = path\n },\n back: () => {\n index--\n },\n forward: () => {\n index = Math.min(index + 1, entries.length - 1)\n },\n go: (n) => {\n index = Math.min(Math.max(index + n, 0), entries.length - 1)\n },\n createHref: (path) => path,\n })\n}\n\nfunction parseHref(href: string, state: HistoryState): HistoryLocation {\n let hashIndex = href.indexOf('#')\n let searchIndex = href.indexOf('?')\n\n return {\n href,\n pathname: href.substring(\n 0,\n hashIndex > 0\n ? searchIndex > 0\n ? Math.min(hashIndex, searchIndex)\n : hashIndex\n : searchIndex > 0\n ? searchIndex\n : href.length,\n ),\n hash: hashIndex > -1 ? href.substring(hashIndex) : '',\n search:\n searchIndex > -1\n ? href.slice(searchIndex, hashIndex === -1 ? undefined : hashIndex)\n : '',\n state: state || {},\n }\n}\n\n// Thanks co-pilot!\nfunction createRandomKey() {\n return (Math.random() + 1).toString(36).substring(7)\n}\n"],"names":["pushStateEvent","popStateEvent","beforeUnloadEvent","beforeUnloadListener","event","preventDefault","returnValue","stopBlocking","removeEventListener","capture","createHistory","opts","location","getLocation","subscribers","Set","blockers","onUpdate","forEach","subscriber","tryNavigation","task","document","length","blocker","allowed","onBlocked","subscribe","cb","add","delete","push","path","state","assignKey","pushState","replace","replaceState","go","index","back","forward","createHref","str","block","addEventListener","filter","b","flush","destroy","notify","key","createRandomKey","createBrowserHistory","win","window","undefined","parseLocation","parseHref","pathname","search","hash","history","currentLocation","rollbackLocation","next","tracking","scheduled","untrack","fn","isPush","href","queueHistoryAction","type","destHref","Promise","resolve","then","onPushPop","originalPushState","originalReplaceState","n","res","apply","arguments","createHashHistory","hashHref","split","slice","join","createMemoryHistory","initialEntries","entries","initialIndex","currentState","Math","min","max","hashIndex","indexOf","searchIndex","substring","random","toString"],"mappings":";;;;;;;;;;;;;;;;EAAA;EACA;EACA;;EAsCA,MAAMA,cAAc,GAAG,WAAW,CAAA;EAClC,MAAMC,aAAa,GAAG,UAAU,CAAA;EAChC,MAAMC,iBAAiB,GAAG,cAAc,CAAA;EAExC,MAAMC,oBAAoB,GAAIC,KAAY,IAAK;IAC7CA,KAAK,CAACC,cAAc,EAAE,CAAA;EACtB;EACA,EAAA,OAAQD,KAAK,CAACE,WAAW,GAAG,EAAE,CAAA;EAChC,CAAC,CAAA;EAED,MAAMC,YAAY,GAAGA,MAAM;EACzBC,EAAAA,mBAAmB,CAACN,iBAAiB,EAAEC,oBAAoB,EAAE;EAC3DM,IAAAA,OAAO,EAAE,IAAA;EACX,GAAC,CAAC,CAAA;EACJ,CAAC,CAAA;EAEM,SAASC,aAAaA,CAACC,IAW7B,EAAiB;EAChB,EAAA,IAAIC,QAAQ,GAAGD,IAAI,CAACE,WAAW,EAAE,CAAA;EACjC,EAAA,IAAIC,WAAW,GAAG,IAAIC,GAAG,EAAc,CAAA;IACvC,IAAIC,QAAqB,GAAG,EAAE,CAAA;IAE9B,MAAMC,QAAQ,GAAGA,MAAM;EACrBL,IAAAA,QAAQ,GAAGD,IAAI,CAACE,WAAW,EAAE,CAAA;MAC7BC,WAAW,CAACI,OAAO,CAAEC,UAAU,IAAKA,UAAU,EAAE,CAAC,CAAA;KAClD,CAAA;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A,KAAK,IAAI,EAAC;KAClB,CAAA;EACH,CAAA;;EAEA;EACA,SAASmB,eAAeA,GAAG;EACzB,EAAA,OAAO,CAAC2C,IAAI,CAACO,MAAM,EAAE,GAAG,CAAC,EAAEC,QAAQ,CAAC,EAAE,CAAC,CAACF,SAAS,CAAC,CAAC,CAAC,CAAA;EACtD;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.development.js","sources":["../../src/index.ts"],"sourcesContent":["// While the public API was clearly inspired by the \"history\" npm package,\n// This implementation attempts to be more lightweight by\n// making assumptions about the way TanStack Router works\n\nexport interface RouterHistory {\n location: HistoryLocation\n subscribe: (cb: () => void) => () => void\n push: (path: string, state?: any) => void\n replace: (path: string, state?: any) => void\n go: (index: number) => void\n back: () => void\n forward: () => void\n createHref: (href: string) => string\n block: (blocker: BlockerFn) => () => void\n flush: () => void\n destroy: () => void\n notify: () => void\n}\n\nexport interface HistoryLocation extends ParsedPath {\n state: HistoryState\n}\n\nexport interface ParsedPath {\n href: string\n pathname: string\n search: string\n hash: string\n}\n\nexport interface HistoryState {\n key: string\n}\n\ntype ShouldAllowNavigation = any\n\nexport type BlockerFn = () =>\n | Promise<ShouldAllowNavigation>\n | ShouldAllowNavigation\n\nconst pushStateEvent = 'pushstate'\nconst popStateEvent = 'popstate'\nconst beforeUnloadEvent = 'beforeunload'\n\nconst beforeUnloadListener = (event: Event) => {\n event.preventDefault()\n // @ts-ignore\n return (event.returnValue = '')\n}\n\nconst stopBlocking = () => {\n removeEventListener(beforeUnloadEvent, beforeUnloadListener, {\n capture: true,\n })\n}\n\nexport function createHistory(opts: {\n getLocation: () => HistoryLocation\n pushState: (path: string, state: any) => void\n replaceState: (path: string, state: any) => void\n go: (n: number) => void\n back: () => void\n forward: () => void\n createHref: (path: string) => string\n flush?: () => void\n destroy?: () => void\n onBlocked?: (onUpdate: () => void) => void\n}): RouterHistory {\n let location = opts.getLocation()\n let subscribers = new Set<() => void>()\n let blockers: BlockerFn[] = []\n\n const onUpdate = () => {\n location = opts.getLocation()\n subscribers.forEach((subscriber) => subscriber())\n }\n\n const tryNavigation = async (task: () => void) => {\n if (typeof document !== 'undefined' && blockers.length) {\n for (let blocker of blockers) {\n const allowed = await blocker()\n if (!allowed) {\n opts.onBlocked?.(onUpdate)\n return\n }\n }\n }\n\n task()\n }\n\n return {\n get location() {\n return location\n },\n subscribe: (cb: () => void) => {\n subscribers.add(cb)\n\n return () => {\n subscribers.delete(cb)\n }\n },\n push: (path: string, state: any) => {\n state = assignKey(state)\n tryNavigation(() => {\n opts.pushState(path, state)\n onUpdate()\n })\n },\n replace: (path: string, state: any) => {\n state = assignKey(state)\n tryNavigation(() => {\n opts.replaceState(path, state)\n onUpdate()\n })\n },\n go: (index) => {\n tryNavigation(() => {\n opts.go(index)\n })\n },\n back: () => {\n tryNavigation(() => {\n opts.back()\n })\n },\n forward: () => {\n tryNavigation(() => {\n opts.forward()\n })\n },\n createHref: (str) => opts.createHref(str),\n block: (blocker) => {\n blockers.push(blocker)\n\n if (blockers.length === 1) {\n addEventListener(beforeUnloadEvent, beforeUnloadListener, {\n capture: true,\n })\n }\n\n return () => {\n blockers = blockers.filter((b) => b !== blocker)\n\n if (!blockers.length) {\n stopBlocking()\n }\n }\n },\n flush: () => opts.flush?.(),\n destroy: () => opts.destroy?.(),\n notify: onUpdate,\n }\n}\n\nfunction assignKey(state: HistoryState) {\n if (!state) {\n state = {} as HistoryState\n }\n return {\n ...state,\n key: createRandomKey(),\n }\n}\n\n/**\n * Creates a history object that can be used to interact with the browser's\n * navigation. This is a lightweight API wrapping the browser's native methods.\n * It is designed to work with TanStack Router, but could be used as a standalone API as well.\n * IMPORTANT: This API implements history throttling via a microtask to prevent\n * excessive calls to the history API. In some browsers, calling history.pushState or\n * history.replaceState in quick succession can cause the browser to ignore subsequent\n * calls. This API smooths out those differences and ensures that your application\n * state will *eventually* match the browser state. In most cases, this is not a problem,\n * but if you need to ensure that the browser state is up to date, you can use the\n * `history.flush` method to immediately flush all pending state changes to the browser URL.\n * @param opts\n * @param opts.getHref A function that returns the current href (path + search + hash)\n * @param opts.createHref A function that takes a path and returns a href (path + search + hash)\n * @returns A history instance\n */\nexport function createBrowserHistory(opts?: {\n parseLocation?: () => HistoryLocation\n createHref?: (path: string) => string\n window?: any\n}): RouterHistory {\n const win =\n opts?.window ??\n (typeof document !== 'undefined' ? window : (undefined as any))\n\n const createHref = opts?.createHref ?? ((path) => path)\n const parseLocation =\n opts?.parseLocation ??\n (() =>\n parseHref(\n `${win.location.pathname}${win.location.search}${win.location.hash}`,\n win.history.state,\n ))\n\n let currentLocation = parseLocation()\n let rollbackLocation: HistoryLocation | undefined\n\n const getLocation = () => currentLocation\n\n let next:\n | undefined\n | {\n // This is the latest location that we were attempting to push/replace\n href: string\n // This is the latest state that we were attempting to push/replace\n state: any\n // This is the latest type that we were attempting to push/replace\n isPush: boolean\n }\n\n // Because we are proactively updating the location\n // in memory before actually updating the browser history,\n // we need to track when we are doing this so we don't\n // notify subscribers twice on the last update.\n let tracking = true\n\n // We need to track the current scheduled update to prevent\n // multiple updates from being scheduled at the same time.\n let scheduled: Promise<void> | undefined\n\n // This function is a wrapper to prevent any of the callback's\n // side effects from causing a subscriber notification\n const untrack = (fn: () => void) => {\n tracking = false\n fn()\n tracking = true\n }\n\n // This function flushes the next update to the browser history\n const flush = () => {\n // Do not notify subscribers about this push/replace call\n untrack(() => {\n if (!next) return\n win.history[next.isPush ? 'pushState' : 'replaceState'](\n next.state,\n '',\n next.href,\n )\n // Reset the nextIsPush flag and clear the scheduled update\n next = undefined\n scheduled = undefined\n rollbackLocation = undefined\n })\n }\n\n // This function queues up a call to update the browser history\n const queueHistoryAction = (\n type: 'push' | 'replace',\n destHref: string,\n state: any,\n ) => {\n const href = createHref(destHref)\n\n if (!scheduled) {\n rollbackLocation = currentLocation\n }\n\n // Update the location in memory\n currentLocation = parseHref(destHref, state)\n\n // Keep track of the next location we need to flush to the URL\n next = {\n href,\n state,\n isPush: next?.isPush || type === 'push',\n }\n\n if (!scheduled) {\n // Schedule an update to the browser history\n scheduled = Promise.resolve().then(() => flush())\n }\n }\n\n const onPushPop = () => {\n currentLocation = parseLocation()\n history.notify()\n }\n\n var originalPushState = win.history.pushState\n var originalReplaceState = win.history.replaceState\n\n const history = createHistory({\n getLocation,\n pushState: (href, state) => queueHistoryAction('push', href, state),\n replaceState: (href, state) => queueHistoryAction('replace', href, state),\n back: () => win.history.back(),\n forward: () => win.history.forward(),\n go: (n) => win.history.go(n),\n createHref: (href) => createHref(href),\n flush,\n destroy: () => {\n win.history.pushState = originalPushState\n win.history.replaceState = originalReplaceState\n win.removeEventListener(pushStateEvent, onPushPop)\n win.removeEventListener(popStateEvent, onPushPop)\n },\n onBlocked: (onUpdate) => {\n // If a navigation is blocked, we need to rollback the location\n // that we optimistically updated in memory.\n if (rollbackLocation && currentLocation !== rollbackLocation) {\n currentLocation = rollbackLocation\n // Notify subscribers\n onUpdate()\n }\n },\n })\n\n win.addEventListener(pushStateEvent, onPushPop)\n win.addEventListener(popStateEvent, onPushPop)\n\n win.history.pushState = function () {\n let res = originalPushState.apply(win.history, arguments as any)\n if (tracking) history.notify()\n return res\n }\n\n win.history.replaceState = function () {\n let res = originalReplaceState.apply(win.history, arguments as any)\n if (tracking) history.notify()\n return res\n }\n\n return history\n}\n\nexport function createHashHistory(opts?: { window?: any }): RouterHistory {\n const win =\n opts?.window ??\n (typeof document !== 'undefined' ? window : (undefined as any))\n return createBrowserHistory({\n window: win,\n parseLocation: () => {\n const hashHref = win.location.hash.split('#').slice(1).join('#') ?? '/'\n return parseHref(hashHref, win.history.state)\n },\n createHref: (href) =>\n `${win.location.pathname}${win.location.search}#${href}`,\n })\n}\n\nexport function createMemoryHistory(\n opts: {\n initialEntries: string[]\n initialIndex?: number\n } = {\n initialEntries: ['/'],\n },\n): RouterHistory {\n const entries = opts.initialEntries\n let index = opts.initialIndex ?? entries.length - 1\n let currentState = {\n key: createRandomKey(),\n } as HistoryState\n\n const getLocation = () => parseHref(entries[index]!, currentState)\n\n return createHistory({\n getLocation,\n\n pushState: (path, state) => {\n currentState = state\n entries.push(path)\n index++\n },\n replaceState: (path, state) => {\n currentState = state\n entries[index] = path\n },\n back: () => {\n index--\n },\n forward: () => {\n index = Math.min(index + 1, entries.length - 1)\n },\n go: (n) => {\n index = Math.min(Math.max(index + n, 0), entries.length - 1)\n },\n createHref: (path) => path,\n })\n}\n\nfunction parseHref(href: string, state: HistoryState): HistoryLocation {\n let hashIndex = href.indexOf('#')\n let searchIndex = href.indexOf('?')\n\n return {\n href,\n pathname: href.substring(\n 0,\n hashIndex > 0\n ? searchIndex > 0\n ? Math.min(hashIndex, searchIndex)\n : hashIndex\n : searchIndex > 0\n ? searchIndex\n : href.length,\n ),\n hash: hashIndex > -1 ? href.substring(hashIndex) : '',\n search:\n searchIndex > -1\n ? href.slice(searchIndex, hashIndex === -1 ? undefined : hashIndex)\n : '',\n state: state || {},\n }\n}\n\n// Thanks co-pilot!\nfunction createRandomKey() {\n return (Math.random() + 1).toString(36).substring(7)\n}\n"],"names":["pushStateEvent","popStateEvent","beforeUnloadEvent","beforeUnloadListener","event","preventDefault","returnValue","stopBlocking","removeEventListener","capture","createHistory","opts","location","getLocation","subscribers","Set","blockers","onUpdate","forEach","subscriber","tryNavigation","task","document","length","blocker","allowed","onBlocked","subscribe","cb","add","delete","push","path","state","assignKey","pushState","replace","replaceState","go","index","back","forward","createHref","str","block","addEventListener","filter","b","flush","destroy","notify","key","createRandomKey","createBrowserHistory","win","window","undefined","parseLocation","parseHref","pathname","search","hash","history","currentLocation","rollbackLocation","next","tracking","scheduled","untrack","fn","isPush","href","queueHistoryAction","type","destHref","Promise","resolve","then","onPushPop","originalPushState","originalReplaceState","n","res","apply","arguments","createHashHistory","hashHref","split","slice","join","createMemoryHistory","initialEntries","entries","initialIndex","currentState","Math","min","max","hashIndex","indexOf","searchIndex","substring","random","toString"],"mappings":";;;;;;;;;;;;;;;;EAAA;EACA;EACA;;EAsCA,MAAMA,cAAc,GAAG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E,EAAA,IAAIiE,SAAS,GAAG3B,IAAI,CAAC4B,OAAO,CAAC,GAAG,CAAC,CAAA;EACjC,EAAA,IAAIC,WAAW,GAAG7B,IAAI,CAAC4B,OAAO,CAAC,GAAG,CAAC,CAAA;IAEnC,OAAO;MACL5B,IAAI;EACJZ,IAAAA,QAAQ,EAAEY,IAAI,CAAC8B,SAAS,CACtB,CAAC,EACDH,SAAS,GAAG,CAAC,GACTE,WAAW,GAAG,CAAC,GACbL,IAAI,CAACC,GAAG,CAACE,SAAS,EAAEE,WAAW,CAAC,GAChCF,SAAS,GACXE,WAAW,GAAG,CAAC,GACbA,WAAW,GACX7B,IAAI,CAAChD,MACb,CAAC;EACDsC,IAAAA,IAAI,EAAEqC,SAAS,GAAG,CAAC,CAAC,GAAG3B,IAAI,CAAC8B,SAAS,CAACH,SAAS,CAAC,GAAG,EAAE;MACrDtC,MAAM,EACJwC,WAAW,GAAG,CAAC,CAAC,GACZ7B,IAAI,CAACiB,KAAK,CAACY,WAAW,EAAEF,SAAS,KAAK,CAAC,CAAC,GAAG1C,SAAS,GAAG0C,SAAS,CAAC,GACjE,EAAE;MACRjE,KAAK,EAAEA,KAAK,IAAI,EAAC;KAClB,CAAA;EACH,CAAA;;EAEA;EACA,SAASmB,eAAeA,GAAG;EACzB,EAAA,OAAO,CAAC2C,IAAI,CAACO,MAAM,EAAE,GAAG,CAAC,EAAEC,QAAQ,CAAC,EAAE,CAAC,CAACF,SAAS,CAAC,CAAC,CAAC,CAAA;EACtD;;;;;;;;;;;"}
@@ -8,5 +8,5 @@
8
8
  *
9
9
  * @license MIT
10
10
  */
11
- !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).TanStackHistory={})}(this,(function(t){"use strict";const e="pushstate",o="popstate",n="beforeunload",r=t=>(t.preventDefault(),t.returnValue=""),i=()=>{removeEventListener(n,r,{capture:!0})};function a(t){let e=t.getLocation(),o=new Set,a=[];const c=()=>{e=t.getLocation(),o.forEach((t=>t()))},h=async e=>{if("undefined"!=typeof document&&a.length)for(let e of a){if(!await e())return void t.onBlocked?.(c)}e()};return{get location(){return e},subscribe:t=>(o.add(t),()=>{o.delete(t)}),push:(e,o)=>{o=s(o),h((()=>{t.pushState(e,o,c)}))},replace:(e,o)=>{o=s(o),h((()=>{t.replaceState(e,o,c)}))},go:e=>{h((()=>{t.go(e)}))},back:()=>{h((()=>{t.back()}))},forward:()=>{h((()=>{t.forward()}))},createHref:e=>t.createHref(e),block:t=>(a.push(t),1===a.length&&addEventListener(n,r,{capture:!0}),()=>{a=a.filter((e=>e!==t)),a.length||i()}),flush:()=>t.flush?.(),destroy:()=>t.destroy?.(),notify:c}}function s(t){return t||(t={}),{...t,key:u()}}function c(t){const n=t?.window??("undefined"!=typeof document?window:void 0),r=t?.createHref??(t=>t),i=t?.parseLocation??(()=>h(`${n.location.pathname}${n.location.search}${n.location.hash}`,n.history.state));let s,c=i();let u,f,l=!0;const d=()=>{l=!1,(()=>{u&&(n.history[u.isPush?"pushState":"replaceState"](u.state,"",u.href),u=void 0,f=void 0,s=void 0)})(),l=!0},p=(t,e,o,n)=>{const i=r(e);f||(s=c),c=h(e,o),u={href:i,state:o,isPush:u?.isPush||"push"===t},n(),f||(f=Promise.resolve().then((()=>d())))},y=()=>{c=i(),m.notify()};var g=n.history.pushState,v=n.history.replaceState;const m=a({getLocation:()=>c,pushState:(t,e,o)=>p("push",t,e,o),replaceState:(t,e,o)=>p("replace",t,e,o),back:()=>n.history.back(),forward:()=>n.history.forward(),go:t=>n.history.go(t),createHref:t=>r(t),flush:d,destroy:()=>{n.history.pushState=g,n.history.replaceState=v,n.removeEventListener(e,y),n.removeEventListener(o,y)},onBlocked:t=>{s&&c!==s&&(c=s,t())}});return n.addEventListener(e,y),n.addEventListener(o,y),n.history.pushState=function(){let t=g.apply(n.history,arguments);return l&&m.notify(),t},n.history.replaceState=function(){let t=v.apply(n.history,arguments);return l&&m.notify(),t},m}function h(t,e){let o=t.indexOf("#"),n=t.indexOf("?");return{href:t,pathname:t.substring(0,o>0?n>0?Math.min(o,n):o:n>0?n:t.length),hash:o>-1?t.substring(o):"",search:n>-1?t.slice(n,-1===o?void 0:o):"",state:e||{}}}function u(){return(Math.random()+1).toString(36).substring(7)}t.createBrowserHistory=c,t.createHashHistory=function(t){const e=t?.window??("undefined"!=typeof document?window:void 0);return c({window:e,parseLocation:()=>h(e.location.hash.split("#").slice(1).join("#")??"/",e.history.state),createHref:t=>`${e.location.pathname}${e.location.search}#${t}`})},t.createHistory=a,t.createMemoryHistory=function(t={initialEntries:["/"]}){const e=t.initialEntries;let o=t.initialIndex??e.length-1,n={key:u()};return a({getLocation:()=>h(e[o],n),pushState:(t,r)=>{n=r,e.push(t),o++},replaceState:(t,r)=>{n=r,e[o]=t},back:()=>{o--},forward:()=>{o=Math.min(o+1,e.length-1)},go:t=>{o=Math.min(Math.max(o+t,0),e.length-1)},createHref:t=>t})}}));
11
+ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).TanStackHistory={})}(this,(function(t){"use strict";const e="pushstate",o="popstate",n="beforeunload",r=t=>(t.preventDefault(),t.returnValue=""),i=()=>{removeEventListener(n,r,{capture:!0})};function a(t){let e=t.getLocation(),o=new Set,a=[];const c=()=>{e=t.getLocation(),o.forEach((t=>t()))},h=async e=>{if("undefined"!=typeof document&&a.length)for(let e of a){if(!await e())return void t.onBlocked?.(c)}e()};return{get location(){return e},subscribe:t=>(o.add(t),()=>{o.delete(t)}),push:(e,o)=>{o=s(o),h((()=>{t.pushState(e,o),c()}))},replace:(e,o)=>{o=s(o),h((()=>{t.replaceState(e,o),c()}))},go:e=>{h((()=>{t.go(e)}))},back:()=>{h((()=>{t.back()}))},forward:()=>{h((()=>{t.forward()}))},createHref:e=>t.createHref(e),block:t=>(a.push(t),1===a.length&&addEventListener(n,r,{capture:!0}),()=>{a=a.filter((e=>e!==t)),a.length||i()}),flush:()=>t.flush?.(),destroy:()=>t.destroy?.(),notify:c}}function s(t){return t||(t={}),{...t,key:u()}}function c(t){const n=t?.window??("undefined"!=typeof document?window:void 0),r=t?.createHref??(t=>t),i=t?.parseLocation??(()=>h(`${n.location.pathname}${n.location.search}${n.location.hash}`,n.history.state));let s,c=i();let u,f,l=!0;const d=()=>{l=!1,(()=>{u&&(n.history[u.isPush?"pushState":"replaceState"](u.state,"",u.href),u=void 0,f=void 0,s=void 0)})(),l=!0},p=(t,e,o)=>{const n=r(e);f||(s=c),c=h(e,o),u={href:n,state:o,isPush:u?.isPush||"push"===t},f||(f=Promise.resolve().then((()=>d())))},y=()=>{c=i(),m.notify()};var g=n.history.pushState,v=n.history.replaceState;const m=a({getLocation:()=>c,pushState:(t,e)=>p("push",t,e),replaceState:(t,e)=>p("replace",t,e),back:()=>n.history.back(),forward:()=>n.history.forward(),go:t=>n.history.go(t),createHref:t=>r(t),flush:d,destroy:()=>{n.history.pushState=g,n.history.replaceState=v,n.removeEventListener(e,y),n.removeEventListener(o,y)},onBlocked:t=>{s&&c!==s&&(c=s,t())}});return n.addEventListener(e,y),n.addEventListener(o,y),n.history.pushState=function(){let t=g.apply(n.history,arguments);return l&&m.notify(),t},n.history.replaceState=function(){let t=v.apply(n.history,arguments);return l&&m.notify(),t},m}function h(t,e){let o=t.indexOf("#"),n=t.indexOf("?");return{href:t,pathname:t.substring(0,o>0?n>0?Math.min(o,n):o:n>0?n:t.length),hash:o>-1?t.substring(o):"",search:n>-1?t.slice(n,-1===o?void 0:o):"",state:e||{}}}function u(){return(Math.random()+1).toString(36).substring(7)}t.createBrowserHistory=c,t.createHashHistory=function(t){const e=t?.window??("undefined"!=typeof document?window:void 0);return c({window:e,parseLocation:()=>h(e.location.hash.split("#").slice(1).join("#")??"/",e.history.state),createHref:t=>`${e.location.pathname}${e.location.search}#${t}`})},t.createHistory=a,t.createMemoryHistory=function(t={initialEntries:["/"]}){const e=t.initialEntries;let o=t.initialIndex??e.length-1,n={key:u()};return a({getLocation:()=>h(e[o],n),pushState:(t,r)=>{n=r,e.push(t),o++},replaceState:(t,r)=>{n=r,e[o]=t},back:()=>{o--},forward:()=>{o=Math.min(o+1,e.length-1)},go:t=>{o=Math.min(Math.max(o+t,0),e.length-1)},createHref:t=>t})}}));
12
12
  //# sourceMappingURL=index.production.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.production.js","sources":["../../src/index.ts"],"sourcesContent":["// While the public API was clearly inspired by the \"history\" npm package,\n// This implementation attempts to be more lightweight by\n// making assumptions about the way TanStack Router works\n\nexport interface RouterHistory {\n location: HistoryLocation\n subscribe: (cb: () => void) => () => void\n push: (path: string, state?: any) => void\n replace: (path: string, state?: any) => void\n go: (index: number) => void\n back: () => void\n forward: () => void\n createHref: (href: string) => string\n block: (blocker: BlockerFn) => () => void\n flush: () => void\n destroy: () => void\n notify: () => void\n}\n\nexport interface HistoryLocation extends ParsedPath {\n state: HistoryState\n}\n\nexport interface ParsedPath {\n href: string\n pathname: string\n search: string\n hash: string\n}\n\nexport interface HistoryState {\n key: string\n}\n\ntype ShouldAllowNavigation = any\n\nexport type BlockerFn = () =>\n | Promise<ShouldAllowNavigation>\n | ShouldAllowNavigation\n\nconst pushStateEvent = 'pushstate'\nconst popStateEvent = 'popstate'\nconst beforeUnloadEvent = 'beforeunload'\n\nconst beforeUnloadListener = (event: Event) => {\n event.preventDefault()\n // @ts-ignore\n return (event.returnValue = '')\n}\n\nconst stopBlocking = () => {\n removeEventListener(beforeUnloadEvent, beforeUnloadListener, {\n capture: true,\n })\n}\n\nexport function createHistory(opts: {\n getLocation: () => HistoryLocation\n pushState: (path: string, state: any, onUpdate: () => void) => void\n replaceState: (path: string, state: any, onUpdate: () => void) => void\n go: (n: number) => void\n back: () => void\n forward: () => void\n createHref: (path: string) => string\n flush?: () => void\n destroy?: () => void\n onBlocked?: (onUpdate: () => void) => void\n}): RouterHistory {\n let location = opts.getLocation()\n let subscribers = new Set<() => void>()\n let blockers: BlockerFn[] = []\n\n const onUpdate = () => {\n location = opts.getLocation()\n subscribers.forEach((subscriber) => subscriber())\n }\n\n const tryNavigation = async (task: () => void) => {\n if (typeof document !== 'undefined' && blockers.length) {\n for (let blocker of blockers) {\n const allowed = await blocker()\n if (!allowed) {\n opts.onBlocked?.(onUpdate)\n return\n }\n }\n }\n\n task()\n }\n\n return {\n get location() {\n return location\n },\n subscribe: (cb: () => void) => {\n subscribers.add(cb)\n\n return () => {\n subscribers.delete(cb)\n }\n },\n push: (path: string, state: any) => {\n state = assignKey(state)\n tryNavigation(() => {\n opts.pushState(path, state, onUpdate)\n })\n },\n replace: (path: string, state: any) => {\n state = assignKey(state)\n tryNavigation(() => {\n opts.replaceState(path, state, onUpdate)\n })\n },\n go: (index) => {\n tryNavigation(() => {\n opts.go(index)\n })\n },\n back: () => {\n tryNavigation(() => {\n opts.back()\n })\n },\n forward: () => {\n tryNavigation(() => {\n opts.forward()\n })\n },\n createHref: (str) => opts.createHref(str),\n block: (blocker) => {\n blockers.push(blocker)\n\n if (blockers.length === 1) {\n addEventListener(beforeUnloadEvent, beforeUnloadListener, {\n capture: true,\n })\n }\n\n return () => {\n blockers = blockers.filter((b) => b !== blocker)\n\n if (!blockers.length) {\n stopBlocking()\n }\n }\n },\n flush: () => opts.flush?.(),\n destroy: () => opts.destroy?.(),\n notify: onUpdate,\n }\n}\n\nfunction assignKey(state: HistoryState) {\n if (!state) {\n state = {} as HistoryState\n }\n return {\n ...state,\n key: createRandomKey(),\n }\n}\n\n/**\n * Creates a history object that can be used to interact with the browser's\n * navigation. This is a lightweight API wrapping the browser's native methods.\n * It is designed to work with TanStack Router, but could be used as a standalone API as well.\n * IMPORTANT: This API implements history throttling via a microtask to prevent\n * excessive calls to the history API. In some browsers, calling history.pushState or\n * history.replaceState in quick succession can cause the browser to ignore subsequent\n * calls. This API smooths out those differences and ensures that your application\n * state will *eventually* match the browser state. In most cases, this is not a problem,\n * but if you need to ensure that the browser state is up to date, you can use the\n * `history.flush` method to immediately flush all pending state changes to the browser URL.\n * @param opts\n * @param opts.getHref A function that returns the current href (path + search + hash)\n * @param opts.createHref A function that takes a path and returns a href (path + search + hash)\n * @returns A history instance\n */\nexport function createBrowserHistory(opts?: {\n parseLocation?: () => HistoryLocation\n createHref?: (path: string) => string\n window?: any\n}): RouterHistory {\n const win =\n opts?.window ??\n (typeof document !== 'undefined' ? window : (undefined as any))\n\n const createHref = opts?.createHref ?? ((path) => path)\n const parseLocation =\n opts?.parseLocation ??\n (() =>\n parseHref(\n `${win.location.pathname}${win.location.search}${win.location.hash}`,\n win.history.state,\n ))\n\n let currentLocation = parseLocation()\n let rollbackLocation: HistoryLocation | undefined\n\n const getLocation = () => currentLocation\n\n let next:\n | undefined\n | {\n // This is the latest location that we were attempting to push/replace\n href: string\n // This is the latest state that we were attempting to push/replace\n state: any\n // This is the latest type that we were attempting to push/replace\n isPush: boolean\n }\n\n // Because we are proactively updating the location\n // in memory before actually updating the browser history,\n // we need to track when we are doing this so we don't\n // notify subscribers twice on the last update.\n let tracking = true\n\n // We need to track the current scheduled update to prevent\n // multiple updates from being scheduled at the same time.\n let scheduled: Promise<void> | undefined\n\n // This function is a wrapper to prevent any of the callback's\n // side effects from causing a subscriber notification\n const untrack = (fn: () => void) => {\n tracking = false\n fn()\n tracking = true\n }\n\n // This function flushes the next update to the browser history\n const flush = () => {\n // Do not notify subscribers about this push/replace call\n untrack(() => {\n if (!next) return\n win.history[next.isPush ? 'pushState' : 'replaceState'](\n next.state,\n '',\n next.href,\n )\n // Reset the nextIsPush flag and clear the scheduled update\n next = undefined\n scheduled = undefined\n rollbackLocation = undefined\n })\n }\n\n // This function queues up a call to update the browser history\n const queueHistoryAction = (\n type: 'push' | 'replace',\n destHref: string,\n state: any,\n onUpdate: () => void,\n ) => {\n const href = createHref(destHref)\n\n if (!scheduled) {\n rollbackLocation = currentLocation\n }\n\n // Update the location in memory\n currentLocation = parseHref(destHref, state)\n\n // Keep track of the next location we need to flush to the URL\n next = {\n href,\n state,\n isPush: next?.isPush || type === 'push',\n }\n\n // Notify subscribers\n onUpdate()\n\n if (!scheduled) {\n // Schedule an update to the browser history\n scheduled = Promise.resolve().then(() => flush())\n }\n }\n\n const onPushPop = () => {\n currentLocation = parseLocation()\n history.notify()\n }\n\n var originalPushState = win.history.pushState\n var originalReplaceState = win.history.replaceState\n\n const history = createHistory({\n getLocation,\n pushState: (href, state, onUpdate) =>\n queueHistoryAction('push', href, state, onUpdate),\n replaceState: (href, state, onUpdate) =>\n queueHistoryAction('replace', href, state, onUpdate),\n back: () => win.history.back(),\n forward: () => win.history.forward(),\n go: (n) => win.history.go(n),\n createHref: (href) => createHref(href),\n flush,\n destroy: () => {\n win.history.pushState = originalPushState\n win.history.replaceState = originalReplaceState\n win.removeEventListener(pushStateEvent, onPushPop)\n win.removeEventListener(popStateEvent, onPushPop)\n },\n onBlocked: (onUpdate) => {\n // If a navigation is blocked, we need to rollback the location\n // that we optimistically updated in memory.\n if (rollbackLocation && currentLocation !== rollbackLocation) {\n currentLocation = rollbackLocation\n // Notify subscribers\n onUpdate()\n }\n },\n })\n\n win.addEventListener(pushStateEvent, onPushPop)\n win.addEventListener(popStateEvent, onPushPop)\n\n win.history.pushState = function () {\n let res = originalPushState.apply(win.history, arguments as any)\n if (tracking) history.notify()\n return res\n }\n\n win.history.replaceState = function () {\n let res = originalReplaceState.apply(win.history, arguments as any)\n if (tracking) history.notify()\n return res\n }\n\n return history\n}\n\nexport function createHashHistory(opts?: { window?: any }): RouterHistory {\n const win =\n opts?.window ??\n (typeof document !== 'undefined' ? window : (undefined as any))\n return createBrowserHistory({\n window: win,\n parseLocation: () => {\n const hashHref = win.location.hash.split('#').slice(1).join('#') ?? '/'\n return parseHref(hashHref, win.history.state)\n },\n createHref: (href) =>\n `${win.location.pathname}${win.location.search}#${href}`,\n })\n}\n\nexport function createMemoryHistory(\n opts: {\n initialEntries: string[]\n initialIndex?: number\n } = {\n initialEntries: ['/'],\n },\n): RouterHistory {\n const entries = opts.initialEntries\n let index = opts.initialIndex ?? entries.length - 1\n let currentState = {\n key: createRandomKey(),\n } as HistoryState\n\n const getLocation = () => parseHref(entries[index]!, currentState)\n\n return createHistory({\n getLocation,\n pushState: (path, state) => {\n currentState = state\n entries.push(path)\n index++\n },\n replaceState: (path, state) => {\n currentState = state\n entries[index] = path\n },\n back: () => {\n index--\n },\n forward: () => {\n index = Math.min(index + 1, entries.length - 1)\n },\n go: (n) => {\n index = Math.min(Math.max(index + n, 0), entries.length - 1)\n },\n createHref: (path) => path,\n })\n}\n\nfunction parseHref(href: string, state: HistoryState): HistoryLocation {\n let hashIndex = href.indexOf('#')\n let searchIndex = href.indexOf('?')\n\n return {\n href,\n pathname: href.substring(\n 0,\n hashIndex > 0\n ? searchIndex > 0\n ? Math.min(hashIndex, searchIndex)\n : hashIndex\n : searchIndex > 0\n ? searchIndex\n : href.length,\n ),\n hash: hashIndex > -1 ? href.substring(hashIndex) : '',\n search:\n searchIndex > -1\n ? href.slice(searchIndex, hashIndex === -1 ? undefined : hashIndex)\n : '',\n state: state || {},\n }\n}\n\n// Thanks co-pilot!\nfunction createRandomKey() {\n return (Math.random() + 1).toString(36).substring(7)\n}\n"],"names":["pushStateEvent","popStateEvent","beforeUnloadEvent","beforeUnloadListener","event","preventDefault","returnValue","stopBlocking","removeEventListener","capture","createHistory","opts","location","getLocation","subscribers","Set","blockers","onUpdate","forEach","subscriber","tryNavigation","async","document","length","blocker","onBlocked","task","subscribe","cb","add","delete","push","path","state","assignKey","pushState","replace","replaceState","go","index","back","forward","createHref","str","block","addEventListener","filter","b","flush","destroy","notify","key","createRandomKey","createBrowserHistory","win","window","undefined","parseLocation","parseHref","pathname","search","hash","history","rollbackLocation","currentLocation","next","scheduled","tracking","isPush","href","fn","queueHistoryAction","type","destHref","Promise","resolve","then","onPushPop","originalPushState","originalReplaceState","n","res","apply","arguments","hashIndex","indexOf","searchIndex","substring","Math","min","slice","random","toString","split","join","initialEntries","entries","initialIndex","currentState","max"],"mappings":";;;;;;;;;;uPAwCA,MAAMA,EAAiB,YACjBC,EAAgB,WAChBC,EAAoB,eAEpBC,EAAwBC,IAC5BA,EAAMC,iBAEED,EAAME,YAAc,IAGxBC,EAAeA,KACnBC,oBAAoBN,EAAmBC,EAAsB,CAC3DM,SAAS,GACT,EAGG,SAASC,EAAcC,GAY5B,IAAIC,EAAWD,EAAKE,cAChBC,EAAc,IAAIC,IAClBC,EAAwB,GAE5B,MAAMC,EAAWA,KACfL,EAAWD,EAAKE,cAChBC,EAAYI,SAASC,GAAeA,KAAa,EAG7CC,EAAgBC,UACpB,GAAwB,oBAAbC,UAA4BN,EAASO,OAC9C,IAAK,IAAIC,KAAWR,EAAU,CAE5B,UADsBQ,IAGpB,YADAb,EAAKc,YAAYR,EAGrB,CAGFS,GAAM,EAGR,MAAO,CACL,YAAId,GACF,OAAOA,CACR,EACDe,UAAYC,IACVd,EAAYe,IAAID,GAET,KACLd,EAAYgB,OAAOF,EAAG,GAG1BG,KAAMA,CAACC,EAAcC,KACnBA,EAAQC,EAAUD,GAClBb,GAAc,KACZT,EAAKwB,UAAUH,EAAMC,EAAOhB,EAAS,GACrC,EAEJmB,QAASA,CAACJ,EAAcC,KACtBA,EAAQC,EAAUD,GAClBb,GAAc,KACZT,EAAK0B,aAAaL,EAAMC,EAAOhB,EAAS,GACxC,EAEJqB,GAAKC,IACHnB,GAAc,KACZT,EAAK2B,GAAGC,EAAM,GACd,EAEJC,KAAMA,KACJpB,GAAc,KACZT,EAAK6B,MAAM,GACX,EAEJC,QAASA,KACPrB,GAAc,KACZT,EAAK8B,SAAS,GACd,EAEJC,WAAaC,GAAQhC,EAAK+B,WAAWC,GACrCC,MAAQpB,IACNR,EAASe,KAAKP,GAEU,IAApBR,EAASO,QACXsB,iBAAiB3C,EAAmBC,EAAsB,CACxDM,SAAS,IAIN,KACLO,EAAWA,EAAS8B,QAAQC,GAAMA,IAAMvB,IAEnC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MAAOA,GAAS,CAAC,EAErB,CAGA,SAASmB,IACP,OAAQoC,KAAKG,SAAW,GAAGC,SAAS,IAAIL,UAAU,EACpD,8CAnFO,SAA2B5E,GAChC,MAAM2C,EACJ3C,GAAM4C,SACe,oBAAbjC,SAA2BiC,YAAUC,GAC/C,OAAOH,EAAqB,CAC1BE,OAAQD,EACRG,cAAeA,IAENC,EADUJ,EAAI1C,SAASiD,KAAKgC,MAAM,KAAKH,MAAM,GAAGI,KAAK,MAAQ,IACzCxC,EAAIQ,QAAQ7B,OAEzCS,WAAa2B,GACV,GAAEf,EAAI1C,SAAS+C,WAAWL,EAAI1C,SAASgD,UAAUS,KAExD,0CAEO,SACL1D,EAGI,CACFoF,eAAgB,CAAC,OAGnB,MAAMC,EAAUrF,EAAKoF,eACrB,IAAIxD,EAAQ5B,EAAKsF,cAAgBD,EAAQzE,OAAS,EAC9C2E,EAAe,CACjB/C,IAAKC,KAKP,OAAO1C,EAAc,CACnBG,YAHkBA,IAAM6C,EAAUsC,EAAQzD,GAAS2D,GAInD/D,UAAWA,CAACH,EAAMC,KAChBiE,EAAejE,EACf+D,EAAQjE,KAAKC,GACbO,GAAO,EAETF,aAAcA,CAACL,EAAMC,KACnBiE,EAAejE,EACf+D,EAAQzD,GAASP,CAAI,EAEvBQ,KAAMA,KACJD,GAAO,EAETE,QAASA,KACPF,EAAQiD,KAAKC,IAAIlD,EAAQ,EAAGyD,EAAQzE,OAAS,EAAE,EAEjDe,GAAK0C,IACHzC,EAAQiD,KAAKC,IAAID,KAAKW,IAAI5D,EAAQyC,EAAG,GAAIgB,EAAQzE,OAAS,EAAE,EAE9DmB,WAAaV,GAASA,GAE1B"}
1
+ {"version":3,"file":"index.production.js","sources":["../../src/index.ts"],"sourcesContent":["// While the public API was clearly inspired by the \"history\" npm package,\n// This implementation attempts to be more lightweight by\n// making assumptions about the way TanStack Router works\n\nexport interface RouterHistory {\n location: HistoryLocation\n subscribe: (cb: () => void) => () => void\n push: (path: string, state?: any) => void\n replace: (path: string, state?: any) => void\n go: (index: number) => void\n back: () => void\n forward: () => void\n createHref: (href: string) => string\n block: (blocker: BlockerFn) => () => void\n flush: () => void\n destroy: () => void\n notify: () => void\n}\n\nexport interface HistoryLocation extends ParsedPath {\n state: HistoryState\n}\n\nexport interface ParsedPath {\n href: string\n pathname: string\n search: string\n hash: string\n}\n\nexport interface HistoryState {\n key: string\n}\n\ntype ShouldAllowNavigation = any\n\nexport type BlockerFn = () =>\n | Promise<ShouldAllowNavigation>\n | ShouldAllowNavigation\n\nconst pushStateEvent = 'pushstate'\nconst popStateEvent = 'popstate'\nconst beforeUnloadEvent = 'beforeunload'\n\nconst beforeUnloadListener = (event: Event) => {\n event.preventDefault()\n // @ts-ignore\n return (event.returnValue = '')\n}\n\nconst stopBlocking = () => {\n removeEventListener(beforeUnloadEvent, beforeUnloadListener, {\n capture: true,\n })\n}\n\nexport function createHistory(opts: {\n getLocation: () => HistoryLocation\n pushState: (path: string, state: any) => void\n replaceState: (path: string, state: any) => void\n go: (n: number) => void\n back: () => void\n forward: () => void\n createHref: (path: string) => string\n flush?: () => void\n destroy?: () => void\n onBlocked?: (onUpdate: () => void) => void\n}): RouterHistory {\n let location = opts.getLocation()\n let subscribers = new Set<() => void>()\n let blockers: BlockerFn[] = []\n\n const onUpdate = () => {\n location = opts.getLocation()\n subscribers.forEach((subscriber) => subscriber())\n }\n\n const tryNavigation = async (task: () => void) => {\n if (typeof document !== 'undefined' && blockers.length) {\n for (let blocker of blockers) {\n const allowed = await blocker()\n if (!allowed) {\n opts.onBlocked?.(onUpdate)\n return\n }\n }\n }\n\n task()\n }\n\n return {\n get location() {\n return location\n },\n subscribe: (cb: () => void) => {\n subscribers.add(cb)\n\n return () => {\n subscribers.delete(cb)\n }\n },\n push: (path: string, state: any) => {\n state = assignKey(state)\n tryNavigation(() => {\n opts.pushState(path, state)\n onUpdate()\n })\n },\n replace: (path: string, state: any) => {\n state = assignKey(state)\n tryNavigation(() => {\n opts.replaceState(path, state)\n onUpdate()\n })\n },\n go: (index) => {\n tryNavigation(() => {\n opts.go(index)\n })\n },\n back: () => {\n tryNavigation(() => {\n opts.back()\n })\n },\n forward: () => {\n tryNavigation(() => {\n opts.forward()\n })\n },\n createHref: (str) => opts.createHref(str),\n block: (blocker) => {\n blockers.push(blocker)\n\n if (blockers.length === 1) {\n addEventListener(beforeUnloadEvent, beforeUnloadListener, {\n capture: true,\n })\n }\n\n return () => {\n blockers = blockers.filter((b) => b !== blocker)\n\n if (!blockers.length) {\n stopBlocking()\n }\n }\n },\n flush: () => opts.flush?.(),\n destroy: () => opts.destroy?.(),\n notify: onUpdate,\n }\n}\n\nfunction assignKey(state: HistoryState) {\n if (!state) {\n state = {} as HistoryState\n }\n return {\n ...state,\n key: createRandomKey(),\n }\n}\n\n/**\n * Creates a history object that can be used to interact with the browser's\n * navigation. This is a lightweight API wrapping the browser's native methods.\n * It is designed to work with TanStack Router, but could be used as a standalone API as well.\n * IMPORTANT: This API implements history throttling via a microtask to prevent\n * excessive calls to the history API. In some browsers, calling history.pushState or\n * history.replaceState in quick succession can cause the browser to ignore subsequent\n * calls. This API smooths out those differences and ensures that your application\n * state will *eventually* match the browser state. In most cases, this is not a problem,\n * but if you need to ensure that the browser state is up to date, you can use the\n * `history.flush` method to immediately flush all pending state changes to the browser URL.\n * @param opts\n * @param opts.getHref A function that returns the current href (path + search + hash)\n * @param opts.createHref A function that takes a path and returns a href (path + search + hash)\n * @returns A history instance\n */\nexport function createBrowserHistory(opts?: {\n parseLocation?: () => HistoryLocation\n createHref?: (path: string) => string\n window?: any\n}): RouterHistory {\n const win =\n opts?.window ??\n (typeof document !== 'undefined' ? window : (undefined as any))\n\n const createHref = opts?.createHref ?? ((path) => path)\n const parseLocation =\n opts?.parseLocation ??\n (() =>\n parseHref(\n `${win.location.pathname}${win.location.search}${win.location.hash}`,\n win.history.state,\n ))\n\n let currentLocation = parseLocation()\n let rollbackLocation: HistoryLocation | undefined\n\n const getLocation = () => currentLocation\n\n let next:\n | undefined\n | {\n // This is the latest location that we were attempting to push/replace\n href: string\n // This is the latest state that we were attempting to push/replace\n state: any\n // This is the latest type that we were attempting to push/replace\n isPush: boolean\n }\n\n // Because we are proactively updating the location\n // in memory before actually updating the browser history,\n // we need to track when we are doing this so we don't\n // notify subscribers twice on the last update.\n let tracking = true\n\n // We need to track the current scheduled update to prevent\n // multiple updates from being scheduled at the same time.\n let scheduled: Promise<void> | undefined\n\n // This function is a wrapper to prevent any of the callback's\n // side effects from causing a subscriber notification\n const untrack = (fn: () => void) => {\n tracking = false\n fn()\n tracking = true\n }\n\n // This function flushes the next update to the browser history\n const flush = () => {\n // Do not notify subscribers about this push/replace call\n untrack(() => {\n if (!next) return\n win.history[next.isPush ? 'pushState' : 'replaceState'](\n next.state,\n '',\n next.href,\n )\n // Reset the nextIsPush flag and clear the scheduled update\n next = undefined\n scheduled = undefined\n rollbackLocation = undefined\n })\n }\n\n // This function queues up a call to update the browser history\n const queueHistoryAction = (\n type: 'push' | 'replace',\n destHref: string,\n state: any,\n ) => {\n const href = createHref(destHref)\n\n if (!scheduled) {\n rollbackLocation = currentLocation\n }\n\n // Update the location in memory\n currentLocation = parseHref(destHref, state)\n\n // Keep track of the next location we need to flush to the URL\n next = {\n href,\n state,\n isPush: next?.isPush || type === 'push',\n }\n\n if (!scheduled) {\n // Schedule an update to the browser history\n scheduled = Promise.resolve().then(() => flush())\n }\n }\n\n const onPushPop = () => {\n currentLocation = parseLocation()\n history.notify()\n }\n\n var originalPushState = win.history.pushState\n var originalReplaceState = win.history.replaceState\n\n const history = createHistory({\n getLocation,\n pushState: (href, state) => queueHistoryAction('push', href, state),\n replaceState: (href, state) => queueHistoryAction('replace', href, state),\n back: () => win.history.back(),\n forward: () => win.history.forward(),\n go: (n) => win.history.go(n),\n createHref: (href) => createHref(href),\n flush,\n destroy: () => {\n win.history.pushState = originalPushState\n win.history.replaceState = originalReplaceState\n win.removeEventListener(pushStateEvent, onPushPop)\n win.removeEventListener(popStateEvent, onPushPop)\n },\n onBlocked: (onUpdate) => {\n // If a navigation is blocked, we need to rollback the location\n // that we optimistically updated in memory.\n if (rollbackLocation && currentLocation !== rollbackLocation) {\n currentLocation = rollbackLocation\n // Notify subscribers\n onUpdate()\n }\n },\n })\n\n win.addEventListener(pushStateEvent, onPushPop)\n win.addEventListener(popStateEvent, onPushPop)\n\n win.history.pushState = function () {\n let res = originalPushState.apply(win.history, arguments as any)\n if (tracking) history.notify()\n return res\n }\n\n win.history.replaceState = function () {\n let res = originalReplaceState.apply(win.history, arguments as any)\n if (tracking) history.notify()\n return res\n }\n\n return history\n}\n\nexport function createHashHistory(opts?: { window?: any }): RouterHistory {\n const win =\n opts?.window ??\n (typeof document !== 'undefined' ? window : (undefined as any))\n return createBrowserHistory({\n window: win,\n parseLocation: () => {\n const hashHref = win.location.hash.split('#').slice(1).join('#') ?? '/'\n return parseHref(hashHref, win.history.state)\n },\n createHref: (href) =>\n `${win.location.pathname}${win.location.search}#${href}`,\n })\n}\n\nexport function createMemoryHistory(\n opts: {\n initialEntries: string[]\n initialIndex?: number\n } = {\n initialEntries: ['/'],\n },\n): RouterHistory {\n const entries = opts.initialEntries\n let index = opts.initialIndex ?? entries.length - 1\n let currentState = {\n key: createRandomKey(),\n } as HistoryState\n\n const getLocation = () => parseHref(entries[index]!, currentState)\n\n return createHistory({\n getLocation,\n\n pushState: (path, state) => {\n currentState = state\n entries.push(path)\n index++\n },\n replaceState: (path, state) => {\n currentState = state\n entries[index] = path\n },\n back: () => {\n index--\n },\n forward: () => {\n index = Math.min(index + 1, entries.length - 1)\n },\n go: (n) => {\n index = Math.min(Math.max(index + n, 0), entries.length - 1)\n },\n createHref: (path) => path,\n })\n}\n\nfunction parseHref(href: string, state: HistoryState): HistoryLocation {\n let hashIndex = href.indexOf('#')\n let searchIndex = href.indexOf('?')\n\n return {\n href,\n pathname: href.substring(\n 0,\n hashIndex > 0\n ? searchIndex > 0\n ? Math.min(hashIndex, searchIndex)\n : hashIndex\n : searchIndex > 0\n ? searchIndex\n : href.length,\n ),\n hash: hashIndex > -1 ? href.substring(hashIndex) : '',\n search:\n searchIndex > -1\n ? href.slice(searchIndex, hashIndex === -1 ? undefined : hashIndex)\n : '',\n state: state || {},\n }\n}\n\n// Thanks co-pilot!\nfunction createRandomKey() {\n return (Math.random() + 1).toString(36).substring(7)\n}\n"],"names":["pushStateEvent","popStateEvent","beforeUnloadEvent","beforeUnloadListener","event","preventDefault","returnValue","stopBlocking","removeEventListener","capture","createHistory","opts","location","getLocation","subscribers","Set","blockers","onUpdate","forEach","subscriber","tryNavigation","async","document","length","blocker","onBlocked","task","subscribe","cb","add","delete","push","path","state","assignKey","pushState","replace","replaceState","go","index","back","forward","createHref","str","block","addEventListener","filter","b","flush","destroy","notify","key","createRandomKey","createBrowserHistory","win","window","undefined","parseLocation","parseHref","pathname","search","hash","history","rollbackLocation","currentLocation","next","scheduled","tracking","isPush","href","fn","queueHistoryAction","type","destHref","Promise","resolve","then","onPushPop","originalPushState","originalReplaceState","n","res","apply","arguments","hashIndex","indexOf","searchIndex","substring","Math","min","slice","random","toString","split","join","initialEntries","entries","initialIndex","currentState","max"],"mappings":";;;;;;;;;;uPAwCA,MAAMA,EAAiB,YACjBC,EAAgB,WAChBC,EAAoB,eAEpBC,EAAwBC,IAC5BA,EAAMC,iBAEED,EAAME,YAAc,IAGxBC,EAAeA,KACnBC,oBAAoBN,EAAmBC,EAAsB,CAC3DM,SAAS,GACT,EAGG,SAASC,EAAcC,GAY5B,IAAIC,EAAWD,EAAKE,cAChBC,EAAc,IAAIC,IAClBC,EAAwB,GAE5B,MAAMC,EAAWA,KACfL,EAAWD,EAAKE,cAChBC,EAAYI,SAASC,GAAeA,KAAa,EAG7CC,EAAgBC,UACpB,GAAwB,oBAAbC,UAA4BN,EAASO,OAC9C,IAAK,IAAIC,KAAWR,EAAU,CAE5B,UADsBQ,IAGpB,YADAb,EAAKc,YAAYR,EAGrB,CAGFS,GAAM,EAGR,MAAO,CACL,YAAId,GACF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EAAQzD,GAAS2D,GAKnD/D,UAAWA,CAACH,EAAMC,KAChBiE,EAAejE,EACf+D,EAAQjE,KAAKC,GACbO,GAAO,EAETF,aAAcA,CAACL,EAAMC,KACnBiE,EAAejE,EACf+D,EAAQzD,GAASP,CAAI,EAEvBQ,KAAMA,KACJD,GAAO,EAETE,QAASA,KACPF,EAAQiD,KAAKC,IAAIlD,EAAQ,EAAGyD,EAAQzE,OAAS,EAAE,EAEjDe,GAAK0C,IACHzC,EAAQiD,KAAKC,IAAID,KAAKW,IAAI5D,EAAQyC,EAAG,GAAIgB,EAAQzE,OAAS,EAAE,EAE9DmB,WAAaV,GAASA,GAE1B"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tanstack/history",
3
3
  "author": "Tanner Linsley",
4
- "version": "1.0.1",
4
+ "version": "1.0.3",
5
5
  "license": "MIT",
6
6
  "repository": "tanstack/history",
7
7
  "homepage": "https://tanstack.com",
package/src/index.ts CHANGED
@@ -56,8 +56,8 @@ const stopBlocking = () => {
56
56
 
57
57
  export function createHistory(opts: {
58
58
  getLocation: () => HistoryLocation
59
- pushState: (path: string, state: any, onUpdate: () => void) => void
60
- replaceState: (path: string, state: any, onUpdate: () => void) => void
59
+ pushState: (path: string, state: any) => void
60
+ replaceState: (path: string, state: any) => void
61
61
  go: (n: number) => void
62
62
  back: () => void
63
63
  forward: () => void
@@ -103,13 +103,15 @@ export function createHistory(opts: {
103
103
  push: (path: string, state: any) => {
104
104
  state = assignKey(state)
105
105
  tryNavigation(() => {
106
- opts.pushState(path, state, onUpdate)
106
+ opts.pushState(path, state)
107
+ onUpdate()
107
108
  })
108
109
  },
109
110
  replace: (path: string, state: any) => {
110
111
  state = assignKey(state)
111
112
  tryNavigation(() => {
112
- opts.replaceState(path, state, onUpdate)
113
+ opts.replaceState(path, state)
114
+ onUpdate()
113
115
  })
114
116
  },
115
117
  go: (index) => {
@@ -251,7 +253,6 @@ export function createBrowserHistory(opts?: {
251
253
  type: 'push' | 'replace',
252
254
  destHref: string,
253
255
  state: any,
254
- onUpdate: () => void,
255
256
  ) => {
256
257
  const href = createHref(destHref)
257
258
 
@@ -269,9 +270,6 @@ export function createBrowserHistory(opts?: {
269
270
  isPush: next?.isPush || type === 'push',
270
271
  }
271
272
 
272
- // Notify subscribers
273
- onUpdate()
274
-
275
273
  if (!scheduled) {
276
274
  // Schedule an update to the browser history
277
275
  scheduled = Promise.resolve().then(() => flush())
@@ -288,10 +286,8 @@ export function createBrowserHistory(opts?: {
288
286
 
289
287
  const history = createHistory({
290
288
  getLocation,
291
- pushState: (href, state, onUpdate) =>
292
- queueHistoryAction('push', href, state, onUpdate),
293
- replaceState: (href, state, onUpdate) =>
294
- queueHistoryAction('replace', href, state, onUpdate),
289
+ pushState: (href, state) => queueHistoryAction('push', href, state),
290
+ replaceState: (href, state) => queueHistoryAction('replace', href, state),
295
291
  back: () => win.history.back(),
296
292
  forward: () => win.history.forward(),
297
293
  go: (n) => win.history.go(n),
@@ -365,6 +361,7 @@ export function createMemoryHistory(
365
361
 
366
362
  return createHistory({
367
363
  getLocation,
364
+
368
365
  pushState: (path, state) => {
369
366
  currentState = state
370
367
  entries.push(path)