@tanstack/history 0.0.1-beta.245 → 0.0.1-beta.246
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.
- package/build/cjs/index.js +34 -22
- package/build/cjs/index.js.map +1 -1
- package/build/esm/index.js +34 -22
- package/build/esm/index.js.map +1 -1
- package/build/stats-html.html +1 -1
- package/build/stats-react.json +7 -7
- package/build/types/index.d.ts +1 -2
- package/build/umd/index.development.js +34 -22
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +1 -1
- package/build/umd/index.production.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +42 -26
package/build/cjs/index.js
CHANGED
|
@@ -31,26 +31,20 @@ function createHistory(opts) {
|
|
|
31
31
|
let location = opts.getLocation();
|
|
32
32
|
let subscribers = new Set();
|
|
33
33
|
let blockers = [];
|
|
34
|
-
let queue = [];
|
|
35
34
|
const onUpdate = () => {
|
|
36
35
|
location = opts.getLocation();
|
|
37
36
|
subscribers.forEach(subscriber => subscriber());
|
|
38
37
|
};
|
|
39
|
-
const
|
|
38
|
+
const tryNavigation = task => {
|
|
40
39
|
if (blockers.length) {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
while (queue.length) {
|
|
48
|
-
queue.shift()?.();
|
|
40
|
+
for (let blocker of blockers) {
|
|
41
|
+
if (!window.confirm(blocker.message)) {
|
|
42
|
+
opts.onBlocked?.(onUpdate);
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
49
46
|
}
|
|
50
|
-
|
|
51
|
-
const queueTask = task => {
|
|
52
|
-
queue.push(task);
|
|
53
|
-
tryUnblock();
|
|
47
|
+
task();
|
|
54
48
|
};
|
|
55
49
|
return {
|
|
56
50
|
get location() {
|
|
@@ -64,41 +58,44 @@ function createHistory(opts) {
|
|
|
64
58
|
},
|
|
65
59
|
push: (path, state) => {
|
|
66
60
|
state = assignKey(state);
|
|
67
|
-
|
|
61
|
+
tryNavigation(() => {
|
|
68
62
|
opts.pushState(path, state, onUpdate);
|
|
69
63
|
});
|
|
70
64
|
},
|
|
71
65
|
replace: (path, state) => {
|
|
72
66
|
state = assignKey(state);
|
|
73
|
-
|
|
67
|
+
tryNavigation(() => {
|
|
74
68
|
opts.replaceState(path, state, onUpdate);
|
|
75
69
|
});
|
|
76
70
|
},
|
|
77
71
|
go: index => {
|
|
78
|
-
|
|
72
|
+
tryNavigation(() => {
|
|
79
73
|
opts.go(index);
|
|
80
74
|
});
|
|
81
75
|
},
|
|
82
76
|
back: () => {
|
|
83
|
-
|
|
77
|
+
tryNavigation(() => {
|
|
84
78
|
opts.back();
|
|
85
79
|
});
|
|
86
80
|
},
|
|
87
81
|
forward: () => {
|
|
88
|
-
|
|
82
|
+
tryNavigation(() => {
|
|
89
83
|
opts.forward();
|
|
90
84
|
});
|
|
91
85
|
},
|
|
92
86
|
createHref: str => opts.createHref(str),
|
|
93
|
-
block:
|
|
94
|
-
|
|
87
|
+
block: message => {
|
|
88
|
+
const payload = {
|
|
89
|
+
message
|
|
90
|
+
};
|
|
91
|
+
blockers.push(payload);
|
|
95
92
|
if (blockers.length === 1) {
|
|
96
93
|
addEventListener(beforeUnloadEvent, beforeUnloadListener, {
|
|
97
94
|
capture: true
|
|
98
95
|
});
|
|
99
96
|
}
|
|
100
97
|
return () => {
|
|
101
|
-
blockers = blockers.filter(b => b !==
|
|
98
|
+
blockers = blockers.filter(b => b !== payload);
|
|
102
99
|
if (!blockers.length) {
|
|
103
100
|
stopBlocking();
|
|
104
101
|
}
|
|
@@ -139,6 +136,7 @@ function createBrowserHistory(opts) {
|
|
|
139
136
|
const getHref = opts?.getHref ?? (() => `${window.location.pathname}${window.location.search}${window.location.hash}`);
|
|
140
137
|
const createHref = opts?.createHref ?? (path => path);
|
|
141
138
|
let currentLocation = parseLocation(getHref(), window.history.state);
|
|
139
|
+
let rollbackLocation;
|
|
142
140
|
const getLocation = () => currentLocation;
|
|
143
141
|
let next;
|
|
144
142
|
|
|
@@ -169,12 +167,16 @@ function createBrowserHistory(opts) {
|
|
|
169
167
|
// Reset the nextIsPush flag and clear the scheduled update
|
|
170
168
|
next = undefined;
|
|
171
169
|
scheduled = undefined;
|
|
170
|
+
rollbackLocation = undefined;
|
|
172
171
|
});
|
|
173
172
|
};
|
|
174
173
|
|
|
175
174
|
// This function queues up a call to update the browser history
|
|
176
175
|
const queueHistoryAction = (type, path, state, onUpdate) => {
|
|
177
176
|
const href = createHref(path);
|
|
177
|
+
if (!scheduled) {
|
|
178
|
+
rollbackLocation = currentLocation;
|
|
179
|
+
}
|
|
178
180
|
|
|
179
181
|
// Update the location in memory
|
|
180
182
|
currentLocation = parseLocation(href, state);
|
|
@@ -185,6 +187,7 @@ function createBrowserHistory(opts) {
|
|
|
185
187
|
state,
|
|
186
188
|
isPush: next?.isPush || type === 'push'
|
|
187
189
|
};
|
|
190
|
+
|
|
188
191
|
// Notify subscribers
|
|
189
192
|
onUpdate();
|
|
190
193
|
if (!scheduled) {
|
|
@@ -212,6 +215,15 @@ function createBrowserHistory(opts) {
|
|
|
212
215
|
window.history.replaceState = originalReplaceState;
|
|
213
216
|
window.removeEventListener(pushStateEvent, onPushPop);
|
|
214
217
|
window.removeEventListener(popStateEvent, onPushPop);
|
|
218
|
+
},
|
|
219
|
+
onBlocked: onUpdate => {
|
|
220
|
+
// If a navigation is blocked, we need to rollback the location
|
|
221
|
+
// that we optimistically updated in memory.
|
|
222
|
+
if (rollbackLocation && currentLocation !== rollbackLocation) {
|
|
223
|
+
currentLocation = rollbackLocation;
|
|
224
|
+
// Notify subscribers
|
|
225
|
+
onUpdate();
|
|
226
|
+
}
|
|
215
227
|
}
|
|
216
228
|
});
|
|
217
229
|
window.addEventListener(pushStateEvent, onPushPop);
|
package/build/cjs/index.js.map
CHANGED
|
@@ -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: (blockerFn: 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 BlockerFn = (retry: () => void, cancel: () => void) => void\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\nfunction 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}): RouterHistory {\n let location = opts.getLocation()\n let subscribers = new Set<() => void>()\n let blockers: BlockerFn[] = []\n let queue: (() => void)[] = []\n\n const onUpdate = () => {\n location = opts.getLocation()\n subscribers.forEach((subscriber) => subscriber())\n }\n\n const tryUnblock = () => {\n if (blockers.length) {\n blockers[0]?.(tryUnblock, () => {\n blockers = []\n stopBlocking()\n })\n return\n }\n\n while (queue.length) {\n queue.shift()?.()\n }\n }\n\n const queueTask = (task: () => void) => {\n queue.push(task)\n tryUnblock()\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 queueTask(() => {\n opts.pushState(path, state, onUpdate)\n })\n },\n replace: (path: string, state: any) => {\n state = assignKey(state)\n queueTask(() => {\n opts.replaceState(path, state, onUpdate)\n })\n },\n go: (index) => {\n queueTask(() => {\n opts.go(index)\n })\n },\n back: () => {\n queueTask(() => {\n opts.back()\n })\n },\n forward: () => {\n queueTask(() => {\n opts.forward()\n })\n },\n createHref: (str) => opts.createHref(str),\n block: (cb) => {\n blockers.push(cb)\n\n if (blockers.length === 1) {\n addEventListener(beforeUnloadEvent, beforeUnloadListener, {\n capture: true,\n })\n }\n\n return () => {\n blockers = blockers.filter((b) => b !== cb)\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 getHref?: () => string\n createHref?: (path: string) => string\n}): RouterHistory {\n const getHref =\n opts?.getHref ??\n (() =>\n `${window.location.pathname}${window.location.search}${window.location.hash}`)\n\n const createHref = opts?.createHref ?? ((path) => path)\n\n let currentLocation = parseLocation(getHref(), window.history.state)\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 window.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 })\n }\n\n // This function queues up a call to update the browser history\n const queueHistoryAction = (\n type: 'push' | 'replace',\n path: string,\n state: any,\n onUpdate: () => void,\n ) => {\n const href = createHref(path)\n\n // Update the location in memory\n currentLocation = parseLocation(href, 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 // 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(getHref(), window.history.state)\n history.notify()\n }\n\n var originalPushState = window.history.pushState\n var originalReplaceState = window.history.replaceState\n\n const history = createHistory({\n getLocation,\n pushState: (path, state, onUpdate) =>\n queueHistoryAction('push', path, state, onUpdate),\n replaceState: (path, state, onUpdate) =>\n queueHistoryAction('replace', path, state, onUpdate),\n back: () => window.history.back(),\n forward: () => window.history.forward(),\n go: (n) => window.history.go(n),\n createHref: (path) => createHref(path),\n flush,\n destroy: () => {\n window.history.pushState = originalPushState\n window.history.replaceState = originalReplaceState\n window.removeEventListener(pushStateEvent, onPushPop)\n window.removeEventListener(popStateEvent, onPushPop)\n },\n })\n\n window.addEventListener(pushStateEvent, onPushPop)\n window.addEventListener(popStateEvent, onPushPop)\n\n window.history.pushState = function () {\n let res = originalPushState.apply(window.history, arguments as any)\n if (tracking) history.notify()\n return res\n }\n\n window.history.replaceState = function () {\n let res = originalReplaceState.apply(window.history, arguments as any)\n if (tracking) history.notify()\n return res\n }\n\n return history\n}\n\nexport function createHashHistory(): RouterHistory {\n return createBrowserHistory({\n getHref: () => window.location.hash.substring(1),\n createHref: (path) => `#${path}`,\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 = () => parseLocation(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) => window.history.go(n),\n createHref: (path) => path,\n })\n}\n\nfunction parseLocation(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","queue","onUpdate","forEach","subscriber","tryUnblock","length","shift","queueTask","task","push","subscribe","cb","add","delete","path","state","assignKey","pushState","replace","replaceState","go","index","back","forward","createHref","str","block","addEventListener","filter","b","flush","destroy","notify","key","createRandomKey","createBrowserHistory","getHref","window","pathname","search","hash","currentLocation","parseLocation","history","next","tracking","scheduled","untrack","fn","isPush","href","undefined","queueHistoryAction","type","Promise","resolve","then","onPushPop","originalPushState","originalReplaceState","n","res","apply","arguments","createHashHistory","substring","createMemoryHistory","initialEntries","entries","initialIndex","currentState","Math","min","hashIndex","indexOf","searchIndex","slice","random","toString"],"mappings":";;;;;;;;;;;;AAAA;AACA;AACA;;AAkCA,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;AAED,SAASC,aAAaA,CAACC,IAUtB,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;EAC9B,IAAIC,KAAqB,GAAG,EAAE,CAAA;EAE9B,MAAMC,QAAQ,GAAGA,MAAM;AACrBN,IAAAA,QAAQ,GAAGD,IAAI,CAACE,WAAW,EAAE,CAAA;IAC7BC,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CAAC,GACbA,WAAW,GACXzB,IAAI,CAAC7C,MACb,CAAC;AACDmC,IAAAA,IAAI,EAAEiC,SAAS,GAAG,CAAC,CAAC,GAAGvB,IAAI,CAACe,SAAS,CAACQ,SAAS,CAAC,GAAG,EAAE;IACrDlC,MAAM,EACJoC,WAAW,GAAG,CAAC,CAAC,GACZzB,IAAI,CAAC0B,KAAK,CAACD,WAAW,EAAEF,SAAS,KAAK,CAAC,CAAC,GAAGtB,SAAS,GAAGsB,SAAS,CAAC,GACjE,EAAE;IACR1D,KAAK,EAAEA,KAAK,IAAI,EAAC;GAClB,CAAA;AACH,CAAA;;AAEA;AACA,SAASmB,eAAeA,GAAG;AACzB,EAAA,OAAO,CAACqC,IAAI,CAACM,MAAM,EAAE,GAAG,CAAC,EAAEC,QAAQ,CAAC,EAAE,CAAC,CAACb,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: (message: string) => () => 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 Blocker = {\n message: string\n}\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\nfunction 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: Blocker[] = []\n\n const onUpdate = () => {\n location = opts.getLocation()\n subscribers.forEach((subscriber) => subscriber())\n }\n\n const tryNavigation = (task: () => void) => {\n if (blockers.length) {\n for (let blocker of blockers) {\n if (!window.confirm(blocker.message)) {\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: (message) => {\n const payload: Blocker = {\n message,\n }\n\n blockers.push(payload)\n\n if (blockers.length === 1) {\n addEventListener(beforeUnloadEvent, beforeUnloadListener, {\n capture: true,\n })\n }\n\n return () => {\n blockers = blockers.filter((b) => b !== payload)\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 getHref?: () => string\n createHref?: (path: string) => string\n}): RouterHistory {\n const getHref =\n opts?.getHref ??\n (() =>\n `${window.location.pathname}${window.location.search}${window.location.hash}`)\n\n const createHref = opts?.createHref ?? ((path) => path)\n\n let currentLocation = parseLocation(getHref(), window.history.state)\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 window.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 path: string,\n state: any,\n onUpdate: () => void,\n ) => {\n const href = createHref(path)\n\n if (!scheduled) {\n rollbackLocation = currentLocation\n }\n\n // Update the location in memory\n currentLocation = parseLocation(href, 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(getHref(), window.history.state)\n history.notify()\n }\n\n var originalPushState = window.history.pushState\n var originalReplaceState = window.history.replaceState\n\n const history = createHistory({\n getLocation,\n pushState: (path, state, onUpdate) =>\n queueHistoryAction('push', path, state, onUpdate),\n replaceState: (path, state, onUpdate) =>\n queueHistoryAction('replace', path, state, onUpdate),\n back: () => window.history.back(),\n forward: () => window.history.forward(),\n go: (n) => window.history.go(n),\n createHref: (path) => createHref(path),\n flush,\n destroy: () => {\n window.history.pushState = originalPushState\n window.history.replaceState = originalReplaceState\n window.removeEventListener(pushStateEvent, onPushPop)\n window.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 window.addEventListener(pushStateEvent, onPushPop)\n window.addEventListener(popStateEvent, onPushPop)\n\n window.history.pushState = function () {\n let res = originalPushState.apply(window.history, arguments as any)\n if (tracking) history.notify()\n return res\n }\n\n window.history.replaceState = function () {\n let res = originalReplaceState.apply(window.history, arguments as any)\n if (tracking) history.notify()\n return res\n }\n\n return history\n}\n\nexport function createHashHistory(): RouterHistory {\n return createBrowserHistory({\n getHref: () => window.location.hash.substring(1),\n createHref: (path) => `#${path}`,\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 = () => parseLocation(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) => window.history.go(n),\n createHref: (path) => path,\n })\n}\n\nfunction parseLocation(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","length","blocker","window","confirm","message","onBlocked","subscribe","cb","add","delete","push","path","state","assignKey","pushState","replace","replaceState","go","index","back","forward","createHref","str","block","payload","addEventListener","filter","b","flush","destroy","notify","key","createRandomKey","createBrowserHistory","getHref","pathname","search","hash","currentLocation","parseLocation","history","rollbackLocation","next","tracking","scheduled","untrack","fn","isPush","href","undefined","queueHistoryAction","type","Promise","resolve","then","onPushPop","originalPushState","originalReplaceState","n","res","apply","arguments","createHashHistory","substring","createMemoryHistory","initialEntries","entries","initialIndex","currentState","Math","min","h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AAC,GAAGvB,IAAI,CAACe,SAAS,CAACQ,SAAS,CAAC,GAAG,EAAE;IACrDnC,MAAM,EACJqC,WAAW,GAAG,CAAC,CAAC,GACZzB,IAAI,CAAC0B,KAAK,CAACD,WAAW,EAAEF,SAAS,KAAK,CAAC,CAAC,GAAGtB,SAAS,GAAGsB,SAAS,CAAC,GACjE,EAAE;IACR3D,KAAK,EAAEA,KAAK,IAAI,EAAC;GAClB,CAAA;AACH,CAAA;;AAEA;AACA,SAASoB,eAAeA,GAAG;AACzB,EAAA,OAAO,CAACqC,IAAI,CAACM,MAAM,EAAE,GAAG,CAAC,EAAEC,QAAQ,CAAC,EAAE,CAAC,CAACb,SAAS,CAAC,CAAC,CAAC,CAAA;AACtD;;;;;;"}
|
package/build/esm/index.js
CHANGED
|
@@ -29,26 +29,20 @@ function createHistory(opts) {
|
|
|
29
29
|
let location = opts.getLocation();
|
|
30
30
|
let subscribers = new Set();
|
|
31
31
|
let blockers = [];
|
|
32
|
-
let queue = [];
|
|
33
32
|
const onUpdate = () => {
|
|
34
33
|
location = opts.getLocation();
|
|
35
34
|
subscribers.forEach(subscriber => subscriber());
|
|
36
35
|
};
|
|
37
|
-
const
|
|
36
|
+
const tryNavigation = task => {
|
|
38
37
|
if (blockers.length) {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
while (queue.length) {
|
|
46
|
-
queue.shift()?.();
|
|
38
|
+
for (let blocker of blockers) {
|
|
39
|
+
if (!window.confirm(blocker.message)) {
|
|
40
|
+
opts.onBlocked?.(onUpdate);
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
47
44
|
}
|
|
48
|
-
|
|
49
|
-
const queueTask = task => {
|
|
50
|
-
queue.push(task);
|
|
51
|
-
tryUnblock();
|
|
45
|
+
task();
|
|
52
46
|
};
|
|
53
47
|
return {
|
|
54
48
|
get location() {
|
|
@@ -62,41 +56,44 @@ function createHistory(opts) {
|
|
|
62
56
|
},
|
|
63
57
|
push: (path, state) => {
|
|
64
58
|
state = assignKey(state);
|
|
65
|
-
|
|
59
|
+
tryNavigation(() => {
|
|
66
60
|
opts.pushState(path, state, onUpdate);
|
|
67
61
|
});
|
|
68
62
|
},
|
|
69
63
|
replace: (path, state) => {
|
|
70
64
|
state = assignKey(state);
|
|
71
|
-
|
|
65
|
+
tryNavigation(() => {
|
|
72
66
|
opts.replaceState(path, state, onUpdate);
|
|
73
67
|
});
|
|
74
68
|
},
|
|
75
69
|
go: index => {
|
|
76
|
-
|
|
70
|
+
tryNavigation(() => {
|
|
77
71
|
opts.go(index);
|
|
78
72
|
});
|
|
79
73
|
},
|
|
80
74
|
back: () => {
|
|
81
|
-
|
|
75
|
+
tryNavigation(() => {
|
|
82
76
|
opts.back();
|
|
83
77
|
});
|
|
84
78
|
},
|
|
85
79
|
forward: () => {
|
|
86
|
-
|
|
80
|
+
tryNavigation(() => {
|
|
87
81
|
opts.forward();
|
|
88
82
|
});
|
|
89
83
|
},
|
|
90
84
|
createHref: str => opts.createHref(str),
|
|
91
|
-
block:
|
|
92
|
-
|
|
85
|
+
block: message => {
|
|
86
|
+
const payload = {
|
|
87
|
+
message
|
|
88
|
+
};
|
|
89
|
+
blockers.push(payload);
|
|
93
90
|
if (blockers.length === 1) {
|
|
94
91
|
addEventListener(beforeUnloadEvent, beforeUnloadListener, {
|
|
95
92
|
capture: true
|
|
96
93
|
});
|
|
97
94
|
}
|
|
98
95
|
return () => {
|
|
99
|
-
blockers = blockers.filter(b => b !==
|
|
96
|
+
blockers = blockers.filter(b => b !== payload);
|
|
100
97
|
if (!blockers.length) {
|
|
101
98
|
stopBlocking();
|
|
102
99
|
}
|
|
@@ -137,6 +134,7 @@ function createBrowserHistory(opts) {
|
|
|
137
134
|
const getHref = opts?.getHref ?? (() => `${window.location.pathname}${window.location.search}${window.location.hash}`);
|
|
138
135
|
const createHref = opts?.createHref ?? (path => path);
|
|
139
136
|
let currentLocation = parseLocation(getHref(), window.history.state);
|
|
137
|
+
let rollbackLocation;
|
|
140
138
|
const getLocation = () => currentLocation;
|
|
141
139
|
let next;
|
|
142
140
|
|
|
@@ -167,12 +165,16 @@ function createBrowserHistory(opts) {
|
|
|
167
165
|
// Reset the nextIsPush flag and clear the scheduled update
|
|
168
166
|
next = undefined;
|
|
169
167
|
scheduled = undefined;
|
|
168
|
+
rollbackLocation = undefined;
|
|
170
169
|
});
|
|
171
170
|
};
|
|
172
171
|
|
|
173
172
|
// This function queues up a call to update the browser history
|
|
174
173
|
const queueHistoryAction = (type, path, state, onUpdate) => {
|
|
175
174
|
const href = createHref(path);
|
|
175
|
+
if (!scheduled) {
|
|
176
|
+
rollbackLocation = currentLocation;
|
|
177
|
+
}
|
|
176
178
|
|
|
177
179
|
// Update the location in memory
|
|
178
180
|
currentLocation = parseLocation(href, state);
|
|
@@ -183,6 +185,7 @@ function createBrowserHistory(opts) {
|
|
|
183
185
|
state,
|
|
184
186
|
isPush: next?.isPush || type === 'push'
|
|
185
187
|
};
|
|
188
|
+
|
|
186
189
|
// Notify subscribers
|
|
187
190
|
onUpdate();
|
|
188
191
|
if (!scheduled) {
|
|
@@ -210,6 +213,15 @@ function createBrowserHistory(opts) {
|
|
|
210
213
|
window.history.replaceState = originalReplaceState;
|
|
211
214
|
window.removeEventListener(pushStateEvent, onPushPop);
|
|
212
215
|
window.removeEventListener(popStateEvent, onPushPop);
|
|
216
|
+
},
|
|
217
|
+
onBlocked: onUpdate => {
|
|
218
|
+
// If a navigation is blocked, we need to rollback the location
|
|
219
|
+
// that we optimistically updated in memory.
|
|
220
|
+
if (rollbackLocation && currentLocation !== rollbackLocation) {
|
|
221
|
+
currentLocation = rollbackLocation;
|
|
222
|
+
// Notify subscribers
|
|
223
|
+
onUpdate();
|
|
224
|
+
}
|
|
213
225
|
}
|
|
214
226
|
});
|
|
215
227
|
window.addEventListener(pushStateEvent, onPushPop);
|
package/build/esm/index.js.map
CHANGED
|
@@ -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: (blockerFn: 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 BlockerFn = (retry: () => void, cancel: () => void) => void\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\nfunction 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}): RouterHistory {\n let location = opts.getLocation()\n let subscribers = new Set<() => void>()\n let blockers: BlockerFn[] = []\n let queue: (() => void)[] = []\n\n const onUpdate = () => {\n location = opts.getLocation()\n subscribers.forEach((subscriber) => subscriber())\n }\n\n const tryUnblock = () => {\n if (blockers.length) {\n blockers[0]?.(tryUnblock, () => {\n blockers = []\n stopBlocking()\n })\n return\n }\n\n while (queue.length) {\n queue.shift()?.()\n }\n }\n\n const queueTask = (task: () => void) => {\n queue.push(task)\n tryUnblock()\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 queueTask(() => {\n opts.pushState(path, state, onUpdate)\n })\n },\n replace: (path: string, state: any) => {\n state = assignKey(state)\n queueTask(() => {\n opts.replaceState(path, state, onUpdate)\n })\n },\n go: (index) => {\n queueTask(() => {\n opts.go(index)\n })\n },\n back: () => {\n queueTask(() => {\n opts.back()\n })\n },\n forward: () => {\n queueTask(() => {\n opts.forward()\n })\n },\n createHref: (str) => opts.createHref(str),\n block: (cb) => {\n blockers.push(cb)\n\n if (blockers.length === 1) {\n addEventListener(beforeUnloadEvent, beforeUnloadListener, {\n capture: true,\n })\n }\n\n return () => {\n blockers = blockers.filter((b) => b !== cb)\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 getHref?: () => string\n createHref?: (path: string) => string\n}): RouterHistory {\n const getHref =\n opts?.getHref ??\n (() =>\n `${window.location.pathname}${window.location.search}${window.location.hash}`)\n\n const createHref = opts?.createHref ?? ((path) => path)\n\n let currentLocation = parseLocation(getHref(), window.history.state)\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 window.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 })\n }\n\n // This function queues up a call to update the browser history\n const queueHistoryAction = (\n type: 'push' | 'replace',\n path: string,\n state: any,\n onUpdate: () => void,\n ) => {\n const href = createHref(path)\n\n // Update the location in memory\n currentLocation = parseLocation(href, 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 // 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(getHref(), window.history.state)\n history.notify()\n }\n\n var originalPushState = window.history.pushState\n var originalReplaceState = window.history.replaceState\n\n const history = createHistory({\n getLocation,\n pushState: (path, state, onUpdate) =>\n queueHistoryAction('push', path, state, onUpdate),\n replaceState: (path, state, onUpdate) =>\n queueHistoryAction('replace', path, state, onUpdate),\n back: () => window.history.back(),\n forward: () => window.history.forward(),\n go: (n) => window.history.go(n),\n createHref: (path) => createHref(path),\n flush,\n destroy: () => {\n window.history.pushState = originalPushState\n window.history.replaceState = originalReplaceState\n window.removeEventListener(pushStateEvent, onPushPop)\n window.removeEventListener(popStateEvent, onPushPop)\n },\n })\n\n window.addEventListener(pushStateEvent, onPushPop)\n window.addEventListener(popStateEvent, onPushPop)\n\n window.history.pushState = function () {\n let res = originalPushState.apply(window.history, arguments as any)\n if (tracking) history.notify()\n return res\n }\n\n window.history.replaceState = function () {\n let res = originalReplaceState.apply(window.history, arguments as any)\n if (tracking) history.notify()\n return res\n }\n\n return history\n}\n\nexport function createHashHistory(): RouterHistory {\n return createBrowserHistory({\n getHref: () => window.location.hash.substring(1),\n createHref: (path) => `#${path}`,\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 = () => parseLocation(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) => window.history.go(n),\n createHref: (path) => path,\n })\n}\n\nfunction parseLocation(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","queue","onUpdate","forEach","subscriber","tryUnblock","length","shift","queueTask","task","push","subscribe","cb","add","delete","path","state","assignKey","pushState","replace","replaceState","go","index","back","forward","createHref","str","block","addEventListener","filter","b","flush","destroy","notify","key","createRandomKey","createBrowserHistory","getHref","window","pathname","search","hash","currentLocation","parseLocation","history","next","tracking","scheduled","untrack","fn","isPush","href","undefined","queueHistoryAction","type","Promise","resolve","then","onPushPop","originalPushState","originalReplaceState","n","res","apply","arguments","createHashHistory","substring","createMemoryHistory","initialEntries","entries","initialIndex","currentState","Math","min","hashIndex","indexOf","searchIndex","slice","random","toString"],"mappings":";;;;;;;;;;AAAA;AACA;AACA;;AAkCA,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;AAED,SAASC,aAAaA,CAACC,IAUtB,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;EAC9B,IAAIC,KAAqB,GAAG,EAAE,CAAA;EAE9B,MAAMC,QAAQ,GAAGA,MAAM;AACrBN,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ACC,SAAS,EAAEE,WAAW,CAAC,GAChCF,SAAS,GACXE,WAAW,GAAG,CAAC,GACbA,WAAW,GACXzB,IAAI,CAAC7C,MACb,CAAC;AACDmC,IAAAA,IAAI,EAAEiC,SAAS,GAAG,CAAC,CAAC,GAAGvB,IAAI,CAACe,SAAS,CAACQ,SAAS,CAAC,GAAG,EAAE;IACrDlC,MAAM,EACJoC,WAAW,GAAG,CAAC,CAAC,GACZzB,IAAI,CAAC0B,KAAK,CAACD,WAAW,EAAEF,SAAS,KAAK,CAAC,CAAC,GAAGtB,SAAS,GAAGsB,SAAS,CAAC,GACjE,EAAE;IACR1D,KAAK,EAAEA,KAAK,IAAI,EAAC;GAClB,CAAA;AACH,CAAA;;AAEA;AACA,SAASmB,eAAeA,GAAG;AACzB,EAAA,OAAO,CAACqC,IAAI,CAACM,MAAM,EAAE,GAAG,CAAC,EAAEC,QAAQ,CAAC,EAAE,CAAC,CAACb,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: (message: string) => () => 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 Blocker = {\n message: string\n}\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\nfunction 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: Blocker[] = []\n\n const onUpdate = () => {\n location = opts.getLocation()\n subscribers.forEach((subscriber) => subscriber())\n }\n\n const tryNavigation = (task: () => void) => {\n if (blockers.length) {\n for (let blocker of blockers) {\n if (!window.confirm(blocker.message)) {\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: (message) => {\n const payload: Blocker = {\n message,\n }\n\n blockers.push(payload)\n\n if (blockers.length === 1) {\n addEventListener(beforeUnloadEvent, beforeUnloadListener, {\n capture: true,\n })\n }\n\n return () => {\n blockers = blockers.filter((b) => b !== payload)\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 getHref?: () => string\n createHref?: (path: string) => string\n}): RouterHistory {\n const getHref =\n opts?.getHref ??\n (() =>\n `${window.location.pathname}${window.location.search}${window.location.hash}`)\n\n const createHref = opts?.createHref ?? ((path) => path)\n\n let currentLocation = parseLocation(getHref(), window.history.state)\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 window.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 path: string,\n state: any,\n onUpdate: () => void,\n ) => {\n const href = createHref(path)\n\n if (!scheduled) {\n rollbackLocation = currentLocation\n }\n\n // Update the location in memory\n currentLocation = parseLocation(href, 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(getHref(), window.history.state)\n history.notify()\n }\n\n var originalPushState = window.history.pushState\n var originalReplaceState = window.history.replaceState\n\n const history = createHistory({\n getLocation,\n pushState: (path, state, onUpdate) =>\n queueHistoryAction('push', path, state, onUpdate),\n replaceState: (path, state, onUpdate) =>\n queueHistoryAction('replace', path, state, onUpdate),\n back: () => window.history.back(),\n forward: () => window.history.forward(),\n go: (n) => window.history.go(n),\n createHref: (path) => createHref(path),\n flush,\n destroy: () => {\n window.history.pushState = originalPushState\n window.history.replaceState = originalReplaceState\n window.removeEventListener(pushStateEvent, onPushPop)\n window.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 window.addEventListener(pushStateEvent, onPushPop)\n window.addEventListener(popStateEvent, onPushPop)\n\n window.history.pushState = function () {\n let res = originalPushState.apply(window.history, arguments as any)\n if (tracking) history.notify()\n return res\n }\n\n window.history.replaceState = function () {\n let res = originalReplaceState.apply(window.history, arguments as any)\n if (tracking) history.notify()\n return res\n }\n\n return history\n}\n\nexport function createHashHistory(): RouterHistory {\n return createBrowserHistory({\n getHref: () => window.location.hash.substring(1),\n createHref: (path) => `#${path}`,\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 = () => parseLocation(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) => window.history.go(n),\n createHref: (path) => path,\n })\n}\n\nfunction parseLocation(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","length","blocker","window","confirm","message","onBlocked","subscribe","cb","add","delete","push","path","state","assignKey","pushState","replace","replaceState","go","index","back","forward","createHref","str","block","payload","addEventListener","filter","b","flush","destroy","notify","key","createRandomKey","createBrowserHistory","getHref","pathname","search","hash","currentLocation","parseLocation","history","rollbackLocation","next","tracking","scheduled","untrack","fn","isPush","href","undefined","queueHistoryAction","type","Promise","resolve","then","onPushPop","originalPushState","originalReplaceState","n","res","apply","arguments","createHashHistory","substring","createMemoryHistory","initialEntries","entries","initialIndex","currentState","Math","min","hashIndex","indexOf","searchIndex","slice","random","toString"],"mappings":";;;;;;;;;;AAAA;AACA;AACA;;AAoCA,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;AAED,SAASC,aAAaA,CAACC,IAWtB,EAAiB;AAChB,EAAA,IAAIC,QAAQ,GAAGD,IAAI,CAACE,WAAW,EAAE,CAAA;AACjC,EAAA,IAAIC,WAAW,GAAG,IAAIC,GAAG,EAAc,CAAA;EACvC,IAAIC,QAAmB,GAAG,EAAE,CAAA;EAE5B,MAAMC,QAAQ,GAAGA,MAAM;AACrBL,IAAAA,QAAQ,GAAGD,IAAI,CAACE,WAAW,EAAE,CAAA;IAC7BC,WAAW,CAACI,OAAO,CAAEC,UAAU,IAAKA,UAAU,EAAE,CAAC,CAAA;GAClD,CAAA;EAED,MAAMC,aAAa,GAAIC,IAAgB,IAAK;IAC1C,IAAIL,QAAQ,CAACM,MAAM,EAAE;AACnB,MAAA,KAAK,IAAIC,OAAO,IAAIP,QAAQ,EAAE;QAC5B,IAAI,CAACQ,MAAM,CAACC,OAAO,CAACF,OAAO,CAACG,OAAO,CAAC,EAAE;AACpCf,UAAAA,IAAI,CAACgB,SAAS,GAAGV,QAAQ,CAAC,CAAA;AAC1B,UAAA,OAAA;AACF,SAAA;AAC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,CAAC;AACDqC,IAAAA,IAAI,EAAEkC,SAAS,GAAG,CAAC,CAAC,GAAGvB,IAAI,CAACe,SAAS,CAACQ,SAAS,CAAC,GAAG,EAAE;IACrDnC,MAAM,EACJqC,WAAW,GAAG,CAAC,CAAC,GACZzB,IAAI,CAAC0B,KAAK,CAACD,WAAW,EAAEF,SAAS,KAAK,CAAC,CAAC,GAAGtB,SAAS,GAAGsB,SAAS,CAAC,GACjE,EAAE;IACR3D,KAAK,EAAEA,KAAK,IAAI,EAAC;GAClB,CAAA;AACH,CAAA;;AAEA;AACA,SAASoB,eAAeA,GAAG;AACzB,EAAA,OAAO,CAACqC,IAAI,CAACM,MAAM,EAAE,GAAG,CAAC,EAAEC,QAAQ,CAAC,EAAE,CAAC,CAACb,SAAS,CAAC,CAAC,CAAC,CAAA;AACtD;;;;"}
|
package/build/stats-html.html
CHANGED
|
@@ -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":"
|
|
4821
|
+
const data = {"version":2,"tree":{"name":"root","children":[{"name":"index.production.js","children":[{"name":"packages/history/src/index.ts","uid":"7ea9-1"}]}],"isRoot":true},"nodeParts":{"7ea9-1":{"renderedLength":9381,"gzipLength":2719,"brotliLength":0,"metaUid":"7ea9-0"}},"nodeMetas":{"7ea9-0":{"id":"/packages/history/src/index.ts","moduleParts":{"index.production.js":"7ea9-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;
|
package/build/stats-react.json
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
"children": [
|
|
9
9
|
{
|
|
10
10
|
"name": "packages/history/src/index.ts",
|
|
11
|
-
"uid": "
|
|
11
|
+
"uid": "7ea9-3"
|
|
12
12
|
}
|
|
13
13
|
]
|
|
14
14
|
}
|
|
@@ -16,18 +16,18 @@
|
|
|
16
16
|
"isRoot": true
|
|
17
17
|
},
|
|
18
18
|
"nodeParts": {
|
|
19
|
-
"
|
|
20
|
-
"renderedLength":
|
|
21
|
-
"gzipLength":
|
|
19
|
+
"7ea9-3": {
|
|
20
|
+
"renderedLength": 9381,
|
|
21
|
+
"gzipLength": 2719,
|
|
22
22
|
"brotliLength": 0,
|
|
23
|
-
"metaUid": "
|
|
23
|
+
"metaUid": "7ea9-2"
|
|
24
24
|
}
|
|
25
25
|
},
|
|
26
26
|
"nodeMetas": {
|
|
27
|
-
"
|
|
27
|
+
"7ea9-2": {
|
|
28
28
|
"id": "/packages/history/src/index.ts",
|
|
29
29
|
"moduleParts": {
|
|
30
|
-
"index.production.js": "
|
|
30
|
+
"index.production.js": "7ea9-3"
|
|
31
31
|
},
|
|
32
32
|
"imported": [],
|
|
33
33
|
"importedBy": [],
|
package/build/types/index.d.ts
CHANGED
|
@@ -17,7 +17,7 @@ interface RouterHistory {
|
|
|
17
17
|
back: () => void;
|
|
18
18
|
forward: () => void;
|
|
19
19
|
createHref: (href: string) => string;
|
|
20
|
-
block: (
|
|
20
|
+
block: (message: string) => () => void;
|
|
21
21
|
flush: () => void;
|
|
22
22
|
destroy: () => void;
|
|
23
23
|
notify: () => void;
|
|
@@ -34,7 +34,6 @@ interface ParsedPath {
|
|
|
34
34
|
interface HistoryState {
|
|
35
35
|
key: string;
|
|
36
36
|
}
|
|
37
|
-
type BlockerFn = (retry: () => void, cancel: () => void) => void;
|
|
38
37
|
/**
|
|
39
38
|
* Creates a history object that can be used to interact with the browser's
|
|
40
39
|
* navigation. This is a lightweight API wrapping the browser's native methods.
|
|
@@ -35,26 +35,20 @@
|
|
|
35
35
|
let location = opts.getLocation();
|
|
36
36
|
let subscribers = new Set();
|
|
37
37
|
let blockers = [];
|
|
38
|
-
let queue = [];
|
|
39
38
|
const onUpdate = () => {
|
|
40
39
|
location = opts.getLocation();
|
|
41
40
|
subscribers.forEach(subscriber => subscriber());
|
|
42
41
|
};
|
|
43
|
-
const
|
|
42
|
+
const tryNavigation = task => {
|
|
44
43
|
if (blockers.length) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
while (queue.length) {
|
|
52
|
-
queue.shift()?.();
|
|
44
|
+
for (let blocker of blockers) {
|
|
45
|
+
if (!window.confirm(blocker.message)) {
|
|
46
|
+
opts.onBlocked?.(onUpdate);
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
53
50
|
}
|
|
54
|
-
|
|
55
|
-
const queueTask = task => {
|
|
56
|
-
queue.push(task);
|
|
57
|
-
tryUnblock();
|
|
51
|
+
task();
|
|
58
52
|
};
|
|
59
53
|
return {
|
|
60
54
|
get location() {
|
|
@@ -68,41 +62,44 @@
|
|
|
68
62
|
},
|
|
69
63
|
push: (path, state) => {
|
|
70
64
|
state = assignKey(state);
|
|
71
|
-
|
|
65
|
+
tryNavigation(() => {
|
|
72
66
|
opts.pushState(path, state, onUpdate);
|
|
73
67
|
});
|
|
74
68
|
},
|
|
75
69
|
replace: (path, state) => {
|
|
76
70
|
state = assignKey(state);
|
|
77
|
-
|
|
71
|
+
tryNavigation(() => {
|
|
78
72
|
opts.replaceState(path, state, onUpdate);
|
|
79
73
|
});
|
|
80
74
|
},
|
|
81
75
|
go: index => {
|
|
82
|
-
|
|
76
|
+
tryNavigation(() => {
|
|
83
77
|
opts.go(index);
|
|
84
78
|
});
|
|
85
79
|
},
|
|
86
80
|
back: () => {
|
|
87
|
-
|
|
81
|
+
tryNavigation(() => {
|
|
88
82
|
opts.back();
|
|
89
83
|
});
|
|
90
84
|
},
|
|
91
85
|
forward: () => {
|
|
92
|
-
|
|
86
|
+
tryNavigation(() => {
|
|
93
87
|
opts.forward();
|
|
94
88
|
});
|
|
95
89
|
},
|
|
96
90
|
createHref: str => opts.createHref(str),
|
|
97
|
-
block:
|
|
98
|
-
|
|
91
|
+
block: message => {
|
|
92
|
+
const payload = {
|
|
93
|
+
message
|
|
94
|
+
};
|
|
95
|
+
blockers.push(payload);
|
|
99
96
|
if (blockers.length === 1) {
|
|
100
97
|
addEventListener(beforeUnloadEvent, beforeUnloadListener, {
|
|
101
98
|
capture: true
|
|
102
99
|
});
|
|
103
100
|
}
|
|
104
101
|
return () => {
|
|
105
|
-
blockers = blockers.filter(b => b !==
|
|
102
|
+
blockers = blockers.filter(b => b !== payload);
|
|
106
103
|
if (!blockers.length) {
|
|
107
104
|
stopBlocking();
|
|
108
105
|
}
|
|
@@ -143,6 +140,7 @@
|
|
|
143
140
|
const getHref = opts?.getHref ?? (() => `${window.location.pathname}${window.location.search}${window.location.hash}`);
|
|
144
141
|
const createHref = opts?.createHref ?? (path => path);
|
|
145
142
|
let currentLocation = parseLocation(getHref(), window.history.state);
|
|
143
|
+
let rollbackLocation;
|
|
146
144
|
const getLocation = () => currentLocation;
|
|
147
145
|
let next;
|
|
148
146
|
|
|
@@ -173,12 +171,16 @@
|
|
|
173
171
|
// Reset the nextIsPush flag and clear the scheduled update
|
|
174
172
|
next = undefined;
|
|
175
173
|
scheduled = undefined;
|
|
174
|
+
rollbackLocation = undefined;
|
|
176
175
|
});
|
|
177
176
|
};
|
|
178
177
|
|
|
179
178
|
// This function queues up a call to update the browser history
|
|
180
179
|
const queueHistoryAction = (type, path, state, onUpdate) => {
|
|
181
180
|
const href = createHref(path);
|
|
181
|
+
if (!scheduled) {
|
|
182
|
+
rollbackLocation = currentLocation;
|
|
183
|
+
}
|
|
182
184
|
|
|
183
185
|
// Update the location in memory
|
|
184
186
|
currentLocation = parseLocation(href, state);
|
|
@@ -189,6 +191,7 @@
|
|
|
189
191
|
state,
|
|
190
192
|
isPush: next?.isPush || type === 'push'
|
|
191
193
|
};
|
|
194
|
+
|
|
192
195
|
// Notify subscribers
|
|
193
196
|
onUpdate();
|
|
194
197
|
if (!scheduled) {
|
|
@@ -216,6 +219,15 @@
|
|
|
216
219
|
window.history.replaceState = originalReplaceState;
|
|
217
220
|
window.removeEventListener(pushStateEvent, onPushPop);
|
|
218
221
|
window.removeEventListener(popStateEvent, onPushPop);
|
|
222
|
+
},
|
|
223
|
+
onBlocked: onUpdate => {
|
|
224
|
+
// If a navigation is blocked, we need to rollback the location
|
|
225
|
+
// that we optimistically updated in memory.
|
|
226
|
+
if (rollbackLocation && currentLocation !== rollbackLocation) {
|
|
227
|
+
currentLocation = rollbackLocation;
|
|
228
|
+
// Notify subscribers
|
|
229
|
+
onUpdate();
|
|
230
|
+
}
|
|
219
231
|
}
|
|
220
232
|
});
|
|
221
233
|
window.addEventListener(pushStateEvent, onPushPop);
|
|
@@ -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: (blockerFn: 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 BlockerFn = (retry: () => void, cancel: () => void) => void\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\nfunction 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}): RouterHistory {\n let location = opts.getLocation()\n let subscribers = new Set<() => void>()\n let blockers: BlockerFn[] = []\n let queue: (() => void)[] = []\n\n const onUpdate = () => {\n location = opts.getLocation()\n subscribers.forEach((subscriber) => subscriber())\n }\n\n const tryUnblock = () => {\n if (blockers.length) {\n blockers[0]?.(tryUnblock, () => {\n blockers = []\n stopBlocking()\n })\n return\n }\n\n while (queue.length) {\n queue.shift()?.()\n }\n }\n\n const queueTask = (task: () => void) => {\n queue.push(task)\n tryUnblock()\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 queueTask(() => {\n opts.pushState(path, state, onUpdate)\n })\n },\n replace: (path: string, state: any) => {\n state = assignKey(state)\n queueTask(() => {\n opts.replaceState(path, state, onUpdate)\n })\n },\n go: (index) => {\n queueTask(() => {\n opts.go(index)\n })\n },\n back: () => {\n queueTask(() => {\n opts.back()\n })\n },\n forward: () => {\n queueTask(() => {\n opts.forward()\n })\n },\n createHref: (str) => opts.createHref(str),\n block: (cb) => {\n blockers.push(cb)\n\n if (blockers.length === 1) {\n addEventListener(beforeUnloadEvent, beforeUnloadListener, {\n capture: true,\n })\n }\n\n return () => {\n blockers = blockers.filter((b) => b !== cb)\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 getHref?: () => string\n createHref?: (path: string) => string\n}): RouterHistory {\n const getHref =\n opts?.getHref ??\n (() =>\n `${window.location.pathname}${window.location.search}${window.location.hash}`)\n\n const createHref = opts?.createHref ?? ((path) => path)\n\n let currentLocation = parseLocation(getHref(), window.history.state)\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 window.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 })\n }\n\n // This function queues up a call to update the browser history\n const queueHistoryAction = (\n type: 'push' | 'replace',\n path: string,\n state: any,\n onUpdate: () => void,\n ) => {\n const href = createHref(path)\n\n // Update the location in memory\n currentLocation = parseLocation(href, 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 // 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(getHref(), window.history.state)\n history.notify()\n }\n\n var originalPushState = window.history.pushState\n var originalReplaceState = window.history.replaceState\n\n const history = createHistory({\n getLocation,\n pushState: (path, state, onUpdate) =>\n queueHistoryAction('push', path, state, onUpdate),\n replaceState: (path, state, onUpdate) =>\n queueHistoryAction('replace', path, state, onUpdate),\n back: () => window.history.back(),\n forward: () => window.history.forward(),\n go: (n) => window.history.go(n),\n createHref: (path) => createHref(path),\n flush,\n destroy: () => {\n window.history.pushState = originalPushState\n window.history.replaceState = originalReplaceState\n window.removeEventListener(pushStateEvent, onPushPop)\n window.removeEventListener(popStateEvent, onPushPop)\n },\n })\n\n window.addEventListener(pushStateEvent, onPushPop)\n window.addEventListener(popStateEvent, onPushPop)\n\n window.history.pushState = function () {\n let res = originalPushState.apply(window.history, arguments as any)\n if (tracking) history.notify()\n return res\n }\n\n window.history.replaceState = function () {\n let res = originalReplaceState.apply(window.history, arguments as any)\n if (tracking) history.notify()\n return res\n }\n\n return history\n}\n\nexport function createHashHistory(): RouterHistory {\n return createBrowserHistory({\n getHref: () => window.location.hash.substring(1),\n createHref: (path) => `#${path}`,\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 = () => parseLocation(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) => window.history.go(n),\n createHref: (path) => path,\n })\n}\n\nfunction parseLocation(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","queue","onUpdate","forEach","subscriber","tryUnblock","length","shift","queueTask","task","push","subscribe","cb","add","delete","path","state","assignKey","pushState","replace","replaceState","go","index","back","forward","createHref","str","block","addEventListener","filter","b","flush","destroy","notify","key","createRandomKey","createBrowserHistory","getHref","window","pathname","search","hash","currentLocation","parseLocation","history","next","tracking","scheduled","untrack","fn","isPush","href","undefined","queueHistoryAction","type","Promise","resolve","then","onPushPop","originalPushState","originalReplaceState","n","res","apply","arguments","createHashHistory","substring","createMemoryHistory","initialEntries","entries","initialIndex","currentState","Math","min","hashIndex","indexOf","searchIndex","slice","random","toString"],"mappings":";;;;;;;;;;;;;;;;EAAA;EACA;EACA;;EAkCA,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,cAA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e,EAAE,EAAGwC,CAAC,IAAKvB,MAAM,CAACM,OAAO,CAACvB,EAAE,CAACwC,CAAC,CAAC;MAC/BpC,UAAU,EAAGV,IAAI,IAAKA,IAAAA;EACxB,GAAC,CAAC,CAAA;EACJ,CAAA;EAEA,SAAS4B,aAAaA,CAACQ,IAAY,EAAEnC,KAAmB,EAAmB;EACzE,EAAA,IAAI0D,SAAS,GAAGvB,IAAI,CAACwB,OAAO,CAAC,GAAG,CAAC,CAAA;EACjC,EAAA,IAAIC,WAAW,GAAGzB,IAAI,CAACwB,OAAO,CAAC,GAAG,CAAC,CAAA;IAEnC,OAAO;MACLxB,IAAI;EACJZ,IAAAA,QAAQ,EAAEY,IAAI,CAACe,SAAS,CACtB,CAAC,EACDQ,SAAS,GAAG,CAAC,GACTE,WAAW,GAAG,CAAC,GACbJ,IAAI,CAACC,GAAG,CAACC,SAAS,EAAEE,WAAW,CAAC,GAChCF,SAAS,GACXE,WAAW,GAAG,CAAC,GACbA,WAAW,GACXzB,IAAI,CAAC7C,MACb,CAAC;EACDmC,IAAAA,IAAI,EAAEiC,SAAS,GAAG,CAAC,CAAC,GAAGvB,IAAI,CAACe,SAAS,CAACQ,SAAS,CAAC,GAAG,EAAE;MACrDlC,MAAM,EACJoC,WAAW,GAAG,CAAC,CAAC,GACZzB,IAAI,CAAC0B,KAAK,CAACD,WAAW,EAAEF,SAAS,KAAK,CAAC,CAAC,GAAGtB,SAAS,GAAGsB,SAAS,CAAC,GACjE,EAAE;MACR1D,KAAK,EAAEA,KAAK,IAAI,EAAC;KAClB,CAAA;EACH,CAAA;;EAEA;EACA,SAASmB,eAAeA,GAAG;EACzB,EAAA,OAAO,CAACqC,IAAI,CAACM,MAAM,EAAE,GAAG,CAAC,EAAEC,QAAQ,CAAC,EAAE,CAAC,CAACb,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: (message: string) => () => 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 Blocker = {\n message: string\n}\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\nfunction 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: Blocker[] = []\n\n const onUpdate = () => {\n location = opts.getLocation()\n subscribers.forEach((subscriber) => subscriber())\n }\n\n const tryNavigation = (task: () => void) => {\n if (blockers.length) {\n for (let blocker of blockers) {\n if (!window.confirm(blocker.message)) {\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: (message) => {\n const payload: Blocker = {\n message,\n }\n\n blockers.push(payload)\n\n if (blockers.length === 1) {\n addEventListener(beforeUnloadEvent, beforeUnloadListener, {\n capture: true,\n })\n }\n\n return () => {\n blockers = blockers.filter((b) => b !== payload)\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 getHref?: () => string\n createHref?: (path: string) => string\n}): RouterHistory {\n const getHref =\n opts?.getHref ??\n (() =>\n `${window.location.pathname}${window.location.search}${window.location.hash}`)\n\n const createHref = opts?.createHref ?? ((path) => path)\n\n let currentLocation = parseLocation(getHref(), window.history.state)\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 window.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 path: string,\n state: any,\n onUpdate: () => void,\n ) => {\n const href = createHref(path)\n\n if (!scheduled) {\n rollbackLocation = currentLocation\n }\n\n // Update the location in memory\n currentLocation = parseLocation(href, 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(getHref(), window.history.state)\n history.notify()\n }\n\n var originalPushState = window.history.pushState\n var originalReplaceState = window.history.replaceState\n\n const history = createHistory({\n getLocation,\n pushState: (path, state, onUpdate) =>\n queueHistoryAction('push', path, state, onUpdate),\n replaceState: (path, state, onUpdate) =>\n queueHistoryAction('replace', path, state, onUpdate),\n back: () => window.history.back(),\n forward: () => window.history.forward(),\n go: (n) => window.history.go(n),\n createHref: (path) => createHref(path),\n flush,\n destroy: () => {\n window.history.pushState = originalPushState\n window.history.replaceState = originalReplaceState\n window.removeEventListener(pushStateEvent, onPushPop)\n window.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 window.addEventListener(pushStateEvent, onPushPop)\n window.addEventListener(popStateEvent, onPushPop)\n\n window.history.pushState = function () {\n let res = originalPushState.apply(window.history, arguments as any)\n if (tracking) history.notify()\n return res\n }\n\n window.history.replaceState = function () {\n let res = originalReplaceState.apply(window.history, arguments as any)\n if (tracking) history.notify()\n return res\n }\n\n return history\n}\n\nexport function createHashHistory(): RouterHistory {\n return createBrowserHistory({\n getHref: () => window.location.hash.substring(1),\n createHref: (path) => `#${path}`,\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 = () => parseLocation(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) => window.history.go(n),\n createHref: (path) => path,\n })\n}\n\nfunction parseLocation(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","length","blocker","window","confirm","message","onBlocked","subscribe","cb","add","delete","push","path","state","assignKey","pushState","replace","replaceState","go","index","back","forward","createHref","str","block","payload","addEventListener","filter","b","flush","destroy","notify","key","createRandomKey","createBrowserHistory","getHref","pathname","search","hash","currentLocation","parseLocation","history","rollbackLocation","next","tracking","scheduled","untrack","fn","isPush","href","undefined","queueHistoryAction","type","Promise","resolve","then","onPushPop","originalPushState","originalReplaceState","n","res","apply","arguments","createHashHistory","substring","createMemoryHistory","initialEntries","entries","initialIndex","currentState","Math","min","hashIndex","indexOf","searchIndex","slice","random","toString"],"mappings":";;;;;;;;;;;;;;;;EAAA;EACA;EACA;;EAoCA,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;EAED,SAASC,aAAaA,CAACC,IAWtB,EAAiB;EAChB,EAAA,IAAIC,QAAQ,GAAGD,IAAI,CAACE,WAAW,EAAE,CAAA;EACjC,EAAA,IAA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AC/BrC,UAAU,EAAGV,IAAI,IAAKA,IAAAA;EACxB,GAAC,CAAC,CAAA;EACJ,CAAA;EAEA,SAAS4B,aAAaA,CAACS,IAAY,EAAEpC,KAAmB,EAAmB;EACzE,EAAA,IAAI2D,SAAS,GAAGvB,IAAI,CAACwB,OAAO,CAAC,GAAG,CAAC,CAAA;EACjC,EAAA,IAAIC,WAAW,GAAGzB,IAAI,CAACwB,OAAO,CAAC,GAAG,CAAC,CAAA;IAEnC,OAAO;MACLxB,IAAI;EACJb,IAAAA,QAAQ,EAAEa,IAAI,CAACe,SAAS,CACtB,CAAC,EACDQ,SAAS,GAAG,CAAC,GACTE,WAAW,GAAG,CAAC,GACbJ,IAAI,CAACC,GAAG,CAACC,SAAS,EAAEE,WAAW,CAAC,GAChCF,SAAS,GACXE,WAAW,GAAG,CAAC,GACbA,WAAW,GACXzB,IAAI,CAAChD,MACb,CAAC;EACDqC,IAAAA,IAAI,EAAEkC,SAAS,GAAG,CAAC,CAAC,GAAGvB,IAAI,CAACe,SAAS,CAACQ,SAAS,CAAC,GAAG,EAAE;MACrDnC,MAAM,EACJqC,WAAW,GAAG,CAAC,CAAC,GACZzB,IAAI,CAAC0B,KAAK,CAACD,WAAW,EAAEF,SAAS,KAAK,CAAC,CAAC,GAAGtB,SAAS,GAAGsB,SAAS,CAAC,GACjE,EAAE;MACR3D,KAAK,EAAEA,KAAK,IAAI,EAAC;KAClB,CAAA;EACH,CAAA;;EAEA;EACA,SAASoB,eAAeA,GAAG;EACzB,EAAA,OAAO,CAACqC,IAAI,CAACM,MAAM,EAAE,GAAG,CAAC,EAAEC,QAAQ,CAAC,EAAE,CAAC,CAACb,SAAS,CAAC,CAAC,CAAC,CAAA;EACtD;;;;;;;;;;"}
|
|
@@ -8,5 +8,5 @@
|
|
|
8
8
|
*
|
|
9
9
|
* @license MIT
|
|
10
10
|
*/
|
|
11
|
-
!function(t
|
|
11
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).TanStackHistory={})}(this,(function(e){"use strict";const t="pushstate",o="popstate",n="beforeunload",r=e=>(e.preventDefault(),e.returnValue=""),i=()=>{removeEventListener(n,r,{capture:!0})};function s(e){let t=e.getLocation(),o=new Set,s=[];const c=()=>{t=e.getLocation(),o.forEach((e=>e()))},d=t=>{if(s.length)for(let t of s)if(!window.confirm(t.message))return void e.onBlocked?.(c);t()};return{get location(){return t},subscribe:e=>(o.add(e),()=>{o.delete(e)}),push:(t,o)=>{o=a(o),d((()=>{e.pushState(t,o,c)}))},replace:(t,o)=>{o=a(o),d((()=>{e.replaceState(t,o,c)}))},go:t=>{d((()=>{e.go(t)}))},back:()=>{d((()=>{e.back()}))},forward:()=>{d((()=>{e.forward()}))},createHref:t=>e.createHref(t),block:e=>{const t={message:e};return s.push(t),1===s.length&&addEventListener(n,r,{capture:!0}),()=>{s=s.filter((e=>e!==t)),s.length||i()}},flush:()=>e.flush?.(),destroy:()=>e.destroy?.(),notify:c}}function a(e){return e||(e={}),{...e,key:h()}}function c(e){const n=e?.getHref??(()=>`${window.location.pathname}${window.location.search}${window.location.hash}`),r=e?.createHref??(e=>e);let i,a=d(n(),window.history.state);let c,h,u=!0;const f=()=>{u=!1,(()=>{c&&(window.history[c.isPush?"pushState":"replaceState"](c.state,"",c.href),c=void 0,h=void 0,i=void 0)})(),u=!0},w=(e,t,o,n)=>{const s=r(t);h||(i=a),a=d(s,o),c={href:s,state:o,isPush:c?.isPush||"push"===e},n(),h||(h=Promise.resolve().then((()=>f())))},l=()=>{a=d(n(),window.history.state),g.notify()};var p=window.history.pushState,y=window.history.replaceState;const g=s({getLocation:()=>a,pushState:(e,t,o)=>w("push",e,t,o),replaceState:(e,t,o)=>w("replace",e,t,o),back:()=>window.history.back(),forward:()=>window.history.forward(),go:e=>window.history.go(e),createHref:e=>r(e),flush:f,destroy:()=>{window.history.pushState=p,window.history.replaceState=y,window.removeEventListener(t,l),window.removeEventListener(o,l)},onBlocked:e=>{i&&a!==i&&(a=i,e())}});return window.addEventListener(t,l),window.addEventListener(o,l),window.history.pushState=function(){let e=p.apply(window.history,arguments);return u&&g.notify(),e},window.history.replaceState=function(){let e=y.apply(window.history,arguments);return u&&g.notify(),e},g}function d(e,t){let o=e.indexOf("#"),n=e.indexOf("?");return{href:e,pathname:e.substring(0,o>0?n>0?Math.min(o,n):o:n>0?n:e.length),hash:o>-1?e.substring(o):"",search:n>-1?e.slice(n,-1===o?void 0:o):"",state:t||{}}}function h(){return(Math.random()+1).toString(36).substring(7)}e.createBrowserHistory=c,e.createHashHistory=function(){return c({getHref:()=>window.location.hash.substring(1),createHref:e=>`#${e}`})},e.createMemoryHistory=function(e={initialEntries:["/"]}){const t=e.initialEntries;let o=e.initialIndex??t.length-1,n={key:h()};return s({getLocation:()=>d(t[o],n),pushState:(e,r)=>{n=r,t.push(e),o++},replaceState:(e,r)=>{n=r,t[o]=e},back:()=>{o--},forward:()=>{o=Math.min(o+1,t.length-1)},go:e=>window.history.go(e),createHref:e=>e})}}));
|
|
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: (blockerFn: 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 BlockerFn = (retry: () => void, cancel: () => void) => void\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\nfunction 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}): RouterHistory {\n let location = opts.getLocation()\n let subscribers = new Set<() => void>()\n let blockers: BlockerFn[] = []\n let queue: (() => void)[] = []\n\n const onUpdate = () => {\n location = opts.getLocation()\n subscribers.forEach((subscriber) => subscriber())\n }\n\n const tryUnblock = () => {\n if (blockers.length) {\n blockers[0]?.(tryUnblock, () => {\n blockers = []\n stopBlocking()\n })\n return\n }\n\n while (queue.length) {\n queue.shift()?.()\n }\n }\n\n const queueTask = (task: () => void) => {\n queue.push(task)\n tryUnblock()\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 queueTask(() => {\n opts.pushState(path, state, onUpdate)\n })\n },\n replace: (path: string, state: any) => {\n state = assignKey(state)\n queueTask(() => {\n opts.replaceState(path, state, onUpdate)\n })\n },\n go: (index) => {\n queueTask(() => {\n opts.go(index)\n })\n },\n back: () => {\n queueTask(() => {\n opts.back()\n })\n },\n forward: () => {\n queueTask(() => {\n opts.forward()\n })\n },\n createHref: (str) => opts.createHref(str),\n block: (cb) => {\n blockers.push(cb)\n\n if (blockers.length === 1) {\n addEventListener(beforeUnloadEvent, beforeUnloadListener, {\n capture: true,\n })\n }\n\n return () => {\n blockers = blockers.filter((b) => b !== cb)\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 getHref?: () => string\n createHref?: (path: string) => string\n}): RouterHistory {\n const getHref =\n opts?.getHref ??\n (() =>\n `${window.location.pathname}${window.location.search}${window.location.hash}`)\n\n const createHref = opts?.createHref ?? ((path) => path)\n\n let currentLocation = parseLocation(getHref(), window.history.state)\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 window.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 })\n }\n\n // This function queues up a call to update the browser history\n const queueHistoryAction = (\n type: 'push' | 'replace',\n path: string,\n state: any,\n onUpdate: () => void,\n ) => {\n const href = createHref(path)\n\n // Update the location in memory\n currentLocation = parseLocation(href, 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 // 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(getHref(), window.history.state)\n history.notify()\n }\n\n var originalPushState = window.history.pushState\n var originalReplaceState = window.history.replaceState\n\n const history = createHistory({\n getLocation,\n pushState: (path, state, onUpdate) =>\n queueHistoryAction('push', path, state, onUpdate),\n replaceState: (path, state, onUpdate) =>\n queueHistoryAction('replace', path, state, onUpdate),\n back: () => window.history.back(),\n forward: () => window.history.forward(),\n go: (n) => window.history.go(n),\n createHref: (path) => createHref(path),\n flush,\n destroy: () => {\n window.history.pushState = originalPushState\n window.history.replaceState = originalReplaceState\n window.removeEventListener(pushStateEvent, onPushPop)\n window.removeEventListener(popStateEvent, onPushPop)\n },\n })\n\n window.addEventListener(pushStateEvent, onPushPop)\n window.addEventListener(popStateEvent, onPushPop)\n\n window.history.pushState = function () {\n let res = originalPushState.apply(window.history, arguments as any)\n if (tracking) history.notify()\n return res\n }\n\n window.history.replaceState = function () {\n let res = originalReplaceState.apply(window.history, arguments as any)\n if (tracking) history.notify()\n return res\n }\n\n return history\n}\n\nexport function createHashHistory(): RouterHistory {\n return createBrowserHistory({\n getHref: () => window.location.hash.substring(1),\n createHref: (path) => `#${path}`,\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 = () => parseLocation(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) => window.history.go(n),\n createHref: (path) => path,\n })\n}\n\nfunction parseLocation(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","queue","onUpdate","forEach","subscriber","tryUnblock","length","shift","queueTask","task","push","subscribe","cb","add","delete","path","state","assignKey","pushState","replace","replaceState","go","index","back","forward","createHref","str","block","addEventListener","filter","b","flush","destroy","notify","key","createRandomKey","createBrowserHistory","getHref","window","pathname","search","hash","currentLocation","parseLocation","history","next","scheduled","tracking","isPush","href","undefined","fn","queueHistoryAction","type","Promise","resolve","then","onPushPop","originalPushState","originalReplaceState","n","res","apply","arguments","hashIndex","indexOf","searchIndex","substring","Math","min","slice","random","toString","initialEntries","entries","initialIndex","currentState"],"mappings":";;;;;;;;;;uPAoCA,MAAMA,EAAiB,YACjBC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,wBAEO,SACLpB,EAGI,CACF8E,eAAgB,CAAC,OAGnB,MAAMC,EAAU/E,EAAK8E,eACrB,IAAInD,EAAQ3B,EAAKgF,cAAgBD,EAAQpE,OAAS,EAC9CsE,EAAe,CACjB1C,IAAKC,KAKP,OAAOzC,EAAc,CACnBG,YAHkBA,IAAM8C,EAAc+B,EAAQpD,GAASsD,GAIvD1D,UAAWA,CAACH,EAAMC,KAChB4D,EAAe5D,EACf0D,EAAQhE,KAAKK,GACbO,GAAO,EAETF,aAAcA,CAACL,EAAMC,KACnB4D,EAAe5D,EACf0D,EAAQpD,GAASP,CAAI,EAEvBQ,KAAMA,KACJD,GAAO,EAETE,QAASA,KACPF,EAAQ8C,KAAKC,IAAI/C,EAAQ,EAAGoD,EAAQpE,OAAS,EAAE,EAEjDe,GAAKuC,GAAMtB,OAAOM,QAAQvB,GAAGuC,GAC7BnC,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: (message: string) => () => 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 Blocker = {\n message: string\n}\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\nfunction 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: Blocker[] = []\n\n const onUpdate = () => {\n location = opts.getLocation()\n subscribers.forEach((subscriber) => subscriber())\n }\n\n const tryNavigation = (task: () => void) => {\n if (blockers.length) {\n for (let blocker of blockers) {\n if (!window.confirm(blocker.message)) {\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: (message) => {\n const payload: Blocker = {\n message,\n }\n\n blockers.push(payload)\n\n if (blockers.length === 1) {\n addEventListener(beforeUnloadEvent, beforeUnloadListener, {\n capture: true,\n })\n }\n\n return () => {\n blockers = blockers.filter((b) => b !== payload)\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 getHref?: () => string\n createHref?: (path: string) => string\n}): RouterHistory {\n const getHref =\n opts?.getHref ??\n (() =>\n `${window.location.pathname}${window.location.search}${window.location.hash}`)\n\n const createHref = opts?.createHref ?? ((path) => path)\n\n let currentLocation = parseLocation(getHref(), window.history.state)\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 window.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 path: string,\n state: any,\n onUpdate: () => void,\n ) => {\n const href = createHref(path)\n\n if (!scheduled) {\n rollbackLocation = currentLocation\n }\n\n // Update the location in memory\n currentLocation = parseLocation(href, 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(getHref(), window.history.state)\n history.notify()\n }\n\n var originalPushState = window.history.pushState\n var originalReplaceState = window.history.replaceState\n\n const history = createHistory({\n getLocation,\n pushState: (path, state, onUpdate) =>\n queueHistoryAction('push', path, state, onUpdate),\n replaceState: (path, state, onUpdate) =>\n queueHistoryAction('replace', path, state, onUpdate),\n back: () => window.history.back(),\n forward: () => window.history.forward(),\n go: (n) => window.history.go(n),\n createHref: (path) => createHref(path),\n flush,\n destroy: () => {\n window.history.pushState = originalPushState\n window.history.replaceState = originalReplaceState\n window.removeEventListener(pushStateEvent, onPushPop)\n window.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 window.addEventListener(pushStateEvent, onPushPop)\n window.addEventListener(popStateEvent, onPushPop)\n\n window.history.pushState = function () {\n let res = originalPushState.apply(window.history, arguments as any)\n if (tracking) history.notify()\n return res\n }\n\n window.history.replaceState = function () {\n let res = originalReplaceState.apply(window.history, arguments as any)\n if (tracking) history.notify()\n return res\n }\n\n return history\n}\n\nexport function createHashHistory(): RouterHistory {\n return createBrowserHistory({\n getHref: () => window.location.hash.substring(1),\n createHref: (path) => `#${path}`,\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 = () => parseLocation(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) => window.history.go(n),\n createHref: (path) => path,\n })\n}\n\nfunction parseLocation(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","length","blocker","window","confirm","message","onBlocked","subscribe","cb","add","delete","push","path","state","assignKey","pushState","replace","replaceState","go","index","back","forward","createHref","str","block","payload","addEventListener","filter","b","flush","destroy","notify","key","createRandomKey","createBrowserHistory","getHref","pathname","search","hash","rollbackLocation","currentLocation","parseLocation","history","next","scheduled","tracking","isPush","href","undefined","fn","queueHistoryAction","type","Promise","resolve","then","onPushPop","originalPushState","originalReplaceState","n","res","apply","arguments","hashIndex","indexOf","searchIndex","substring","Math","min","slice","random","toString","initialEntries","entries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CAAC,EAErB,CAGA,SAASoB,IACP,OAAQiC,KAAKG,SAAW,GAAGC,SAAS,IAAIL,UAAU,EACpD,8CAzEO,WACL,OAAO/B,EAAqB,CAC1BC,QAASA,IAAMhC,OAAOZ,SAAS+C,KAAK2B,UAAU,GAC9C3C,WAAaV,GAAU,IAAGA,KAE9B,wBAEO,SACLtB,EAGI,CACFiF,eAAgB,CAAC,OAGnB,MAAMC,EAAUlF,EAAKiF,eACrB,IAAIpD,EAAQ7B,EAAKmF,cAAgBD,EAAQvE,OAAS,EAC9CyE,EAAe,CACjB1C,IAAKC,KAKP,OAAO5C,EAAc,CACnBG,YAHkBA,IAAMiD,EAAc+B,EAAQrD,GAASuD,GAIvD3D,UAAWA,CAACH,EAAMC,KAChB6D,EAAe7D,EACf2D,EAAQ7D,KAAKC,GACbO,GAAO,EAETF,aAAcA,CAACL,EAAMC,KACnB6D,EAAe7D,EACf2D,EAAQrD,GAASP,CAAI,EAEvBQ,KAAMA,KACJD,GAAO,EAETE,QAASA,KACPF,EAAQ+C,KAAKC,IAAIhD,EAAQ,EAAGqD,EAAQvE,OAAS,EAAE,EAEjDiB,GAAKwC,GAAMvD,OAAOuC,QAAQxB,GAAGwC,GAC7BpC,WAAaV,GAASA,GAE1B"}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -11,7 +11,7 @@ export interface RouterHistory {
|
|
|
11
11
|
back: () => void
|
|
12
12
|
forward: () => void
|
|
13
13
|
createHref: (href: string) => string
|
|
14
|
-
block: (
|
|
14
|
+
block: (message: string) => () => void
|
|
15
15
|
flush: () => void
|
|
16
16
|
destroy: () => void
|
|
17
17
|
notify: () => void
|
|
@@ -32,7 +32,9 @@ export interface HistoryState {
|
|
|
32
32
|
key: string
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
-
type
|
|
35
|
+
type Blocker = {
|
|
36
|
+
message: string
|
|
37
|
+
}
|
|
36
38
|
|
|
37
39
|
const pushStateEvent = 'pushstate'
|
|
38
40
|
const popStateEvent = 'popstate'
|
|
@@ -60,34 +62,28 @@ function createHistory(opts: {
|
|
|
60
62
|
createHref: (path: string) => string
|
|
61
63
|
flush?: () => void
|
|
62
64
|
destroy?: () => void
|
|
65
|
+
onBlocked?: (onUpdate: () => void) => void
|
|
63
66
|
}): RouterHistory {
|
|
64
67
|
let location = opts.getLocation()
|
|
65
68
|
let subscribers = new Set<() => void>()
|
|
66
|
-
let blockers:
|
|
67
|
-
let queue: (() => void)[] = []
|
|
69
|
+
let blockers: Blocker[] = []
|
|
68
70
|
|
|
69
71
|
const onUpdate = () => {
|
|
70
72
|
location = opts.getLocation()
|
|
71
73
|
subscribers.forEach((subscriber) => subscriber())
|
|
72
74
|
}
|
|
73
75
|
|
|
74
|
-
const
|
|
76
|
+
const tryNavigation = (task: () => void) => {
|
|
75
77
|
if (blockers.length) {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
while (queue.length) {
|
|
84
|
-
queue.shift()?.()
|
|
78
|
+
for (let blocker of blockers) {
|
|
79
|
+
if (!window.confirm(blocker.message)) {
|
|
80
|
+
opts.onBlocked?.(onUpdate)
|
|
81
|
+
return
|
|
82
|
+
}
|
|
83
|
+
}
|
|
85
84
|
}
|
|
86
|
-
}
|
|
87
85
|
|
|
88
|
-
|
|
89
|
-
queue.push(task)
|
|
90
|
-
tryUnblock()
|
|
86
|
+
task()
|
|
91
87
|
}
|
|
92
88
|
|
|
93
89
|
return {
|
|
@@ -103,34 +99,38 @@ function createHistory(opts: {
|
|
|
103
99
|
},
|
|
104
100
|
push: (path: string, state: any) => {
|
|
105
101
|
state = assignKey(state)
|
|
106
|
-
|
|
102
|
+
tryNavigation(() => {
|
|
107
103
|
opts.pushState(path, state, onUpdate)
|
|
108
104
|
})
|
|
109
105
|
},
|
|
110
106
|
replace: (path: string, state: any) => {
|
|
111
107
|
state = assignKey(state)
|
|
112
|
-
|
|
108
|
+
tryNavigation(() => {
|
|
113
109
|
opts.replaceState(path, state, onUpdate)
|
|
114
110
|
})
|
|
115
111
|
},
|
|
116
112
|
go: (index) => {
|
|
117
|
-
|
|
113
|
+
tryNavigation(() => {
|
|
118
114
|
opts.go(index)
|
|
119
115
|
})
|
|
120
116
|
},
|
|
121
117
|
back: () => {
|
|
122
|
-
|
|
118
|
+
tryNavigation(() => {
|
|
123
119
|
opts.back()
|
|
124
120
|
})
|
|
125
121
|
},
|
|
126
122
|
forward: () => {
|
|
127
|
-
|
|
123
|
+
tryNavigation(() => {
|
|
128
124
|
opts.forward()
|
|
129
125
|
})
|
|
130
126
|
},
|
|
131
127
|
createHref: (str) => opts.createHref(str),
|
|
132
|
-
block: (
|
|
133
|
-
|
|
128
|
+
block: (message) => {
|
|
129
|
+
const payload: Blocker = {
|
|
130
|
+
message,
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
blockers.push(payload)
|
|
134
134
|
|
|
135
135
|
if (blockers.length === 1) {
|
|
136
136
|
addEventListener(beforeUnloadEvent, beforeUnloadListener, {
|
|
@@ -139,7 +139,7 @@ function createHistory(opts: {
|
|
|
139
139
|
}
|
|
140
140
|
|
|
141
141
|
return () => {
|
|
142
|
-
blockers = blockers.filter((b) => b !==
|
|
142
|
+
blockers = blockers.filter((b) => b !== payload)
|
|
143
143
|
|
|
144
144
|
if (!blockers.length) {
|
|
145
145
|
stopBlocking()
|
|
@@ -190,6 +190,7 @@ export function createBrowserHistory(opts?: {
|
|
|
190
190
|
const createHref = opts?.createHref ?? ((path) => path)
|
|
191
191
|
|
|
192
192
|
let currentLocation = parseLocation(getHref(), window.history.state)
|
|
193
|
+
let rollbackLocation: HistoryLocation | undefined
|
|
193
194
|
|
|
194
195
|
const getLocation = () => currentLocation
|
|
195
196
|
|
|
@@ -235,6 +236,7 @@ export function createBrowserHistory(opts?: {
|
|
|
235
236
|
// Reset the nextIsPush flag and clear the scheduled update
|
|
236
237
|
next = undefined
|
|
237
238
|
scheduled = undefined
|
|
239
|
+
rollbackLocation = undefined
|
|
238
240
|
})
|
|
239
241
|
}
|
|
240
242
|
|
|
@@ -247,6 +249,10 @@ export function createBrowserHistory(opts?: {
|
|
|
247
249
|
) => {
|
|
248
250
|
const href = createHref(path)
|
|
249
251
|
|
|
252
|
+
if (!scheduled) {
|
|
253
|
+
rollbackLocation = currentLocation
|
|
254
|
+
}
|
|
255
|
+
|
|
250
256
|
// Update the location in memory
|
|
251
257
|
currentLocation = parseLocation(href, state)
|
|
252
258
|
|
|
@@ -256,6 +262,7 @@ export function createBrowserHistory(opts?: {
|
|
|
256
262
|
state,
|
|
257
263
|
isPush: next?.isPush || type === 'push',
|
|
258
264
|
}
|
|
265
|
+
|
|
259
266
|
// Notify subscribers
|
|
260
267
|
onUpdate()
|
|
261
268
|
|
|
@@ -290,6 +297,15 @@ export function createBrowserHistory(opts?: {
|
|
|
290
297
|
window.removeEventListener(pushStateEvent, onPushPop)
|
|
291
298
|
window.removeEventListener(popStateEvent, onPushPop)
|
|
292
299
|
},
|
|
300
|
+
onBlocked: (onUpdate) => {
|
|
301
|
+
// If a navigation is blocked, we need to rollback the location
|
|
302
|
+
// that we optimistically updated in memory.
|
|
303
|
+
if (rollbackLocation && currentLocation !== rollbackLocation) {
|
|
304
|
+
currentLocation = rollbackLocation
|
|
305
|
+
// Notify subscribers
|
|
306
|
+
onUpdate()
|
|
307
|
+
}
|
|
308
|
+
},
|
|
293
309
|
})
|
|
294
310
|
|
|
295
311
|
window.addEventListener(pushStateEvent, onPushPop)
|