@tanstack/history 0.0.1-beta.247 → 0.0.1-beta.249
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 +31 -24
- package/build/cjs/index.js.map +1 -1
- package/build/esm/index.js +31 -25
- 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 +17 -2
- package/build/umd/index.development.js +31 -24
- 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 +36 -25
package/build/cjs/index.js
CHANGED
|
@@ -36,7 +36,7 @@ function createHistory(opts) {
|
|
|
36
36
|
subscribers.forEach(subscriber => subscriber());
|
|
37
37
|
};
|
|
38
38
|
const tryNavigation = task => {
|
|
39
|
-
if (blockers.length) {
|
|
39
|
+
if (typeof document !== 'undefined' && blockers.length) {
|
|
40
40
|
for (let blocker of blockers) {
|
|
41
41
|
if (!window.confirm(blocker.message)) {
|
|
42
42
|
opts.onBlocked?.(onUpdate);
|
|
@@ -133,9 +133,10 @@ function assignKey(state) {
|
|
|
133
133
|
* @returns A history instance
|
|
134
134
|
*/
|
|
135
135
|
function createBrowserHistory(opts) {
|
|
136
|
-
const
|
|
136
|
+
const win = opts?.window ?? (typeof document !== 'undefined' ? window : undefined);
|
|
137
|
+
const getHref = opts?.getHref ?? (() => `${win.location.pathname}${win.location.search}${win.location.hash}`);
|
|
137
138
|
const createHref = opts?.createHref ?? (path => path);
|
|
138
|
-
let currentLocation = parseLocation(getHref(),
|
|
139
|
+
let currentLocation = parseLocation(getHref(), win.history.state);
|
|
139
140
|
let rollbackLocation;
|
|
140
141
|
const getLocation = () => currentLocation;
|
|
141
142
|
let next;
|
|
@@ -163,7 +164,7 @@ function createBrowserHistory(opts) {
|
|
|
163
164
|
// Do not notify subscribers about this push/replace call
|
|
164
165
|
untrack(() => {
|
|
165
166
|
if (!next) return;
|
|
166
|
-
|
|
167
|
+
win.history[next.isPush ? 'pushState' : 'replaceState'](next.state, '', next.href);
|
|
167
168
|
// Reset the nextIsPush flag and clear the scheduled update
|
|
168
169
|
next = undefined;
|
|
169
170
|
scheduled = undefined;
|
|
@@ -196,25 +197,25 @@ function createBrowserHistory(opts) {
|
|
|
196
197
|
}
|
|
197
198
|
};
|
|
198
199
|
const onPushPop = () => {
|
|
199
|
-
currentLocation = parseLocation(getHref(),
|
|
200
|
+
currentLocation = parseLocation(getHref(), win.history.state);
|
|
200
201
|
history.notify();
|
|
201
202
|
};
|
|
202
|
-
var originalPushState =
|
|
203
|
-
var originalReplaceState =
|
|
203
|
+
var originalPushState = win.history.pushState;
|
|
204
|
+
var originalReplaceState = win.history.replaceState;
|
|
204
205
|
const history = createHistory({
|
|
205
206
|
getLocation,
|
|
206
207
|
pushState: (path, state, onUpdate) => queueHistoryAction('push', path, state, onUpdate),
|
|
207
208
|
replaceState: (path, state, onUpdate) => queueHistoryAction('replace', path, state, onUpdate),
|
|
208
|
-
back: () =>
|
|
209
|
-
forward: () =>
|
|
210
|
-
go: n =>
|
|
209
|
+
back: () => win.history.back(),
|
|
210
|
+
forward: () => win.history.forward(),
|
|
211
|
+
go: n => win.history.go(n),
|
|
211
212
|
createHref: path => createHref(path),
|
|
212
213
|
flush,
|
|
213
214
|
destroy: () => {
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
215
|
+
win.history.pushState = originalPushState;
|
|
216
|
+
win.history.replaceState = originalReplaceState;
|
|
217
|
+
win.removeEventListener(pushStateEvent, onPushPop);
|
|
218
|
+
win.removeEventListener(popStateEvent, onPushPop);
|
|
218
219
|
},
|
|
219
220
|
onBlocked: onUpdate => {
|
|
220
221
|
// If a navigation is blocked, we need to rollback the location
|
|
@@ -226,24 +227,27 @@ function createBrowserHistory(opts) {
|
|
|
226
227
|
}
|
|
227
228
|
}
|
|
228
229
|
});
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
let res = originalPushState.apply(
|
|
230
|
+
win.addEventListener(pushStateEvent, onPushPop);
|
|
231
|
+
win.addEventListener(popStateEvent, onPushPop);
|
|
232
|
+
win.history.pushState = function () {
|
|
233
|
+
let res = originalPushState.apply(win.history, arguments);
|
|
233
234
|
if (tracking) history.notify();
|
|
234
235
|
return res;
|
|
235
236
|
};
|
|
236
|
-
|
|
237
|
-
let res = originalReplaceState.apply(
|
|
237
|
+
win.history.replaceState = function () {
|
|
238
|
+
let res = originalReplaceState.apply(win.history, arguments);
|
|
238
239
|
if (tracking) history.notify();
|
|
239
240
|
return res;
|
|
240
241
|
};
|
|
241
242
|
return history;
|
|
242
243
|
}
|
|
243
|
-
function createHashHistory(
|
|
244
|
+
function createHashHistory({
|
|
245
|
+
window: win
|
|
246
|
+
}) {
|
|
244
247
|
return createBrowserHistory({
|
|
245
|
-
getHref: () =>
|
|
246
|
-
createHref: path => `#${path}
|
|
248
|
+
getHref: () => win.location.hash.substring(1),
|
|
249
|
+
createHref: path => `#${path}`,
|
|
250
|
+
window: win
|
|
247
251
|
});
|
|
248
252
|
}
|
|
249
253
|
function createMemoryHistory(opts = {
|
|
@@ -272,7 +276,9 @@ function createMemoryHistory(opts = {
|
|
|
272
276
|
forward: () => {
|
|
273
277
|
index = Math.min(index + 1, entries.length - 1);
|
|
274
278
|
},
|
|
275
|
-
go: n =>
|
|
279
|
+
go: n => {
|
|
280
|
+
index = Math.min(Math.max(index + n, 0), entries.length - 1);
|
|
281
|
+
},
|
|
276
282
|
createHref: path => path
|
|
277
283
|
});
|
|
278
284
|
}
|
|
@@ -295,5 +301,6 @@ function createRandomKey() {
|
|
|
295
301
|
|
|
296
302
|
exports.createBrowserHistory = createBrowserHistory;
|
|
297
303
|
exports.createHashHistory = createHashHistory;
|
|
304
|
+
exports.createHistory = createHistory;
|
|
298
305
|
exports.createMemoryHistory = createMemoryHistory;
|
|
299
306
|
//# sourceMappingURL=index.js.map
|
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: (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,CAAC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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\nexport function createHistory(opts: {\n getLocation: () => HistoryLocation\n pushState: (path: string, state: any, onUpdate: () => void) => void\n replaceState: (path: string, state: any, onUpdate: () => void) => void\n go: (n: number) => void\n back: () => void\n forward: () => void\n createHref: (path: string) => string\n flush?: () => void\n destroy?: () => void\n onBlocked?: (onUpdate: () => void) => void\n}): RouterHistory {\n let location = opts.getLocation()\n let subscribers = new Set<() => void>()\n let blockers: Blocker[] = []\n\n const onUpdate = () => {\n location = opts.getLocation()\n subscribers.forEach((subscriber) => subscriber())\n }\n\n const tryNavigation = (task: () => void) => {\n if (typeof document !== 'undefined' && 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 window?: any\n}): RouterHistory {\n const win =\n opts?.window ??\n (typeof document !== 'undefined' ? window : (undefined as any))\n\n const getHref =\n opts?.getHref ??\n (() => `${win.location.pathname}${win.location.search}${win.location.hash}`)\n\n const createHref = opts?.createHref ?? ((path) => path)\n\n let currentLocation = parseLocation(getHref(), win.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 win.history[next.isPush ? 'pushState' : 'replaceState'](\n next.state,\n '',\n next.href,\n )\n // Reset the nextIsPush flag and clear the scheduled update\n next = undefined\n scheduled = undefined\n rollbackLocation = undefined\n })\n }\n\n // This function queues up a call to update the browser history\n const queueHistoryAction = (\n type: 'push' | 'replace',\n 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(), win.history.state)\n history.notify()\n }\n\n var originalPushState = win.history.pushState\n var originalReplaceState = win.history.replaceState\n\n const history = createHistory({\n getLocation,\n pushState: (path, state, onUpdate) =>\n queueHistoryAction('push', path, state, onUpdate),\n replaceState: (path, state, onUpdate) =>\n queueHistoryAction('replace', path, state, onUpdate),\n back: () => win.history.back(),\n forward: () => win.history.forward(),\n go: (n) => win.history.go(n),\n createHref: (path) => createHref(path),\n flush,\n destroy: () => {\n win.history.pushState = originalPushState\n win.history.replaceState = originalReplaceState\n win.removeEventListener(pushStateEvent, onPushPop)\n win.removeEventListener(popStateEvent, onPushPop)\n },\n onBlocked: (onUpdate) => {\n // If a navigation is blocked, we need to rollback the location\n // that we optimistically updated in memory.\n if (rollbackLocation && currentLocation !== rollbackLocation) {\n currentLocation = rollbackLocation\n // Notify subscribers\n onUpdate()\n }\n },\n })\n\n win.addEventListener(pushStateEvent, onPushPop)\n win.addEventListener(popStateEvent, onPushPop)\n\n win.history.pushState = function () {\n let res = originalPushState.apply(win.history, arguments as any)\n if (tracking) history.notify()\n return res\n }\n\n win.history.replaceState = function () {\n let res = originalReplaceState.apply(win.history, arguments as any)\n if (tracking) history.notify()\n return res\n }\n\n return history\n}\n\nexport function createHashHistory({\n window: win,\n}: {\n window?: any\n}): RouterHistory {\n return createBrowserHistory({\n getHref: () => win.location.hash.substring(1),\n createHref: (path) => `#${path}`,\n window: win,\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) => {\n index = Math.min(Math.max(index + n, 0), entries.length - 1)\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","document","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","win","undefined","getHref","pathname","search","hash","currentLocation","parseLocation","history","rollbackLocation","next","tracking","scheduled","untrack","fn","isPush","href","queueHistoryAction","type","Promise","resolve","then","onPushPop","originalPushState","originalReplaceState","n","res","apply","arguments","createHashHistory","substring","createMemoryHistory","initialEntries","entries","initialIndex","currentState","Math","min","max","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;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CAACD,WAAW,EAAEF,SAAS,KAAK,CAAC,CAAC,GAAGtC,SAAS,GAAGsC,SAAS,CAAC,GACjE,EAAE;IACR7D,KAAK,EAAEA,KAAK,IAAI,EAAC;GAClB,CAAA;AACH,CAAA;;AAEA;AACA,SAASoB,eAAeA,GAAG;AACzB,EAAA,OAAO,CAACsC,IAAI,CAACO,MAAM,EAAE,GAAG,CAAC,EAAEC,QAAQ,CAAC,EAAE,CAAC,CAACd,SAAS,CAAC,CAAC,CAAC,CAAA;AACtD;;;;;;;"}
|
package/build/esm/index.js
CHANGED
|
@@ -34,7 +34,7 @@ function createHistory(opts) {
|
|
|
34
34
|
subscribers.forEach(subscriber => subscriber());
|
|
35
35
|
};
|
|
36
36
|
const tryNavigation = task => {
|
|
37
|
-
if (blockers.length) {
|
|
37
|
+
if (typeof document !== 'undefined' && blockers.length) {
|
|
38
38
|
for (let blocker of blockers) {
|
|
39
39
|
if (!window.confirm(blocker.message)) {
|
|
40
40
|
opts.onBlocked?.(onUpdate);
|
|
@@ -131,9 +131,10 @@ function assignKey(state) {
|
|
|
131
131
|
* @returns A history instance
|
|
132
132
|
*/
|
|
133
133
|
function createBrowserHistory(opts) {
|
|
134
|
-
const
|
|
134
|
+
const win = opts?.window ?? (typeof document !== 'undefined' ? window : undefined);
|
|
135
|
+
const getHref = opts?.getHref ?? (() => `${win.location.pathname}${win.location.search}${win.location.hash}`);
|
|
135
136
|
const createHref = opts?.createHref ?? (path => path);
|
|
136
|
-
let currentLocation = parseLocation(getHref(),
|
|
137
|
+
let currentLocation = parseLocation(getHref(), win.history.state);
|
|
137
138
|
let rollbackLocation;
|
|
138
139
|
const getLocation = () => currentLocation;
|
|
139
140
|
let next;
|
|
@@ -161,7 +162,7 @@ function createBrowserHistory(opts) {
|
|
|
161
162
|
// Do not notify subscribers about this push/replace call
|
|
162
163
|
untrack(() => {
|
|
163
164
|
if (!next) return;
|
|
164
|
-
|
|
165
|
+
win.history[next.isPush ? 'pushState' : 'replaceState'](next.state, '', next.href);
|
|
165
166
|
// Reset the nextIsPush flag and clear the scheduled update
|
|
166
167
|
next = undefined;
|
|
167
168
|
scheduled = undefined;
|
|
@@ -194,25 +195,25 @@ function createBrowserHistory(opts) {
|
|
|
194
195
|
}
|
|
195
196
|
};
|
|
196
197
|
const onPushPop = () => {
|
|
197
|
-
currentLocation = parseLocation(getHref(),
|
|
198
|
+
currentLocation = parseLocation(getHref(), win.history.state);
|
|
198
199
|
history.notify();
|
|
199
200
|
};
|
|
200
|
-
var originalPushState =
|
|
201
|
-
var originalReplaceState =
|
|
201
|
+
var originalPushState = win.history.pushState;
|
|
202
|
+
var originalReplaceState = win.history.replaceState;
|
|
202
203
|
const history = createHistory({
|
|
203
204
|
getLocation,
|
|
204
205
|
pushState: (path, state, onUpdate) => queueHistoryAction('push', path, state, onUpdate),
|
|
205
206
|
replaceState: (path, state, onUpdate) => queueHistoryAction('replace', path, state, onUpdate),
|
|
206
|
-
back: () =>
|
|
207
|
-
forward: () =>
|
|
208
|
-
go: n =>
|
|
207
|
+
back: () => win.history.back(),
|
|
208
|
+
forward: () => win.history.forward(),
|
|
209
|
+
go: n => win.history.go(n),
|
|
209
210
|
createHref: path => createHref(path),
|
|
210
211
|
flush,
|
|
211
212
|
destroy: () => {
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
213
|
+
win.history.pushState = originalPushState;
|
|
214
|
+
win.history.replaceState = originalReplaceState;
|
|
215
|
+
win.removeEventListener(pushStateEvent, onPushPop);
|
|
216
|
+
win.removeEventListener(popStateEvent, onPushPop);
|
|
216
217
|
},
|
|
217
218
|
onBlocked: onUpdate => {
|
|
218
219
|
// If a navigation is blocked, we need to rollback the location
|
|
@@ -224,24 +225,27 @@ function createBrowserHistory(opts) {
|
|
|
224
225
|
}
|
|
225
226
|
}
|
|
226
227
|
});
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
let res = originalPushState.apply(
|
|
228
|
+
win.addEventListener(pushStateEvent, onPushPop);
|
|
229
|
+
win.addEventListener(popStateEvent, onPushPop);
|
|
230
|
+
win.history.pushState = function () {
|
|
231
|
+
let res = originalPushState.apply(win.history, arguments);
|
|
231
232
|
if (tracking) history.notify();
|
|
232
233
|
return res;
|
|
233
234
|
};
|
|
234
|
-
|
|
235
|
-
let res = originalReplaceState.apply(
|
|
235
|
+
win.history.replaceState = function () {
|
|
236
|
+
let res = originalReplaceState.apply(win.history, arguments);
|
|
236
237
|
if (tracking) history.notify();
|
|
237
238
|
return res;
|
|
238
239
|
};
|
|
239
240
|
return history;
|
|
240
241
|
}
|
|
241
|
-
function createHashHistory(
|
|
242
|
+
function createHashHistory({
|
|
243
|
+
window: win
|
|
244
|
+
}) {
|
|
242
245
|
return createBrowserHistory({
|
|
243
|
-
getHref: () =>
|
|
244
|
-
createHref: path => `#${path}
|
|
246
|
+
getHref: () => win.location.hash.substring(1),
|
|
247
|
+
createHref: path => `#${path}`,
|
|
248
|
+
window: win
|
|
245
249
|
});
|
|
246
250
|
}
|
|
247
251
|
function createMemoryHistory(opts = {
|
|
@@ -270,7 +274,9 @@ function createMemoryHistory(opts = {
|
|
|
270
274
|
forward: () => {
|
|
271
275
|
index = Math.min(index + 1, entries.length - 1);
|
|
272
276
|
},
|
|
273
|
-
go: n =>
|
|
277
|
+
go: n => {
|
|
278
|
+
index = Math.min(Math.max(index + n, 0), entries.length - 1);
|
|
279
|
+
},
|
|
274
280
|
createHref: path => path
|
|
275
281
|
});
|
|
276
282
|
}
|
|
@@ -291,5 +297,5 @@ function createRandomKey() {
|
|
|
291
297
|
return (Math.random() + 1).toString(36).substring(7);
|
|
292
298
|
}
|
|
293
299
|
|
|
294
|
-
export { createBrowserHistory, createHashHistory, createMemoryHistory };
|
|
300
|
+
export { createBrowserHistory, createHashHistory, createHistory, createMemoryHistory };
|
|
295
301
|
//# sourceMappingURL=index.js.map
|
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: (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,W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AACvB,EAAE,CAACyC,CAAC,CAAC;IAC/BrC,UAAU,EAAGV,IAAI,IAAKA,IAAAA;AACxB,GAAC,CAAC,CAAA;AACJ,CAAA;AAEA,SAAS4B,aAAaA,CAACS,IAAY,EAAEpC,KAAmB,EAAmB;AACzE,EAAA,IAAI2D,SAAS,GAAGvB,IAAI,CAACwB,OAAO,CAAC,GAAG,CAAC,CAAA;AACjC,EAAA,IAAIC,WAAW,GAAGzB,IAAI,CAACwB,OAAO,CAAC,GAAG,CAAC,CAAA;EAEnC,OAAO;IACLxB,IAAI;AACJb,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;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;;;;"}
|
|
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\nexport function createHistory(opts: {\n getLocation: () => HistoryLocation\n pushState: (path: string, state: any, onUpdate: () => void) => void\n replaceState: (path: string, state: any, onUpdate: () => void) => void\n go: (n: number) => void\n back: () => void\n forward: () => void\n createHref: (path: string) => string\n flush?: () => void\n destroy?: () => void\n onBlocked?: (onUpdate: () => void) => void\n}): RouterHistory {\n let location = opts.getLocation()\n let subscribers = new Set<() => void>()\n let blockers: Blocker[] = []\n\n const onUpdate = () => {\n location = opts.getLocation()\n subscribers.forEach((subscriber) => subscriber())\n }\n\n const tryNavigation = (task: () => void) => {\n if (typeof document !== 'undefined' && 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 window?: any\n}): RouterHistory {\n const win =\n opts?.window ??\n (typeof document !== 'undefined' ? window : (undefined as any))\n\n const getHref =\n opts?.getHref ??\n (() => `${win.location.pathname}${win.location.search}${win.location.hash}`)\n\n const createHref = opts?.createHref ?? ((path) => path)\n\n let currentLocation = parseLocation(getHref(), win.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 win.history[next.isPush ? 'pushState' : 'replaceState'](\n next.state,\n '',\n next.href,\n )\n // Reset the nextIsPush flag and clear the scheduled update\n next = undefined\n scheduled = undefined\n rollbackLocation = undefined\n })\n }\n\n // This function queues up a call to update the browser history\n const queueHistoryAction = (\n type: 'push' | 'replace',\n 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(), win.history.state)\n history.notify()\n }\n\n var originalPushState = win.history.pushState\n var originalReplaceState = win.history.replaceState\n\n const history = createHistory({\n getLocation,\n pushState: (path, state, onUpdate) =>\n queueHistoryAction('push', path, state, onUpdate),\n replaceState: (path, state, onUpdate) =>\n queueHistoryAction('replace', path, state, onUpdate),\n back: () => win.history.back(),\n forward: () => win.history.forward(),\n go: (n) => win.history.go(n),\n createHref: (path) => createHref(path),\n flush,\n destroy: () => {\n win.history.pushState = originalPushState\n win.history.replaceState = originalReplaceState\n win.removeEventListener(pushStateEvent, onPushPop)\n win.removeEventListener(popStateEvent, onPushPop)\n },\n onBlocked: (onUpdate) => {\n // If a navigation is blocked, we need to rollback the location\n // that we optimistically updated in memory.\n if (rollbackLocation && currentLocation !== rollbackLocation) {\n currentLocation = rollbackLocation\n // Notify subscribers\n onUpdate()\n }\n },\n })\n\n win.addEventListener(pushStateEvent, onPushPop)\n win.addEventListener(popStateEvent, onPushPop)\n\n win.history.pushState = function () {\n let res = originalPushState.apply(win.history, arguments as any)\n if (tracking) history.notify()\n return res\n }\n\n win.history.replaceState = function () {\n let res = originalReplaceState.apply(win.history, arguments as any)\n if (tracking) history.notify()\n return res\n }\n\n return history\n}\n\nexport function createHashHistory({\n window: win,\n}: {\n window?: any\n}): RouterHistory {\n return createBrowserHistory({\n getHref: () => win.location.hash.substring(1),\n createHref: (path) => `#${path}`,\n window: win,\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) => {\n index = Math.min(Math.max(index + n, 0), entries.length - 1)\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","document","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","win","undefined","getHref","pathname","search","hash","currentLocation","parseLocation","history","rollbackLocation","next","tracking","scheduled","untrack","fn","isPush","href","queueHistoryAction","type","Promise","resolve","then","onPushPop","originalPushState","originalReplaceState","n","res","apply","arguments","createHashHistory","substring","createMemoryHistory","initialEntries","entries","initialIndex","currentState","Math","min","max","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;AAEM,SAASC,aAAaA,CAACC,IAW7B,EAAiB;AAChB,EAAA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AS,CAAC,GACjE,EAAE;IACR7D,KAAK,EAAEA,KAAK,IAAI,EAAC;GAClB,CAAA;AACH,CAAA;;AAEA;AACA,SAASoB,eAAeA,GAAG;AACzB,EAAA,OAAO,CAACsC,IAAI,CAACO,MAAM,EAAE,GAAG,CAAC,EAAEC,QAAQ,CAAC,EAAE,CAAC,CAACd,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":"9881-1"}]}],"isRoot":true},"nodeParts":{"9881-1":{"renderedLength":9537,"gzipLength":2784,"brotliLength":0,"metaUid":"9881-0"}},"nodeMetas":{"9881-0":{"id":"/packages/history/src/index.ts","moduleParts":{"index.production.js":"9881-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": "9881-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
|
+
"9881-3": {
|
|
20
|
+
"renderedLength": 9537,
|
|
21
|
+
"gzipLength": 2784,
|
|
22
22
|
"brotliLength": 0,
|
|
23
|
-
"metaUid": "
|
|
23
|
+
"metaUid": "9881-2"
|
|
24
24
|
}
|
|
25
25
|
},
|
|
26
26
|
"nodeMetas": {
|
|
27
|
-
"
|
|
27
|
+
"9881-2": {
|
|
28
28
|
"id": "/packages/history/src/index.ts",
|
|
29
29
|
"moduleParts": {
|
|
30
|
-
"index.production.js": "
|
|
30
|
+
"index.production.js": "9881-3"
|
|
31
31
|
},
|
|
32
32
|
"imported": [],
|
|
33
33
|
"importedBy": [],
|
package/build/types/index.d.ts
CHANGED
|
@@ -34,6 +34,18 @@ interface ParsedPath {
|
|
|
34
34
|
interface HistoryState {
|
|
35
35
|
key: string;
|
|
36
36
|
}
|
|
37
|
+
declare function createHistory(opts: {
|
|
38
|
+
getLocation: () => HistoryLocation;
|
|
39
|
+
pushState: (path: string, state: any, onUpdate: () => void) => void;
|
|
40
|
+
replaceState: (path: string, state: any, onUpdate: () => void) => void;
|
|
41
|
+
go: (n: number) => void;
|
|
42
|
+
back: () => void;
|
|
43
|
+
forward: () => void;
|
|
44
|
+
createHref: (path: string) => string;
|
|
45
|
+
flush?: () => void;
|
|
46
|
+
destroy?: () => void;
|
|
47
|
+
onBlocked?: (onUpdate: () => void) => void;
|
|
48
|
+
}): RouterHistory;
|
|
37
49
|
/**
|
|
38
50
|
* Creates a history object that can be used to interact with the browser's
|
|
39
51
|
* navigation. This is a lightweight API wrapping the browser's native methods.
|
|
@@ -53,11 +65,14 @@ interface HistoryState {
|
|
|
53
65
|
declare function createBrowserHistory(opts?: {
|
|
54
66
|
getHref?: () => string;
|
|
55
67
|
createHref?: (path: string) => string;
|
|
68
|
+
window?: any;
|
|
69
|
+
}): RouterHistory;
|
|
70
|
+
declare function createHashHistory({ window: win, }: {
|
|
71
|
+
window?: any;
|
|
56
72
|
}): RouterHistory;
|
|
57
|
-
declare function createHashHistory(): RouterHistory;
|
|
58
73
|
declare function createMemoryHistory(opts?: {
|
|
59
74
|
initialEntries: string[];
|
|
60
75
|
initialIndex?: number;
|
|
61
76
|
}): RouterHistory;
|
|
62
77
|
|
|
63
|
-
export { type HistoryLocation, type HistoryState, type ParsedPath, type RouterHistory, createBrowserHistory, createHashHistory, createMemoryHistory };
|
|
78
|
+
export { type HistoryLocation, type HistoryState, type ParsedPath, type RouterHistory, createBrowserHistory, createHashHistory, createHistory, createMemoryHistory };
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
subscribers.forEach(subscriber => subscriber());
|
|
41
41
|
};
|
|
42
42
|
const tryNavigation = task => {
|
|
43
|
-
if (blockers.length) {
|
|
43
|
+
if (typeof document !== 'undefined' && blockers.length) {
|
|
44
44
|
for (let blocker of blockers) {
|
|
45
45
|
if (!window.confirm(blocker.message)) {
|
|
46
46
|
opts.onBlocked?.(onUpdate);
|
|
@@ -137,9 +137,10 @@
|
|
|
137
137
|
* @returns A history instance
|
|
138
138
|
*/
|
|
139
139
|
function createBrowserHistory(opts) {
|
|
140
|
-
const
|
|
140
|
+
const win = opts?.window ?? (typeof document !== 'undefined' ? window : undefined);
|
|
141
|
+
const getHref = opts?.getHref ?? (() => `${win.location.pathname}${win.location.search}${win.location.hash}`);
|
|
141
142
|
const createHref = opts?.createHref ?? (path => path);
|
|
142
|
-
let currentLocation = parseLocation(getHref(),
|
|
143
|
+
let currentLocation = parseLocation(getHref(), win.history.state);
|
|
143
144
|
let rollbackLocation;
|
|
144
145
|
const getLocation = () => currentLocation;
|
|
145
146
|
let next;
|
|
@@ -167,7 +168,7 @@
|
|
|
167
168
|
// Do not notify subscribers about this push/replace call
|
|
168
169
|
untrack(() => {
|
|
169
170
|
if (!next) return;
|
|
170
|
-
|
|
171
|
+
win.history[next.isPush ? 'pushState' : 'replaceState'](next.state, '', next.href);
|
|
171
172
|
// Reset the nextIsPush flag and clear the scheduled update
|
|
172
173
|
next = undefined;
|
|
173
174
|
scheduled = undefined;
|
|
@@ -200,25 +201,25 @@
|
|
|
200
201
|
}
|
|
201
202
|
};
|
|
202
203
|
const onPushPop = () => {
|
|
203
|
-
currentLocation = parseLocation(getHref(),
|
|
204
|
+
currentLocation = parseLocation(getHref(), win.history.state);
|
|
204
205
|
history.notify();
|
|
205
206
|
};
|
|
206
|
-
var originalPushState =
|
|
207
|
-
var originalReplaceState =
|
|
207
|
+
var originalPushState = win.history.pushState;
|
|
208
|
+
var originalReplaceState = win.history.replaceState;
|
|
208
209
|
const history = createHistory({
|
|
209
210
|
getLocation,
|
|
210
211
|
pushState: (path, state, onUpdate) => queueHistoryAction('push', path, state, onUpdate),
|
|
211
212
|
replaceState: (path, state, onUpdate) => queueHistoryAction('replace', path, state, onUpdate),
|
|
212
|
-
back: () =>
|
|
213
|
-
forward: () =>
|
|
214
|
-
go: n =>
|
|
213
|
+
back: () => win.history.back(),
|
|
214
|
+
forward: () => win.history.forward(),
|
|
215
|
+
go: n => win.history.go(n),
|
|
215
216
|
createHref: path => createHref(path),
|
|
216
217
|
flush,
|
|
217
218
|
destroy: () => {
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
219
|
+
win.history.pushState = originalPushState;
|
|
220
|
+
win.history.replaceState = originalReplaceState;
|
|
221
|
+
win.removeEventListener(pushStateEvent, onPushPop);
|
|
222
|
+
win.removeEventListener(popStateEvent, onPushPop);
|
|
222
223
|
},
|
|
223
224
|
onBlocked: onUpdate => {
|
|
224
225
|
// If a navigation is blocked, we need to rollback the location
|
|
@@ -230,24 +231,27 @@
|
|
|
230
231
|
}
|
|
231
232
|
}
|
|
232
233
|
});
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
let res = originalPushState.apply(
|
|
234
|
+
win.addEventListener(pushStateEvent, onPushPop);
|
|
235
|
+
win.addEventListener(popStateEvent, onPushPop);
|
|
236
|
+
win.history.pushState = function () {
|
|
237
|
+
let res = originalPushState.apply(win.history, arguments);
|
|
237
238
|
if (tracking) history.notify();
|
|
238
239
|
return res;
|
|
239
240
|
};
|
|
240
|
-
|
|
241
|
-
let res = originalReplaceState.apply(
|
|
241
|
+
win.history.replaceState = function () {
|
|
242
|
+
let res = originalReplaceState.apply(win.history, arguments);
|
|
242
243
|
if (tracking) history.notify();
|
|
243
244
|
return res;
|
|
244
245
|
};
|
|
245
246
|
return history;
|
|
246
247
|
}
|
|
247
|
-
function createHashHistory(
|
|
248
|
+
function createHashHistory({
|
|
249
|
+
window: win
|
|
250
|
+
}) {
|
|
248
251
|
return createBrowserHistory({
|
|
249
|
-
getHref: () =>
|
|
250
|
-
createHref: path => `#${path}
|
|
252
|
+
getHref: () => win.location.hash.substring(1),
|
|
253
|
+
createHref: path => `#${path}`,
|
|
254
|
+
window: win
|
|
251
255
|
});
|
|
252
256
|
}
|
|
253
257
|
function createMemoryHistory(opts = {
|
|
@@ -276,7 +280,9 @@
|
|
|
276
280
|
forward: () => {
|
|
277
281
|
index = Math.min(index + 1, entries.length - 1);
|
|
278
282
|
},
|
|
279
|
-
go: n =>
|
|
283
|
+
go: n => {
|
|
284
|
+
index = Math.min(Math.max(index + n, 0), entries.length - 1);
|
|
285
|
+
},
|
|
280
286
|
createHref: path => path
|
|
281
287
|
});
|
|
282
288
|
}
|
|
@@ -299,6 +305,7 @@
|
|
|
299
305
|
|
|
300
306
|
exports.createBrowserHistory = createBrowserHistory;
|
|
301
307
|
exports.createHashHistory = createHashHistory;
|
|
308
|
+
exports.createHistory = createHistory;
|
|
302
309
|
exports.createMemoryHistory = createMemoryHistory;
|
|
303
310
|
|
|
304
311
|
}));
|
|
@@ -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: (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,IAAIC,WAAW,GAAG,IAAIC,GAAG,EAAc,CAAA;IACvC,IAAIC,QAAmB,GAAG,EAAE,CAAA;IAE5B,MAAMC,QAAQ,GAAGA,MAAM;EACrBL,IAAAA,QAAQ,GAAGD,IAAI,CAACE,WAAW,EAAE,CAAA;MAC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AACwB,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;;;;;;;;;;"}
|
|
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\nexport function createHistory(opts: {\n getLocation: () => HistoryLocation\n pushState: (path: string, state: any, onUpdate: () => void) => void\n replaceState: (path: string, state: any, onUpdate: () => void) => void\n go: (n: number) => void\n back: () => void\n forward: () => void\n createHref: (path: string) => string\n flush?: () => void\n destroy?: () => void\n onBlocked?: (onUpdate: () => void) => void\n}): RouterHistory {\n let location = opts.getLocation()\n let subscribers = new Set<() => void>()\n let blockers: Blocker[] = []\n\n const onUpdate = () => {\n location = opts.getLocation()\n subscribers.forEach((subscriber) => subscriber())\n }\n\n const tryNavigation = (task: () => void) => {\n if (typeof document !== 'undefined' && 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 window?: any\n}): RouterHistory {\n const win =\n opts?.window ??\n (typeof document !== 'undefined' ? window : (undefined as any))\n\n const getHref =\n opts?.getHref ??\n (() => `${win.location.pathname}${win.location.search}${win.location.hash}`)\n\n const createHref = opts?.createHref ?? ((path) => path)\n\n let currentLocation = parseLocation(getHref(), win.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 win.history[next.isPush ? 'pushState' : 'replaceState'](\n next.state,\n '',\n next.href,\n )\n // Reset the nextIsPush flag and clear the scheduled update\n next = undefined\n scheduled = undefined\n rollbackLocation = undefined\n })\n }\n\n // This function queues up a call to update the browser history\n const queueHistoryAction = (\n type: 'push' | 'replace',\n 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(), win.history.state)\n history.notify()\n }\n\n var originalPushState = win.history.pushState\n var originalReplaceState = win.history.replaceState\n\n const history = createHistory({\n getLocation,\n pushState: (path, state, onUpdate) =>\n queueHistoryAction('push', path, state, onUpdate),\n replaceState: (path, state, onUpdate) =>\n queueHistoryAction('replace', path, state, onUpdate),\n back: () => win.history.back(),\n forward: () => win.history.forward(),\n go: (n) => win.history.go(n),\n createHref: (path) => createHref(path),\n flush,\n destroy: () => {\n win.history.pushState = originalPushState\n win.history.replaceState = originalReplaceState\n win.removeEventListener(pushStateEvent, onPushPop)\n win.removeEventListener(popStateEvent, onPushPop)\n },\n onBlocked: (onUpdate) => {\n // If a navigation is blocked, we need to rollback the location\n // that we optimistically updated in memory.\n if (rollbackLocation && currentLocation !== rollbackLocation) {\n currentLocation = rollbackLocation\n // Notify subscribers\n onUpdate()\n }\n },\n })\n\n win.addEventListener(pushStateEvent, onPushPop)\n win.addEventListener(popStateEvent, onPushPop)\n\n win.history.pushState = function () {\n let res = originalPushState.apply(win.history, arguments as any)\n if (tracking) history.notify()\n return res\n }\n\n win.history.replaceState = function () {\n let res = originalReplaceState.apply(win.history, arguments as any)\n if (tracking) history.notify()\n return res\n }\n\n return history\n}\n\nexport function createHashHistory({\n window: win,\n}: {\n window?: any\n}): RouterHistory {\n return createBrowserHistory({\n getHref: () => win.location.hash.substring(1),\n createHref: (path) => `#${path}`,\n window: win,\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) => {\n index = Math.min(Math.max(index + n, 0), entries.length - 1)\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","document","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","win","undefined","getHref","pathname","search","hash","currentLocation","parseLocation","history","rollbackLocation","next","tracking","scheduled","untrack","fn","isPush","href","queueHistoryAction","type","Promise","resolve","then","onPushPop","originalPushState","originalReplaceState","n","res","apply","arguments","createHashHistory","substring","createMemoryHistory","initialEntries","entries","initialIndex","currentState","Math","min","max","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;EAEM,SAASC,aAAaA,CAACC,IAW7B,EAAiB;EAChB,EAAA,IAAIC,QAAQ,GAAGD,IAAI,CAACE,WAAW,EAAE,CAAA;EACjC,EAAA,IAAIC,WAAW,GAAG,IAAIC,GAAG,EAAc,CAAA;IACvC,IAAIC,QAAmB,GAAG,EAAE,CAAA;IAE5B,MAAMC,QAAQ,GAAGA,MAAM;EACrBL,IA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AAC,EAAE,CAAC,CAACd,SAAS,CAAC,CAAC,CAAC,CAAA;EACtD;;;;;;;;;;;"}
|
|
@@ -8,5 +8,5 @@
|
|
|
8
8
|
*
|
|
9
9
|
* @license MIT
|
|
10
10
|
*/
|
|
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",
|
|
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",r="popstate",n="beforeunload",o=e=>(e.preventDefault(),e.returnValue=""),i=()=>{removeEventListener(n,o,{capture:!0})};function s(e){let t=e.getLocation(),r=new Set,s=[];const c=()=>{t=e.getLocation(),r.forEach((e=>e()))},h=t=>{if("undefined"!=typeof document&&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=>(r.add(e),()=>{r.delete(e)}),push:(t,r)=>{r=a(r),h((()=>{e.pushState(t,r,c)}))},replace:(t,r)=>{r=a(r),h((()=>{e.replaceState(t,r,c)}))},go:t=>{h((()=>{e.go(t)}))},back:()=>{h((()=>{e.back()}))},forward:()=>{h((()=>{e.forward()}))},createHref:t=>e.createHref(t),block:e=>{const t={message:e};return s.push(t),1===s.length&&addEventListener(n,o,{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:u()}}function c(e){const n=e?.window??("undefined"!=typeof document?window:void 0),o=e?.getHref??(()=>`${n.location.pathname}${n.location.search}${n.location.hash}`),i=e?.createHref??(e=>e);let a,c=h(o(),n.history.state);let u,f,l=!0;const d=()=>{l=!1,(()=>{u&&(n.history[u.isPush?"pushState":"replaceState"](u.state,"",u.href),u=void 0,f=void 0,a=void 0)})(),l=!0},p=(e,t,r,n)=>{const o=i(t);f||(a=c),c=h(o,r),u={href:o,state:r,isPush:u?.isPush||"push"===e},n(),f||(f=Promise.resolve().then((()=>d())))},y=()=>{c=h(o(),n.history.state),v.notify()};var g=n.history.pushState,m=n.history.replaceState;const v=s({getLocation:()=>c,pushState:(e,t,r)=>p("push",e,t,r),replaceState:(e,t,r)=>p("replace",e,t,r),back:()=>n.history.back(),forward:()=>n.history.forward(),go:e=>n.history.go(e),createHref:e=>i(e),flush:d,destroy:()=>{n.history.pushState=g,n.history.replaceState=m,n.removeEventListener(t,y),n.removeEventListener(r,y)},onBlocked:e=>{a&&c!==a&&(c=a,e())}});return n.addEventListener(t,y),n.addEventListener(r,y),n.history.pushState=function(){let e=g.apply(n.history,arguments);return l&&v.notify(),e},n.history.replaceState=function(){let e=m.apply(n.history,arguments);return l&&v.notify(),e},v}function h(e,t){let r=e.indexOf("#"),n=e.indexOf("?");return{href:e,pathname:e.substring(0,r>0?n>0?Math.min(r,n):r:n>0?n:e.length),hash:r>-1?e.substring(r):"",search:n>-1?e.slice(n,-1===r?void 0:r):"",state:t||{}}}function u(){return(Math.random()+1).toString(36).substring(7)}e.createBrowserHistory=c,e.createHashHistory=function({window:e}){return c({getHref:()=>e.location.hash.substring(1),createHref:e=>`#${e}`,window:e})},e.createHistory=s,e.createMemoryHistory=function(e={initialEntries:["/"]}){const t=e.initialEntries;let r=e.initialIndex??t.length-1,n={key:u()};return s({getLocation:()=>h(t[r],n),pushState:(e,o)=>{n=o,t.push(e),r++},replaceState:(e,o)=>{n=o,t[r]=e},back:()=>{r--},forward:()=>{r=Math.min(r+1,t.length-1)},go:e=>{r=Math.min(Math.max(r+e,0),t.length-1)},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: (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","initialIndex","currentState"],"mappings":";;;;;;;;;;uPAsCA,MAAMA,EAAiB,YACjBC,EAAgB,WAChBC,EAAoB,eAEpBC,EAAwBC,IAC5BA,EAAMC,iBAEED,EAAME,YAAc,IAGxBC,EAAeA,KACnBC,oBAAoBN,EAAmBC,EAAsB,CAC3DM,SAAS,GACT,EAGJ,SAASC,EAAcC,GAYrB,IAAIC,EAAWD,EAAKE,cAChBC,EAAc,IAAIC,IAClBC,EAAsB,GAE1B,MAAMC,EAAWA,KACfL,EAAWD,EAAKE,cAChBC,EAAYI,SAASC,GAAeA,KAAa,EAG7CC,EAAiBC,IACrB,GAAIL,EAASM,OACX,IAAK,IAAIC,KAAWP,EAClB,IAAKQ,OAAOC,QAAQF,EAAQG,SAE1B,YADAf,EAAKgB,YAAYV,GAMvBI,GAAM,EAGR,MAAO,CACL,YAAIT,GACF,OAAOA,CACR,EACDgB,UAAYC,IACVf,EAAYgB,IAAID,GAET,KACLf,EAAYiB,OAAOF,EAAG,GAG1BG,KA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ASA,KACPF,EAAQ+C,KAAKC,IAAIhD,EAAQ,EAAGqD,EAAQvE,OAAS,EAAE,EAEjDiB,GAAKwC,GAAMvD,OAAOuC,QAAQxB,GAAGwC,GAC7BpC,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\nexport function createHistory(opts: {\n getLocation: () => HistoryLocation\n pushState: (path: string, state: any, onUpdate: () => void) => void\n replaceState: (path: string, state: any, onUpdate: () => void) => void\n go: (n: number) => void\n back: () => void\n forward: () => void\n createHref: (path: string) => string\n flush?: () => void\n destroy?: () => void\n onBlocked?: (onUpdate: () => void) => void\n}): RouterHistory {\n let location = opts.getLocation()\n let subscribers = new Set<() => void>()\n let blockers: Blocker[] = []\n\n const onUpdate = () => {\n location = opts.getLocation()\n subscribers.forEach((subscriber) => subscriber())\n }\n\n const tryNavigation = (task: () => void) => {\n if (typeof document !== 'undefined' && 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 window?: any\n}): RouterHistory {\n const win =\n opts?.window ??\n (typeof document !== 'undefined' ? window : (undefined as any))\n\n const getHref =\n opts?.getHref ??\n (() => `${win.location.pathname}${win.location.search}${win.location.hash}`)\n\n const createHref = opts?.createHref ?? ((path) => path)\n\n let currentLocation = parseLocation(getHref(), win.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 win.history[next.isPush ? 'pushState' : 'replaceState'](\n next.state,\n '',\n next.href,\n )\n // Reset the nextIsPush flag and clear the scheduled update\n next = undefined\n scheduled = undefined\n rollbackLocation = undefined\n })\n }\n\n // This function queues up a call to update the browser history\n const queueHistoryAction = (\n type: 'push' | 'replace',\n 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(), win.history.state)\n history.notify()\n }\n\n var originalPushState = win.history.pushState\n var originalReplaceState = win.history.replaceState\n\n const history = createHistory({\n getLocation,\n pushState: (path, state, onUpdate) =>\n queueHistoryAction('push', path, state, onUpdate),\n replaceState: (path, state, onUpdate) =>\n queueHistoryAction('replace', path, state, onUpdate),\n back: () => win.history.back(),\n forward: () => win.history.forward(),\n go: (n) => win.history.go(n),\n createHref: (path) => createHref(path),\n flush,\n destroy: () => {\n win.history.pushState = originalPushState\n win.history.replaceState = originalReplaceState\n win.removeEventListener(pushStateEvent, onPushPop)\n win.removeEventListener(popStateEvent, onPushPop)\n },\n onBlocked: (onUpdate) => {\n // If a navigation is blocked, we need to rollback the location\n // that we optimistically updated in memory.\n if (rollbackLocation && currentLocation !== rollbackLocation) {\n currentLocation = rollbackLocation\n // Notify subscribers\n onUpdate()\n }\n },\n })\n\n win.addEventListener(pushStateEvent, onPushPop)\n win.addEventListener(popStateEvent, onPushPop)\n\n win.history.pushState = function () {\n let res = originalPushState.apply(win.history, arguments as any)\n if (tracking) history.notify()\n return res\n }\n\n win.history.replaceState = function () {\n let res = originalReplaceState.apply(win.history, arguments as any)\n if (tracking) history.notify()\n return res\n }\n\n return history\n}\n\nexport function createHashHistory({\n window: win,\n}: {\n window?: any\n}): RouterHistory {\n return createBrowserHistory({\n getHref: () => win.location.hash.substring(1),\n createHref: (path) => `#${path}`,\n window: win,\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) => {\n index = Math.min(Math.max(index + n, 0), entries.length - 1)\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","document","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","win","undefined","getHref","pathname","search","hash","rollbackLocation","currentLocation","parseLocation","history","next","scheduled","tracking","isPush","href","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","max"],"mappings":";;;;;;;;;;uPAsCA,MAAMA,EAAiB,YACjBC,EAAgB,WAChBC,EAAoB,eAEpBC,EAAwBC,IAC5BA,EAAMC,iBAEED,EAAME,YAAc,IAGxBC,EAAeA,KACnBC,oBAAoBN,EAAmBC,EAAsB,CAC3DM,SAAS,GACT,EAGG,SAASC,EAAcC,GAY5B,IAAIC,EAAWD,EAAKE,cAChBC,EAAc,IAAIC,IAClB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,SACL9C,EAGI,CACFmF,eAAgB,CAAC,OAGnB,MAAMC,EAAUpF,EAAKmF,eACrB,IAAIrD,EAAQ9B,EAAKqF,cAAgBD,EAAQxE,OAAS,EAC9C0E,EAAe,CACjB3C,IAAKC,KAKP,OAAO7C,EAAc,CACnBG,YAHkBA,IAAMoD,EAAc8B,EAAQtD,GAASwD,GAIvD5D,UAAWA,CAACH,EAAMC,KAChB8D,EAAe9D,EACf4D,EAAQ9D,KAAKC,GACbO,GAAO,EAETF,aAAcA,CAACL,EAAMC,KACnB8D,EAAe9D,EACf4D,EAAQtD,GAASP,CAAI,EAEvBQ,KAAMA,KACJD,GAAO,EAETE,QAASA,KACPF,EAAQgD,KAAKC,IAAIjD,EAAQ,EAAGsD,EAAQxE,OAAS,EAAE,EAEjDiB,GAAKyC,IACHxC,EAAQgD,KAAKC,IAAID,KAAKS,IAAIzD,EAAQwC,EAAG,GAAIc,EAAQxE,OAAS,EAAE,EAE9DqB,WAAaV,GAASA,GAE1B"}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -52,7 +52,7 @@ const stopBlocking = () => {
|
|
|
52
52
|
})
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
-
function createHistory(opts: {
|
|
55
|
+
export function createHistory(opts: {
|
|
56
56
|
getLocation: () => HistoryLocation
|
|
57
57
|
pushState: (path: string, state: any, onUpdate: () => void) => void
|
|
58
58
|
replaceState: (path: string, state: any, onUpdate: () => void) => void
|
|
@@ -74,7 +74,7 @@ function createHistory(opts: {
|
|
|
74
74
|
}
|
|
75
75
|
|
|
76
76
|
const tryNavigation = (task: () => void) => {
|
|
77
|
-
if (blockers.length) {
|
|
77
|
+
if (typeof document !== 'undefined' && blockers.length) {
|
|
78
78
|
for (let blocker of blockers) {
|
|
79
79
|
if (!window.confirm(blocker.message)) {
|
|
80
80
|
opts.onBlocked?.(onUpdate)
|
|
@@ -181,15 +181,19 @@ function assignKey(state: HistoryState) {
|
|
|
181
181
|
export function createBrowserHistory(opts?: {
|
|
182
182
|
getHref?: () => string
|
|
183
183
|
createHref?: (path: string) => string
|
|
184
|
+
window?: any
|
|
184
185
|
}): RouterHistory {
|
|
186
|
+
const win =
|
|
187
|
+
opts?.window ??
|
|
188
|
+
(typeof document !== 'undefined' ? window : (undefined as any))
|
|
189
|
+
|
|
185
190
|
const getHref =
|
|
186
191
|
opts?.getHref ??
|
|
187
|
-
(() =>
|
|
188
|
-
`${window.location.pathname}${window.location.search}${window.location.hash}`)
|
|
192
|
+
(() => `${win.location.pathname}${win.location.search}${win.location.hash}`)
|
|
189
193
|
|
|
190
194
|
const createHref = opts?.createHref ?? ((path) => path)
|
|
191
195
|
|
|
192
|
-
let currentLocation = parseLocation(getHref(),
|
|
196
|
+
let currentLocation = parseLocation(getHref(), win.history.state)
|
|
193
197
|
let rollbackLocation: HistoryLocation | undefined
|
|
194
198
|
|
|
195
199
|
const getLocation = () => currentLocation
|
|
@@ -228,7 +232,7 @@ export function createBrowserHistory(opts?: {
|
|
|
228
232
|
// Do not notify subscribers about this push/replace call
|
|
229
233
|
untrack(() => {
|
|
230
234
|
if (!next) return
|
|
231
|
-
|
|
235
|
+
win.history[next.isPush ? 'pushState' : 'replaceState'](
|
|
232
236
|
next.state,
|
|
233
237
|
'',
|
|
234
238
|
next.href,
|
|
@@ -273,12 +277,12 @@ export function createBrowserHistory(opts?: {
|
|
|
273
277
|
}
|
|
274
278
|
|
|
275
279
|
const onPushPop = () => {
|
|
276
|
-
currentLocation = parseLocation(getHref(),
|
|
280
|
+
currentLocation = parseLocation(getHref(), win.history.state)
|
|
277
281
|
history.notify()
|
|
278
282
|
}
|
|
279
283
|
|
|
280
|
-
var originalPushState =
|
|
281
|
-
var originalReplaceState =
|
|
284
|
+
var originalPushState = win.history.pushState
|
|
285
|
+
var originalReplaceState = win.history.replaceState
|
|
282
286
|
|
|
283
287
|
const history = createHistory({
|
|
284
288
|
getLocation,
|
|
@@ -286,16 +290,16 @@ export function createBrowserHistory(opts?: {
|
|
|
286
290
|
queueHistoryAction('push', path, state, onUpdate),
|
|
287
291
|
replaceState: (path, state, onUpdate) =>
|
|
288
292
|
queueHistoryAction('replace', path, state, onUpdate),
|
|
289
|
-
back: () =>
|
|
290
|
-
forward: () =>
|
|
291
|
-
go: (n) =>
|
|
293
|
+
back: () => win.history.back(),
|
|
294
|
+
forward: () => win.history.forward(),
|
|
295
|
+
go: (n) => win.history.go(n),
|
|
292
296
|
createHref: (path) => createHref(path),
|
|
293
297
|
flush,
|
|
294
298
|
destroy: () => {
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
+
win.history.pushState = originalPushState
|
|
300
|
+
win.history.replaceState = originalReplaceState
|
|
301
|
+
win.removeEventListener(pushStateEvent, onPushPop)
|
|
302
|
+
win.removeEventListener(popStateEvent, onPushPop)
|
|
299
303
|
},
|
|
300
304
|
onBlocked: (onUpdate) => {
|
|
301
305
|
// If a navigation is blocked, we need to rollback the location
|
|
@@ -308,17 +312,17 @@ export function createBrowserHistory(opts?: {
|
|
|
308
312
|
},
|
|
309
313
|
})
|
|
310
314
|
|
|
311
|
-
|
|
312
|
-
|
|
315
|
+
win.addEventListener(pushStateEvent, onPushPop)
|
|
316
|
+
win.addEventListener(popStateEvent, onPushPop)
|
|
313
317
|
|
|
314
|
-
|
|
315
|
-
let res = originalPushState.apply(
|
|
318
|
+
win.history.pushState = function () {
|
|
319
|
+
let res = originalPushState.apply(win.history, arguments as any)
|
|
316
320
|
if (tracking) history.notify()
|
|
317
321
|
return res
|
|
318
322
|
}
|
|
319
323
|
|
|
320
|
-
|
|
321
|
-
let res = originalReplaceState.apply(
|
|
324
|
+
win.history.replaceState = function () {
|
|
325
|
+
let res = originalReplaceState.apply(win.history, arguments as any)
|
|
322
326
|
if (tracking) history.notify()
|
|
323
327
|
return res
|
|
324
328
|
}
|
|
@@ -326,10 +330,15 @@ export function createBrowserHistory(opts?: {
|
|
|
326
330
|
return history
|
|
327
331
|
}
|
|
328
332
|
|
|
329
|
-
export function createHashHistory(
|
|
333
|
+
export function createHashHistory({
|
|
334
|
+
window: win,
|
|
335
|
+
}: {
|
|
336
|
+
window?: any
|
|
337
|
+
}): RouterHistory {
|
|
330
338
|
return createBrowserHistory({
|
|
331
|
-
getHref: () =>
|
|
339
|
+
getHref: () => win.location.hash.substring(1),
|
|
332
340
|
createHref: (path) => `#${path}`,
|
|
341
|
+
window: win,
|
|
333
342
|
})
|
|
334
343
|
}
|
|
335
344
|
|
|
@@ -366,7 +375,9 @@ export function createMemoryHistory(
|
|
|
366
375
|
forward: () => {
|
|
367
376
|
index = Math.min(index + 1, entries.length - 1)
|
|
368
377
|
},
|
|
369
|
-
go: (n) =>
|
|
378
|
+
go: (n) => {
|
|
379
|
+
index = Math.min(Math.max(index + n, 0), entries.length - 1)
|
|
380
|
+
},
|
|
370
381
|
createHref: (path) => path,
|
|
371
382
|
})
|
|
372
383
|
}
|