@tanstack/history 1.26.10 → 1.28.9
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/dist/cjs/index.cjs +23 -32
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/esm/index.js +23 -32
- package/dist/esm/index.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +28 -40
package/dist/cjs/index.cjs
CHANGED
|
@@ -16,7 +16,7 @@ function createHistory(opts) {
|
|
|
16
16
|
let location = opts.getLocation();
|
|
17
17
|
const subscribers = /* @__PURE__ */ new Set();
|
|
18
18
|
let blockers = [];
|
|
19
|
-
const
|
|
19
|
+
const notify = () => {
|
|
20
20
|
location = opts.getLocation();
|
|
21
21
|
subscribers.forEach((subscriber) => subscriber());
|
|
22
22
|
};
|
|
@@ -26,7 +26,7 @@ function createHistory(opts) {
|
|
|
26
26
|
for (const blocker of blockers) {
|
|
27
27
|
const allowed = await blocker();
|
|
28
28
|
if (!allowed) {
|
|
29
|
-
(_a = opts.onBlocked) == null ? void 0 : _a.call(opts,
|
|
29
|
+
(_a = opts.onBlocked) == null ? void 0 : _a.call(opts, notify);
|
|
30
30
|
return;
|
|
31
31
|
}
|
|
32
32
|
}
|
|
@@ -47,32 +47,32 @@ function createHistory(opts) {
|
|
|
47
47
|
state = assignKey(state);
|
|
48
48
|
tryNavigation(() => {
|
|
49
49
|
opts.pushState(path, state);
|
|
50
|
-
|
|
50
|
+
notify();
|
|
51
51
|
});
|
|
52
52
|
},
|
|
53
53
|
replace: (path, state) => {
|
|
54
54
|
state = assignKey(state);
|
|
55
55
|
tryNavigation(() => {
|
|
56
56
|
opts.replaceState(path, state);
|
|
57
|
-
|
|
57
|
+
notify();
|
|
58
58
|
});
|
|
59
59
|
},
|
|
60
60
|
go: (index) => {
|
|
61
61
|
tryNavigation(() => {
|
|
62
62
|
opts.go(index);
|
|
63
|
-
|
|
63
|
+
notify();
|
|
64
64
|
});
|
|
65
65
|
},
|
|
66
66
|
back: () => {
|
|
67
67
|
tryNavigation(() => {
|
|
68
68
|
opts.back();
|
|
69
|
-
|
|
69
|
+
notify();
|
|
70
70
|
});
|
|
71
71
|
},
|
|
72
72
|
forward: () => {
|
|
73
73
|
tryNavigation(() => {
|
|
74
74
|
opts.forward();
|
|
75
|
-
|
|
75
|
+
notify();
|
|
76
76
|
});
|
|
77
77
|
},
|
|
78
78
|
createHref: (str) => opts.createHref(str),
|
|
@@ -98,7 +98,7 @@ function createHistory(opts) {
|
|
|
98
98
|
var _a;
|
|
99
99
|
return (_a = opts.destroy) == null ? void 0 : _a.call(opts);
|
|
100
100
|
},
|
|
101
|
-
notify
|
|
101
|
+
notify
|
|
102
102
|
};
|
|
103
103
|
}
|
|
104
104
|
function assignKey(state) {
|
|
@@ -112,6 +112,8 @@ function assignKey(state) {
|
|
|
112
112
|
}
|
|
113
113
|
function createBrowserHistory(opts) {
|
|
114
114
|
const win = (opts == null ? void 0 : opts.window) ?? (typeof document !== "undefined" ? window : void 0);
|
|
115
|
+
const originalPushState = win.history.pushState;
|
|
116
|
+
const originalReplaceState = win.history.replaceState;
|
|
115
117
|
const createHref = (opts == null ? void 0 : opts.createHref) ?? ((path) => path);
|
|
116
118
|
const parseLocation = (opts == null ? void 0 : opts.parseLocation) ?? (() => parseHref(
|
|
117
119
|
`${win.location.pathname}${win.location.search}${win.location.hash}`,
|
|
@@ -121,26 +123,16 @@ function createBrowserHistory(opts) {
|
|
|
121
123
|
let rollbackLocation;
|
|
122
124
|
const getLocation = () => currentLocation;
|
|
123
125
|
let next;
|
|
124
|
-
let tracking = true;
|
|
125
126
|
let scheduled;
|
|
126
|
-
const untrack = (fn) => {
|
|
127
|
-
tracking = false;
|
|
128
|
-
fn();
|
|
129
|
-
tracking = true;
|
|
130
|
-
};
|
|
131
127
|
const flush = () => {
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
next = void 0;
|
|
141
|
-
scheduled = void 0;
|
|
142
|
-
rollbackLocation = void 0;
|
|
143
|
-
});
|
|
128
|
+
if (!next) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
const caller = next.isPush ? originalPushState : originalReplaceState;
|
|
132
|
+
caller.call(win.history, next.state, "", next.href);
|
|
133
|
+
next = void 0;
|
|
134
|
+
scheduled = void 0;
|
|
135
|
+
rollbackLocation = void 0;
|
|
144
136
|
};
|
|
145
137
|
const queueHistoryAction = (type, destHref, state) => {
|
|
146
138
|
const href = createHref(destHref);
|
|
@@ -161,8 +153,6 @@ function createBrowserHistory(opts) {
|
|
|
161
153
|
currentLocation = parseLocation();
|
|
162
154
|
history.notify();
|
|
163
155
|
};
|
|
164
|
-
const originalPushState = win.history.pushState;
|
|
165
|
-
const originalReplaceState = win.history.replaceState;
|
|
166
156
|
const history = createHistory({
|
|
167
157
|
getLocation,
|
|
168
158
|
pushState: (href, state) => queueHistoryAction("push", href, state),
|
|
@@ -189,14 +179,12 @@ function createBrowserHistory(opts) {
|
|
|
189
179
|
win.addEventListener(popStateEvent, onPushPop);
|
|
190
180
|
win.history.pushState = function(...args) {
|
|
191
181
|
const res = originalPushState.apply(win.history, args);
|
|
192
|
-
|
|
193
|
-
history.notify();
|
|
182
|
+
onPushPop();
|
|
194
183
|
return res;
|
|
195
184
|
};
|
|
196
185
|
win.history.replaceState = function(...args) {
|
|
197
186
|
const res = originalReplaceState.apply(win.history, args);
|
|
198
|
-
|
|
199
|
-
history.notify();
|
|
187
|
+
onPushPop();
|
|
200
188
|
return res;
|
|
201
189
|
};
|
|
202
190
|
return history;
|
|
@@ -233,12 +221,15 @@ function createMemoryHistory(opts = {
|
|
|
233
221
|
entries[index] = path;
|
|
234
222
|
},
|
|
235
223
|
back: () => {
|
|
224
|
+
currentState = assignKey(currentState);
|
|
236
225
|
index--;
|
|
237
226
|
},
|
|
238
227
|
forward: () => {
|
|
228
|
+
currentState = assignKey(currentState);
|
|
239
229
|
index = Math.min(index + 1, entries.length - 1);
|
|
240
230
|
},
|
|
241
231
|
go: (n) => {
|
|
232
|
+
currentState = assignKey(currentState);
|
|
242
233
|
index = Math.min(Math.max(index + n, 0), entries.length - 1);
|
|
243
234
|
},
|
|
244
235
|
createHref: (path) => path
|
package/dist/cjs/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../../src/index.ts"],"sourcesContent":["// While the public API was clearly inspired by the \"history\" npm package,\n// This implementation attempts to be more lightweight by\n// making assumptions about the way TanStack Router works\n\nexport interface RouterHistory {\n location: HistoryLocation\n subscribe: (cb: () => void) => () => void\n push: (path: string, state?: any) => void\n replace: (path: string, state?: any) => void\n go: (index: number) => void\n back: () => void\n forward: () => void\n createHref: (href: string) => string\n block: (blocker: BlockerFn) => () => void\n flush: () => void\n destroy: () => void\n notify: () => void\n}\n\nexport interface HistoryLocation extends ParsedPath {\n state: HistoryState\n}\n\nexport interface ParsedPath {\n href: string\n pathname: string\n search: string\n hash: string\n}\n\nexport interface HistoryState {\n key?: string\n}\n\ntype ShouldAllowNavigation = any\n\nexport type BlockerFn = () =>\n | Promise<ShouldAllowNavigation>\n | ShouldAllowNavigation\n\nconst pushStateEvent = 'pushstate'\nconst popStateEvent = 'popstate'\nconst beforeUnloadEvent = 'beforeunload'\n\nconst beforeUnloadListener = (event: Event) => {\n event.preventDefault()\n // @ts-ignore\n return (event.returnValue = '')\n}\n\nconst stopBlocking = () => {\n removeEventListener(beforeUnloadEvent, beforeUnloadListener, {\n capture: true,\n })\n}\n\nexport function createHistory(opts: {\n getLocation: () => HistoryLocation\n pushState: (path: string, state: any) => void\n replaceState: (path: string, state: any) => void\n go: (n: number) => void\n back: () => void\n forward: () => void\n createHref: (path: string) => string\n flush?: () => void\n destroy?: () => void\n onBlocked?: (onUpdate: () => void) => void\n}): RouterHistory {\n let location = opts.getLocation()\n const subscribers = new Set<() => void>()\n let blockers: Array<BlockerFn> = []\n\n const onUpdate = () => {\n location = opts.getLocation()\n subscribers.forEach((subscriber) => subscriber())\n }\n\n const tryNavigation = async (task: () => void) => {\n if (typeof document !== 'undefined' && blockers.length) {\n for (const blocker of blockers) {\n const allowed = await blocker()\n if (!allowed) {\n opts.onBlocked?.(onUpdate)\n return\n }\n }\n }\n\n task()\n }\n\n return {\n get location() {\n return location\n },\n subscribe: (cb: () => void) => {\n subscribers.add(cb)\n\n return () => {\n subscribers.delete(cb)\n }\n },\n push: (path: string, state: any) => {\n state = assignKey(state)\n tryNavigation(() => {\n opts.pushState(path, state)\n onUpdate()\n })\n },\n replace: (path: string, state: any) => {\n state = assignKey(state)\n tryNavigation(() => {\n opts.replaceState(path, state)\n onUpdate()\n })\n },\n go: (index) => {\n tryNavigation(() => {\n opts.go(index)\n onUpdate()\n })\n },\n back: () => {\n tryNavigation(() => {\n opts.back()\n onUpdate()\n })\n },\n forward: () => {\n tryNavigation(() => {\n opts.forward()\n onUpdate()\n })\n },\n createHref: (str) => opts.createHref(str),\n block: (blocker) => {\n blockers.push(blocker)\n\n if (blockers.length === 1) {\n addEventListener(beforeUnloadEvent, beforeUnloadListener, {\n capture: true,\n })\n }\n\n return () => {\n blockers = blockers.filter((b) => b !== blocker)\n\n if (!blockers.length) {\n stopBlocking()\n }\n }\n },\n flush: () => opts.flush?.(),\n destroy: () => opts.destroy?.(),\n notify: onUpdate,\n }\n}\n\nfunction assignKey(state: HistoryState | undefined) {\n if (!state) {\n state = {} as HistoryState\n }\n return {\n ...state,\n key: createRandomKey(),\n }\n}\n\n/**\n * Creates a history object that can be used to interact with the browser's\n * navigation. This is a lightweight API wrapping the browser's native methods.\n * It is designed to work with TanStack Router, but could be used as a standalone API as well.\n * IMPORTANT: This API implements history throttling via a microtask to prevent\n * excessive calls to the history API. In some browsers, calling history.pushState or\n * history.replaceState in quick succession can cause the browser to ignore subsequent\n * calls. This API smooths out those differences and ensures that your application\n * state will *eventually* match the browser state. In most cases, this is not a problem,\n * but if you need to ensure that the browser state is up to date, you can use the\n * `history.flush` method to immediately flush all pending state changes to the browser URL.\n * @param opts\n * @param opts.getHref A function that returns the current href (path + search + hash)\n * @param opts.createHref A function that takes a path and returns a href (path + search + hash)\n * @returns A history instance\n */\nexport function createBrowserHistory(opts?: {\n parseLocation?: () => HistoryLocation\n createHref?: (path: string) => string\n window?: any\n}): RouterHistory {\n const win =\n opts?.window ??\n (typeof document !== 'undefined' ? window : (undefined as any))\n\n const createHref = opts?.createHref ?? ((path) => path)\n const parseLocation =\n opts?.parseLocation ??\n (() =>\n parseHref(\n `${win.location.pathname}${win.location.search}${win.location.hash}`,\n win.history.state,\n ))\n\n let currentLocation = parseLocation()\n let rollbackLocation: HistoryLocation | undefined\n\n const getLocation = () => currentLocation\n\n let next:\n | undefined\n | {\n // This is the latest location that we were attempting to push/replace\n href: string\n // This is the latest state that we were attempting to push/replace\n state: any\n // This is the latest type that we were attempting to push/replace\n isPush: boolean\n }\n\n // Because we are proactively updating the location\n // in memory before actually updating the browser history,\n // we need to track when we are doing this so we don't\n // notify subscribers twice on the last update.\n let tracking = true\n\n // We need to track the current scheduled update to prevent\n // multiple updates from being scheduled at the same time.\n let scheduled: Promise<void> | undefined\n\n // This function is a wrapper to prevent any of the callback's\n // side effects from causing a subscriber notification\n const untrack = (fn: () => void) => {\n tracking = false\n fn()\n tracking = true\n }\n\n // This function flushes the next update to the browser history\n const flush = () => {\n // Do not notify subscribers about this push/replace call\n untrack(() => {\n if (!next) return\n win.history[next.isPush ? 'pushState' : 'replaceState'](\n next.state,\n '',\n next.href,\n )\n // Reset the nextIsPush flag and clear the scheduled update\n next = undefined\n scheduled = undefined\n rollbackLocation = undefined\n })\n }\n\n // This function queues up a call to update the browser history\n const queueHistoryAction = (\n type: 'push' | 'replace',\n destHref: string,\n state: any,\n ) => {\n const href = createHref(destHref)\n\n if (!scheduled) {\n rollbackLocation = currentLocation\n }\n\n // Update the location in memory\n currentLocation = parseHref(destHref, state)\n\n // Keep track of the next location we need to flush to the URL\n next = {\n href,\n state,\n isPush: next?.isPush || type === 'push',\n }\n\n if (!scheduled) {\n // Schedule an update to the browser history\n scheduled = Promise.resolve().then(() => flush())\n }\n }\n\n const onPushPop = () => {\n currentLocation = parseLocation()\n history.notify()\n }\n\n const originalPushState = win.history.pushState\n const originalReplaceState = win.history.replaceState\n\n const history = createHistory({\n getLocation,\n pushState: (href, state) => queueHistoryAction('push', href, state),\n replaceState: (href, state) => queueHistoryAction('replace', href, state),\n back: () => win.history.back(),\n forward: () => win.history.forward(),\n go: (n) => win.history.go(n),\n createHref: (href) => createHref(href),\n flush,\n destroy: () => {\n win.history.pushState = originalPushState\n win.history.replaceState = originalReplaceState\n win.removeEventListener(pushStateEvent, onPushPop)\n win.removeEventListener(popStateEvent, onPushPop)\n },\n onBlocked: (onUpdate) => {\n // If a navigation is blocked, we need to rollback the location\n // that we optimistically updated in memory.\n if (rollbackLocation && currentLocation !== rollbackLocation) {\n currentLocation = rollbackLocation\n // Notify subscribers\n onUpdate()\n }\n },\n })\n\n win.addEventListener(pushStateEvent, onPushPop)\n win.addEventListener(popStateEvent, onPushPop)\n\n win.history.pushState = function (...args: Array<any>) {\n const res = originalPushState.apply(win.history, args)\n if (tracking) history.notify()\n return res\n }\n\n win.history.replaceState = function (...args: Array<any>) {\n const res = originalReplaceState.apply(win.history, args)\n if (tracking) history.notify()\n return res\n }\n\n return history\n}\n\nexport function createHashHistory(opts?: { window?: any }): RouterHistory {\n const win =\n opts?.window ??\n (typeof document !== 'undefined' ? window : (undefined as any))\n return createBrowserHistory({\n window: win,\n parseLocation: () => {\n const hashHref = win.location.hash.split('#').slice(1).join('#') ?? '/'\n return parseHref(hashHref, win.history.state)\n },\n createHref: (href) =>\n `${win.location.pathname}${win.location.search}#${href}`,\n })\n}\n\nexport function createMemoryHistory(\n opts: {\n initialEntries: Array<string>\n initialIndex?: number\n } = {\n initialEntries: ['/'],\n },\n): RouterHistory {\n const entries = opts.initialEntries\n let index = opts.initialIndex ?? entries.length - 1\n let currentState = {\n key: createRandomKey(),\n } as HistoryState\n\n const getLocation = () => parseHref(entries[index]!, currentState)\n\n return createHistory({\n getLocation,\n\n pushState: (path, state) => {\n currentState = state\n entries.push(path)\n index++\n },\n replaceState: (path, state) => {\n currentState = state\n entries[index] = path\n },\n back: () => {\n index--\n },\n forward: () => {\n index = Math.min(index + 1, entries.length - 1)\n },\n go: (n) => {\n index = Math.min(Math.max(index + n, 0), entries.length - 1)\n },\n createHref: (path) => path,\n })\n}\n\nfunction parseHref(\n href: string,\n state: HistoryState | undefined,\n): HistoryLocation {\n const hashIndex = href.indexOf('#')\n const 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":[],"mappings":";;AAwCA,MAAM,iBAAiB;AACvB,MAAM,gBAAgB;AACtB,MAAM,oBAAoB;AAE1B,MAAM,uBAAuB,CAAC,UAAiB;AAC7C,QAAM,eAAe;AAErB,SAAQ,MAAM,cAAc;AAC9B;AAEA,MAAM,eAAe,MAAM;AACzB,sBAAoB,mBAAmB,sBAAsB;AAAA,IAC3D,SAAS;AAAA,EAAA,CACV;AACH;AAEO,SAAS,cAAc,MAWZ;AACZ,MAAA,WAAW,KAAK;AACd,QAAA,kCAAkB;AACxB,MAAI,WAA6B,CAAA;AAEjC,QAAM,WAAW,MAAM;AACrB,eAAW,KAAK;AAChB,gBAAY,QAAQ,CAAC,eAAe,WAAY,CAAA;AAAA,EAAA;AAG5C,QAAA,gBAAgB,OAAO,SAAqB;;AAChD,QAAI,OAAO,aAAa,e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AQ,GAAG;AAC5B,QAAA,cAAc,KAAK,QAAQ,GAAG;AAE7B,SAAA;AAAA,IACL;AAAA,IACA,UAAU,KAAK;AAAA,MACb;AAAA,MACA,YAAY,IACR,cAAc,IACZ,KAAK,IAAI,WAAW,WAAW,IAC/B,YACF,cAAc,IACZ,cACA,KAAK;AAAA,IACb;AAAA,IACA,MAAM,YAAY,KAAK,KAAK,UAAU,SAAS,IAAI;AAAA,IACnD,QACE,cAAc,KACV,KAAK,MAAM,aAAa,cAAc,KAAK,SAAY,SAAS,IAChE;AAAA,IACN,OAAO,SAAS,CAAC;AAAA,EAAA;AAErB;AAGA,SAAS,kBAAkB;AACjB,UAAA,KAAK,WAAW,GAAG,SAAS,EAAE,EAAE,UAAU,CAAC;AACrD;;;;;"}
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../../src/index.ts"],"sourcesContent":["// While the public API was clearly inspired by the \"history\" npm package,\n// This implementation attempts to be more lightweight by\n// making assumptions about the way TanStack Router works\n\nexport interface RouterHistory {\n location: HistoryLocation\n subscribe: (cb: () => void) => () => void\n push: (path: string, state?: any) => void\n replace: (path: string, state?: any) => void\n go: (index: number) => void\n back: () => void\n forward: () => void\n createHref: (href: string) => string\n block: (blocker: BlockerFn) => () => void\n flush: () => void\n destroy: () => void\n notify: () => void\n}\n\nexport interface HistoryLocation extends ParsedPath {\n state: HistoryState\n}\n\nexport interface ParsedPath {\n href: string\n pathname: string\n search: string\n hash: string\n}\n\nexport interface HistoryState {\n key?: string\n}\n\ntype ShouldAllowNavigation = any\n\nexport type BlockerFn = () =>\n | Promise<ShouldAllowNavigation>\n | ShouldAllowNavigation\n\nconst pushStateEvent = 'pushstate'\nconst popStateEvent = 'popstate'\nconst beforeUnloadEvent = 'beforeunload'\n\nconst beforeUnloadListener = (event: Event) => {\n event.preventDefault()\n // @ts-ignore\n return (event.returnValue = '')\n}\n\nconst stopBlocking = () => {\n removeEventListener(beforeUnloadEvent, beforeUnloadListener, {\n capture: true,\n })\n}\n\nexport function createHistory(opts: {\n getLocation: () => HistoryLocation\n pushState: (path: string, state: any) => void\n replaceState: (path: string, state: any) => void\n go: (n: number) => void\n back: () => void\n forward: () => void\n createHref: (path: string) => string\n flush?: () => void\n destroy?: () => void\n onBlocked?: (onUpdate: () => void) => void\n}): RouterHistory {\n let location = opts.getLocation()\n const subscribers = new Set<() => void>()\n let blockers: Array<BlockerFn> = []\n\n const notify = () => {\n location = opts.getLocation()\n subscribers.forEach((subscriber) => subscriber())\n }\n\n const tryNavigation = async (task: () => void) => {\n if (typeof document !== 'undefined' && blockers.length) {\n for (const blocker of blockers) {\n const allowed = await blocker()\n if (!allowed) {\n opts.onBlocked?.(notify)\n return\n }\n }\n }\n\n task()\n }\n\n return {\n get location() {\n return location\n },\n subscribe: (cb: () => void) => {\n subscribers.add(cb)\n\n return () => {\n subscribers.delete(cb)\n }\n },\n push: (path: string, state: any) => {\n state = assignKey(state)\n tryNavigation(() => {\n opts.pushState(path, state)\n notify()\n })\n },\n replace: (path: string, state: any) => {\n state = assignKey(state)\n tryNavigation(() => {\n opts.replaceState(path, state)\n notify()\n })\n },\n go: (index) => {\n tryNavigation(() => {\n opts.go(index)\n notify()\n })\n },\n back: () => {\n tryNavigation(() => {\n opts.back()\n notify()\n })\n },\n forward: () => {\n tryNavigation(() => {\n opts.forward()\n notify()\n })\n },\n createHref: (str) => opts.createHref(str),\n block: (blocker) => {\n blockers.push(blocker)\n\n if (blockers.length === 1) {\n addEventListener(beforeUnloadEvent, beforeUnloadListener, {\n capture: true,\n })\n }\n\n return () => {\n blockers = blockers.filter((b) => b !== blocker)\n\n if (!blockers.length) {\n stopBlocking()\n }\n }\n },\n flush: () => opts.flush?.(),\n destroy: () => opts.destroy?.(),\n notify,\n }\n}\n\nfunction assignKey(state: HistoryState | undefined) {\n if (!state) {\n state = {} as HistoryState\n }\n return {\n ...state,\n key: createRandomKey(),\n }\n}\n\n/**\n * Creates a history object that can be used to interact with the browser's\n * navigation. This is a lightweight API wrapping the browser's native methods.\n * It is designed to work with TanStack Router, but could be used as a standalone API as well.\n * IMPORTANT: This API implements history throttling via a microtask to prevent\n * excessive calls to the history API. In some browsers, calling history.pushState or\n * history.replaceState in quick succession can cause the browser to ignore subsequent\n * calls. This API smooths out those differences and ensures that your application\n * state will *eventually* match the browser state. In most cases, this is not a problem,\n * but if you need to ensure that the browser state is up to date, you can use the\n * `history.flush` method to immediately flush all pending state changes to the browser URL.\n * @param opts\n * @param opts.getHref A function that returns the current href (path + search + hash)\n * @param opts.createHref A function that takes a path and returns a href (path + search + hash)\n * @returns A history instance\n */\nexport function createBrowserHistory(opts?: {\n parseLocation?: () => HistoryLocation\n createHref?: (path: string) => string\n window?: any\n}): RouterHistory {\n const win =\n opts?.window ??\n (typeof document !== 'undefined' ? window : (undefined as any))\n\n const originalPushState = win.history.pushState\n const originalReplaceState = win.history.replaceState\n\n const createHref = opts?.createHref ?? ((path) => path)\n const parseLocation =\n opts?.parseLocation ??\n (() =>\n parseHref(\n `${win.location.pathname}${win.location.search}${win.location.hash}`,\n win.history.state,\n ))\n\n let currentLocation = parseLocation()\n let rollbackLocation: HistoryLocation | undefined\n\n const getLocation = () => currentLocation\n\n let next:\n | undefined\n | {\n // This is the latest location that we were attempting to push/replace\n href: string\n // This is the latest state that we were attempting to push/replace\n state: any\n // This is the latest type that we were attempting to push/replace\n isPush: boolean\n }\n\n // 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 flushes the next update to the browser history\n const flush = () => {\n if (!next) {\n return\n }\n\n // We use the original push/replace calls here to ensure that\n // we do not notify subscribers about this push/replace call\n const caller = next.isPush ? originalPushState : originalReplaceState\n caller.call(win.history, next.state, '', next.href)\n // Reset the nextIsPush flag and clear the scheduled update\n next = undefined\n scheduled = undefined\n rollbackLocation = undefined\n }\n\n // This function queues up a call to update the browser history\n const queueHistoryAction = (\n type: 'push' | 'replace',\n destHref: string,\n state: any,\n ) => {\n const href = createHref(destHref)\n\n if (!scheduled) {\n rollbackLocation = currentLocation\n }\n\n // Update the location in memory\n currentLocation = parseHref(destHref, state)\n\n // Keep track of the next location we need to flush to the URL\n next = {\n href,\n state,\n isPush: next?.isPush || type === 'push',\n }\n\n if (!scheduled) {\n // Schedule an update to the browser history\n scheduled = Promise.resolve().then(() => flush())\n }\n }\n\n const onPushPop = () => {\n currentLocation = parseLocation()\n history.notify()\n }\n\n const history = createHistory({\n getLocation,\n pushState: (href, state) => queueHistoryAction('push', href, state),\n replaceState: (href, state) => queueHistoryAction('replace', href, state),\n back: () => win.history.back(),\n forward: () => win.history.forward(),\n go: (n) => win.history.go(n),\n createHref: (href) => createHref(href),\n flush,\n destroy: () => {\n win.history.pushState = originalPushState\n win.history.replaceState = originalReplaceState\n win.removeEventListener(pushStateEvent, onPushPop)\n win.removeEventListener(popStateEvent, onPushPop)\n },\n onBlocked: (onUpdate) => {\n // If a navigation is blocked, we need to rollback the location\n // that we optimistically updated in memory.\n if (rollbackLocation && currentLocation !== rollbackLocation) {\n currentLocation = rollbackLocation\n // Notify subscribers\n onUpdate()\n }\n },\n })\n\n win.addEventListener(pushStateEvent, onPushPop)\n win.addEventListener(popStateEvent, onPushPop)\n\n win.history.pushState = function (...args: Array<any>) {\n const res = originalPushState.apply(win.history, args)\n onPushPop()\n return res\n }\n\n win.history.replaceState = function (...args: Array<any>) {\n const res = originalReplaceState.apply(win.history, args)\n onPushPop()\n return res\n }\n\n return history\n}\n\nexport function createHashHistory(opts?: { window?: any }): RouterHistory {\n const win =\n opts?.window ??\n (typeof document !== 'undefined' ? window : (undefined as any))\n return createBrowserHistory({\n window: win,\n parseLocation: () => {\n const hashHref = win.location.hash.split('#').slice(1).join('#') ?? '/'\n return parseHref(hashHref, win.history.state)\n },\n createHref: (href) =>\n `${win.location.pathname}${win.location.search}#${href}`,\n })\n}\n\nexport function createMemoryHistory(\n opts: {\n initialEntries: Array<string>\n initialIndex?: number\n } = {\n initialEntries: ['/'],\n },\n): RouterHistory {\n const entries = opts.initialEntries\n let index = opts.initialIndex ?? entries.length - 1\n let currentState = {\n key: createRandomKey(),\n } as HistoryState\n\n const getLocation = () => parseHref(entries[index]!, currentState)\n\n return createHistory({\n getLocation,\n\n pushState: (path, state) => {\n currentState = state\n entries.push(path)\n index++\n },\n replaceState: (path, state) => {\n currentState = state\n entries[index] = path\n },\n back: () => {\n currentState = assignKey(currentState)\n index--\n },\n forward: () => {\n currentState = assignKey(currentState)\n index = Math.min(index + 1, entries.length - 1)\n },\n go: (n) => {\n currentState = assignKey(currentState)\n index = Math.min(Math.max(index + n, 0), entries.length - 1)\n },\n createHref: (path) => path,\n })\n}\n\nfunction parseHref(\n href: string,\n state: HistoryState | undefined,\n): HistoryLocation {\n const hashIndex = href.indexOf('#')\n const 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":[],"mappings":";;AAwCA,MAAM,iBAAiB;AACvB,MAAM,gBAAgB;AACtB,MAAM,oBAAoB;AAE1B,MAAM,uBAAuB,CAAC,UAAiB;AAC7C,QAAM,eAAe;AAErB,SAAQ,MAAM,cAAc;AAC9B;AAEA,MAAM,eAAe,MAAM;AACzB,sBAAoB,mBAAmB,sBAAsB;AAAA,IAC3D,SAAS;AAAA,EAAA,CACV;AACH;AAEO,SAAS,cAAc,MAWZ;AACZ,MAAA,WAAW,KAAK;AACd,QAAA,kCAAkB;AACxB,MAAI,WAA6B,CAAA;AAEjC,QAAM,SAAS,MAAM;AACnB,eAAW,KAAK;AAChB,gBAAY,QAAQ,CA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QAAQ,GAAG;AAC5B,QAAA,cAAc,KAAK,QAAQ,GAAG;AAE7B,SAAA;AAAA,IACL;AAAA,IACA,UAAU,KAAK;AAAA,MACb;AAAA,MACA,YAAY,IACR,cAAc,IACZ,KAAK,IAAI,WAAW,WAAW,IAC/B,YACF,cAAc,IACZ,cACA,KAAK;AAAA,IACb;AAAA,IACA,MAAM,YAAY,KAAK,KAAK,UAAU,SAAS,IAAI;AAAA,IACnD,QACE,cAAc,KACV,KAAK,MAAM,aAAa,cAAc,KAAK,SAAY,SAAS,IAChE;AAAA,IACN,OAAO,SAAS,CAAC;AAAA,EAAA;AAErB;AAGA,SAAS,kBAAkB;AACjB,UAAA,KAAK,WAAW,GAAG,SAAS,EAAE,EAAE,UAAU,CAAC;AACrD;;;;;"}
|
package/dist/esm/index.js
CHANGED
|
@@ -14,7 +14,7 @@ function createHistory(opts) {
|
|
|
14
14
|
let location = opts.getLocation();
|
|
15
15
|
const subscribers = /* @__PURE__ */ new Set();
|
|
16
16
|
let blockers = [];
|
|
17
|
-
const
|
|
17
|
+
const notify = () => {
|
|
18
18
|
location = opts.getLocation();
|
|
19
19
|
subscribers.forEach((subscriber) => subscriber());
|
|
20
20
|
};
|
|
@@ -24,7 +24,7 @@ function createHistory(opts) {
|
|
|
24
24
|
for (const blocker of blockers) {
|
|
25
25
|
const allowed = await blocker();
|
|
26
26
|
if (!allowed) {
|
|
27
|
-
(_a = opts.onBlocked) == null ? void 0 : _a.call(opts,
|
|
27
|
+
(_a = opts.onBlocked) == null ? void 0 : _a.call(opts, notify);
|
|
28
28
|
return;
|
|
29
29
|
}
|
|
30
30
|
}
|
|
@@ -45,32 +45,32 @@ function createHistory(opts) {
|
|
|
45
45
|
state = assignKey(state);
|
|
46
46
|
tryNavigation(() => {
|
|
47
47
|
opts.pushState(path, state);
|
|
48
|
-
|
|
48
|
+
notify();
|
|
49
49
|
});
|
|
50
50
|
},
|
|
51
51
|
replace: (path, state) => {
|
|
52
52
|
state = assignKey(state);
|
|
53
53
|
tryNavigation(() => {
|
|
54
54
|
opts.replaceState(path, state);
|
|
55
|
-
|
|
55
|
+
notify();
|
|
56
56
|
});
|
|
57
57
|
},
|
|
58
58
|
go: (index) => {
|
|
59
59
|
tryNavigation(() => {
|
|
60
60
|
opts.go(index);
|
|
61
|
-
|
|
61
|
+
notify();
|
|
62
62
|
});
|
|
63
63
|
},
|
|
64
64
|
back: () => {
|
|
65
65
|
tryNavigation(() => {
|
|
66
66
|
opts.back();
|
|
67
|
-
|
|
67
|
+
notify();
|
|
68
68
|
});
|
|
69
69
|
},
|
|
70
70
|
forward: () => {
|
|
71
71
|
tryNavigation(() => {
|
|
72
72
|
opts.forward();
|
|
73
|
-
|
|
73
|
+
notify();
|
|
74
74
|
});
|
|
75
75
|
},
|
|
76
76
|
createHref: (str) => opts.createHref(str),
|
|
@@ -96,7 +96,7 @@ function createHistory(opts) {
|
|
|
96
96
|
var _a;
|
|
97
97
|
return (_a = opts.destroy) == null ? void 0 : _a.call(opts);
|
|
98
98
|
},
|
|
99
|
-
notify
|
|
99
|
+
notify
|
|
100
100
|
};
|
|
101
101
|
}
|
|
102
102
|
function assignKey(state) {
|
|
@@ -110,6 +110,8 @@ function assignKey(state) {
|
|
|
110
110
|
}
|
|
111
111
|
function createBrowserHistory(opts) {
|
|
112
112
|
const win = (opts == null ? void 0 : opts.window) ?? (typeof document !== "undefined" ? window : void 0);
|
|
113
|
+
const originalPushState = win.history.pushState;
|
|
114
|
+
const originalReplaceState = win.history.replaceState;
|
|
113
115
|
const createHref = (opts == null ? void 0 : opts.createHref) ?? ((path) => path);
|
|
114
116
|
const parseLocation = (opts == null ? void 0 : opts.parseLocation) ?? (() => parseHref(
|
|
115
117
|
`${win.location.pathname}${win.location.search}${win.location.hash}`,
|
|
@@ -119,26 +121,16 @@ function createBrowserHistory(opts) {
|
|
|
119
121
|
let rollbackLocation;
|
|
120
122
|
const getLocation = () => currentLocation;
|
|
121
123
|
let next;
|
|
122
|
-
let tracking = true;
|
|
123
124
|
let scheduled;
|
|
124
|
-
const untrack = (fn) => {
|
|
125
|
-
tracking = false;
|
|
126
|
-
fn();
|
|
127
|
-
tracking = true;
|
|
128
|
-
};
|
|
129
125
|
const flush = () => {
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
next = void 0;
|
|
139
|
-
scheduled = void 0;
|
|
140
|
-
rollbackLocation = void 0;
|
|
141
|
-
});
|
|
126
|
+
if (!next) {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
const caller = next.isPush ? originalPushState : originalReplaceState;
|
|
130
|
+
caller.call(win.history, next.state, "", next.href);
|
|
131
|
+
next = void 0;
|
|
132
|
+
scheduled = void 0;
|
|
133
|
+
rollbackLocation = void 0;
|
|
142
134
|
};
|
|
143
135
|
const queueHistoryAction = (type, destHref, state) => {
|
|
144
136
|
const href = createHref(destHref);
|
|
@@ -159,8 +151,6 @@ function createBrowserHistory(opts) {
|
|
|
159
151
|
currentLocation = parseLocation();
|
|
160
152
|
history.notify();
|
|
161
153
|
};
|
|
162
|
-
const originalPushState = win.history.pushState;
|
|
163
|
-
const originalReplaceState = win.history.replaceState;
|
|
164
154
|
const history = createHistory({
|
|
165
155
|
getLocation,
|
|
166
156
|
pushState: (href, state) => queueHistoryAction("push", href, state),
|
|
@@ -187,14 +177,12 @@ function createBrowserHistory(opts) {
|
|
|
187
177
|
win.addEventListener(popStateEvent, onPushPop);
|
|
188
178
|
win.history.pushState = function(...args) {
|
|
189
179
|
const res = originalPushState.apply(win.history, args);
|
|
190
|
-
|
|
191
|
-
history.notify();
|
|
180
|
+
onPushPop();
|
|
192
181
|
return res;
|
|
193
182
|
};
|
|
194
183
|
win.history.replaceState = function(...args) {
|
|
195
184
|
const res = originalReplaceState.apply(win.history, args);
|
|
196
|
-
|
|
197
|
-
history.notify();
|
|
185
|
+
onPushPop();
|
|
198
186
|
return res;
|
|
199
187
|
};
|
|
200
188
|
return history;
|
|
@@ -231,12 +219,15 @@ function createMemoryHistory(opts = {
|
|
|
231
219
|
entries[index] = path;
|
|
232
220
|
},
|
|
233
221
|
back: () => {
|
|
222
|
+
currentState = assignKey(currentState);
|
|
234
223
|
index--;
|
|
235
224
|
},
|
|
236
225
|
forward: () => {
|
|
226
|
+
currentState = assignKey(currentState);
|
|
237
227
|
index = Math.min(index + 1, entries.length - 1);
|
|
238
228
|
},
|
|
239
229
|
go: (n) => {
|
|
230
|
+
currentState = assignKey(currentState);
|
|
240
231
|
index = Math.min(Math.max(index + n, 0), entries.length - 1);
|
|
241
232
|
},
|
|
242
233
|
createHref: (path) => path
|
package/dist/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: (blocker: BlockerFn) => () => void\n flush: () => void\n destroy: () => void\n notify: () => void\n}\n\nexport interface HistoryLocation extends ParsedPath {\n state: HistoryState\n}\n\nexport interface ParsedPath {\n href: string\n pathname: string\n search: string\n hash: string\n}\n\nexport interface HistoryState {\n key?: string\n}\n\ntype ShouldAllowNavigation = any\n\nexport type BlockerFn = () =>\n | Promise<ShouldAllowNavigation>\n | ShouldAllowNavigation\n\nconst pushStateEvent = 'pushstate'\nconst popStateEvent = 'popstate'\nconst beforeUnloadEvent = 'beforeunload'\n\nconst beforeUnloadListener = (event: Event) => {\n event.preventDefault()\n // @ts-ignore\n return (event.returnValue = '')\n}\n\nconst stopBlocking = () => {\n removeEventListener(beforeUnloadEvent, beforeUnloadListener, {\n capture: true,\n })\n}\n\nexport function createHistory(opts: {\n getLocation: () => HistoryLocation\n pushState: (path: string, state: any) => void\n replaceState: (path: string, state: any) => void\n go: (n: number) => void\n back: () => void\n forward: () => void\n createHref: (path: string) => string\n flush?: () => void\n destroy?: () => void\n onBlocked?: (onUpdate: () => void) => void\n}): RouterHistory {\n let location = opts.getLocation()\n const subscribers = new Set<() => void>()\n let blockers: Array<BlockerFn> = []\n\n const onUpdate = () => {\n location = opts.getLocation()\n subscribers.forEach((subscriber) => subscriber())\n }\n\n const tryNavigation = async (task: () => void) => {\n if (typeof document !== 'undefined' && blockers.length) {\n for (const blocker of blockers) {\n const allowed = await blocker()\n if (!allowed) {\n opts.onBlocked?.(onUpdate)\n return\n }\n }\n }\n\n task()\n }\n\n return {\n get location() {\n return location\n },\n subscribe: (cb: () => void) => {\n subscribers.add(cb)\n\n return () => {\n subscribers.delete(cb)\n }\n },\n push: (path: string, state: any) => {\n state = assignKey(state)\n tryNavigation(() => {\n opts.pushState(path, state)\n onUpdate()\n })\n },\n replace: (path: string, state: any) => {\n state = assignKey(state)\n tryNavigation(() => {\n opts.replaceState(path, state)\n onUpdate()\n })\n },\n go: (index) => {\n tryNavigation(() => {\n opts.go(index)\n onUpdate()\n })\n },\n back: () => {\n tryNavigation(() => {\n opts.back()\n onUpdate()\n })\n },\n forward: () => {\n tryNavigation(() => {\n opts.forward()\n onUpdate()\n })\n },\n createHref: (str) => opts.createHref(str),\n block: (blocker) => {\n blockers.push(blocker)\n\n if (blockers.length === 1) {\n addEventListener(beforeUnloadEvent, beforeUnloadListener, {\n capture: true,\n })\n }\n\n return () => {\n blockers = blockers.filter((b) => b !== blocker)\n\n if (!blockers.length) {\n stopBlocking()\n }\n }\n },\n flush: () => opts.flush?.(),\n destroy: () => opts.destroy?.(),\n notify: onUpdate,\n }\n}\n\nfunction assignKey(state: HistoryState | undefined) {\n if (!state) {\n state = {} as HistoryState\n }\n return {\n ...state,\n key: createRandomKey(),\n }\n}\n\n/**\n * Creates a history object that can be used to interact with the browser's\n * navigation. This is a lightweight API wrapping the browser's native methods.\n * It is designed to work with TanStack Router, but could be used as a standalone API as well.\n * IMPORTANT: This API implements history throttling via a microtask to prevent\n * excessive calls to the history API. In some browsers, calling history.pushState or\n * history.replaceState in quick succession can cause the browser to ignore subsequent\n * calls. This API smooths out those differences and ensures that your application\n * state will *eventually* match the browser state. In most cases, this is not a problem,\n * but if you need to ensure that the browser state is up to date, you can use the\n * `history.flush` method to immediately flush all pending state changes to the browser URL.\n * @param opts\n * @param opts.getHref A function that returns the current href (path + search + hash)\n * @param opts.createHref A function that takes a path and returns a href (path + search + hash)\n * @returns A history instance\n */\nexport function createBrowserHistory(opts?: {\n parseLocation?: () => HistoryLocation\n createHref?: (path: string) => string\n window?: any\n}): RouterHistory {\n const win =\n opts?.window ??\n (typeof document !== 'undefined' ? window : (undefined as any))\n\n const createHref = opts?.createHref ?? ((path) => path)\n const parseLocation =\n opts?.parseLocation ??\n (() =>\n parseHref(\n `${win.location.pathname}${win.location.search}${win.location.hash}`,\n win.history.state,\n ))\n\n let currentLocation = parseLocation()\n let rollbackLocation: HistoryLocation | undefined\n\n const getLocation = () => currentLocation\n\n let next:\n | undefined\n | {\n // This is the latest location that we were attempting to push/replace\n href: string\n // This is the latest state that we were attempting to push/replace\n state: any\n // This is the latest type that we were attempting to push/replace\n isPush: boolean\n }\n\n // Because we are proactively updating the location\n // in memory before actually updating the browser history,\n // we need to track when we are doing this so we don't\n // notify subscribers twice on the last update.\n let tracking = true\n\n // We need to track the current scheduled update to prevent\n // multiple updates from being scheduled at the same time.\n let scheduled: Promise<void> | undefined\n\n // This function is a wrapper to prevent any of the callback's\n // side effects from causing a subscriber notification\n const untrack = (fn: () => void) => {\n tracking = false\n fn()\n tracking = true\n }\n\n // This function flushes the next update to the browser history\n const flush = () => {\n // Do not notify subscribers about this push/replace call\n untrack(() => {\n if (!next) return\n win.history[next.isPush ? 'pushState' : 'replaceState'](\n next.state,\n '',\n next.href,\n )\n // Reset the nextIsPush flag and clear the scheduled update\n next = undefined\n scheduled = undefined\n rollbackLocation = undefined\n })\n }\n\n // This function queues up a call to update the browser history\n const queueHistoryAction = (\n type: 'push' | 'replace',\n destHref: string,\n state: any,\n ) => {\n const href = createHref(destHref)\n\n if (!scheduled) {\n rollbackLocation = currentLocation\n }\n\n // Update the location in memory\n currentLocation = parseHref(destHref, state)\n\n // Keep track of the next location we need to flush to the URL\n next = {\n href,\n state,\n isPush: next?.isPush || type === 'push',\n }\n\n if (!scheduled) {\n // Schedule an update to the browser history\n scheduled = Promise.resolve().then(() => flush())\n }\n }\n\n const onPushPop = () => {\n currentLocation = parseLocation()\n history.notify()\n }\n\n const originalPushState = win.history.pushState\n const originalReplaceState = win.history.replaceState\n\n const history = createHistory({\n getLocation,\n pushState: (href, state) => queueHistoryAction('push', href, state),\n replaceState: (href, state) => queueHistoryAction('replace', href, state),\n back: () => win.history.back(),\n forward: () => win.history.forward(),\n go: (n) => win.history.go(n),\n createHref: (href) => createHref(href),\n flush,\n destroy: () => {\n win.history.pushState = originalPushState\n win.history.replaceState = originalReplaceState\n win.removeEventListener(pushStateEvent, onPushPop)\n win.removeEventListener(popStateEvent, onPushPop)\n },\n onBlocked: (onUpdate) => {\n // If a navigation is blocked, we need to rollback the location\n // that we optimistically updated in memory.\n if (rollbackLocation && currentLocation !== rollbackLocation) {\n currentLocation = rollbackLocation\n // Notify subscribers\n onUpdate()\n }\n },\n })\n\n win.addEventListener(pushStateEvent, onPushPop)\n win.addEventListener(popStateEvent, onPushPop)\n\n win.history.pushState = function (...args: Array<any>) {\n const res = originalPushState.apply(win.history, args)\n if (tracking) history.notify()\n return res\n }\n\n win.history.replaceState = function (...args: Array<any>) {\n const res = originalReplaceState.apply(win.history, args)\n if (tracking) history.notify()\n return res\n }\n\n return history\n}\n\nexport function createHashHistory(opts?: { window?: any }): RouterHistory {\n const win =\n opts?.window ??\n (typeof document !== 'undefined' ? window : (undefined as any))\n return createBrowserHistory({\n window: win,\n parseLocation: () => {\n const hashHref = win.location.hash.split('#').slice(1).join('#') ?? '/'\n return parseHref(hashHref, win.history.state)\n },\n createHref: (href) =>\n `${win.location.pathname}${win.location.search}#${href}`,\n })\n}\n\nexport function createMemoryHistory(\n opts: {\n initialEntries: Array<string>\n initialIndex?: number\n } = {\n initialEntries: ['/'],\n },\n): RouterHistory {\n const entries = opts.initialEntries\n let index = opts.initialIndex ?? entries.length - 1\n let currentState = {\n key: createRandomKey(),\n } as HistoryState\n\n const getLocation = () => parseHref(entries[index]!, currentState)\n\n return createHistory({\n getLocation,\n\n pushState: (path, state) => {\n currentState = state\n entries.push(path)\n index++\n },\n replaceState: (path, state) => {\n currentState = state\n entries[index] = path\n },\n back: () => {\n index--\n },\n forward: () => {\n index = Math.min(index + 1, entries.length - 1)\n },\n go: (n) => {\n index = Math.min(Math.max(index + n, 0), entries.length - 1)\n },\n createHref: (path) => path,\n })\n}\n\nfunction parseHref(\n href: string,\n state: HistoryState | undefined,\n): HistoryLocation {\n const hashIndex = href.indexOf('#')\n const 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":[],"mappings":"AAwCA,MAAM,iBAAiB;AACvB,MAAM,gBAAgB;AACtB,MAAM,oBAAoB;AAE1B,MAAM,uBAAuB,CAAC,UAAiB;AAC7C,QAAM,eAAe;AAErB,SAAQ,MAAM,cAAc;AAC9B;AAEA,MAAM,eAAe,MAAM;AACzB,sBAAoB,mBAAmB,sBAAsB;AAAA,IAC3D,SAAS;AAAA,EAAA,CACV;AACH;AAEO,SAAS,cAAc,MA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,QAAQ,SAAS,CAAC;AAAA,IAChD;AAAA,IACA,IAAI,CAAC,MAAM;AACD,cAAA,KAAK,IAAI,KAAK,IAAI,QAAQ,GAAG,CAAC,GAAG,QAAQ,SAAS,CAAC;AAAA,IAC7D;AAAA,IACA,YAAY,CAAC,SAAS;AAAA,EAAA,CACvB;AACH;AAEA,SAAS,UACP,MACA,OACiB;AACX,QAAA,YAAY,KAAK,QAAQ,GAAG;AAC5B,QAAA,cAAc,KAAK,QAAQ,GAAG;AAE7B,SAAA;AAAA,IACL;AAAA,IACA,UAAU,KAAK;AAAA,MACb;AAAA,MACA,YAAY,IACR,cAAc,IACZ,KAAK,IAAI,WAAW,WAAW,IAC/B,YACF,cAAc,IACZ,cACA,KAAK;AAAA,IACb;AAAA,IACA,MAAM,YAAY,KAAK,KAAK,UAAU,SAAS,IAAI;AAAA,IACnD,QACE,cAAc,KACV,KAAK,MAAM,aAAa,cAAc,KAAK,SAAY,SAAS,IAChE;AAAA,IACN,OAAO,SAAS,CAAC;AAAA,EAAA;AAErB;AAGA,SAAS,kBAAkB;AACjB,UAAA,KAAK,WAAW,GAAG,SAAS,EAAE,EAAE,UAAU,CAAC;AACrD;"}
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../src/index.ts"],"sourcesContent":["// While the public API was clearly inspired by the \"history\" npm package,\n// This implementation attempts to be more lightweight by\n// making assumptions about the way TanStack Router works\n\nexport interface RouterHistory {\n location: HistoryLocation\n subscribe: (cb: () => void) => () => void\n push: (path: string, state?: any) => void\n replace: (path: string, state?: any) => void\n go: (index: number) => void\n back: () => void\n forward: () => void\n createHref: (href: string) => string\n block: (blocker: BlockerFn) => () => void\n flush: () => void\n destroy: () => void\n notify: () => void\n}\n\nexport interface HistoryLocation extends ParsedPath {\n state: HistoryState\n}\n\nexport interface ParsedPath {\n href: string\n pathname: string\n search: string\n hash: string\n}\n\nexport interface HistoryState {\n key?: string\n}\n\ntype ShouldAllowNavigation = any\n\nexport type BlockerFn = () =>\n | Promise<ShouldAllowNavigation>\n | ShouldAllowNavigation\n\nconst pushStateEvent = 'pushstate'\nconst popStateEvent = 'popstate'\nconst beforeUnloadEvent = 'beforeunload'\n\nconst beforeUnloadListener = (event: Event) => {\n event.preventDefault()\n // @ts-ignore\n return (event.returnValue = '')\n}\n\nconst stopBlocking = () => {\n removeEventListener(beforeUnloadEvent, beforeUnloadListener, {\n capture: true,\n })\n}\n\nexport function createHistory(opts: {\n getLocation: () => HistoryLocation\n pushState: (path: string, state: any) => void\n replaceState: (path: string, state: any) => void\n go: (n: number) => void\n back: () => void\n forward: () => void\n createHref: (path: string) => string\n flush?: () => void\n destroy?: () => void\n onBlocked?: (onUpdate: () => void) => void\n}): RouterHistory {\n let location = opts.getLocation()\n const subscribers = new Set<() => void>()\n let blockers: Array<BlockerFn> = []\n\n const notify = () => {\n location = opts.getLocation()\n subscribers.forEach((subscriber) => subscriber())\n }\n\n const tryNavigation = async (task: () => void) => {\n if (typeof document !== 'undefined' && blockers.length) {\n for (const blocker of blockers) {\n const allowed = await blocker()\n if (!allowed) {\n opts.onBlocked?.(notify)\n return\n }\n }\n }\n\n task()\n }\n\n return {\n get location() {\n return location\n },\n subscribe: (cb: () => void) => {\n subscribers.add(cb)\n\n return () => {\n subscribers.delete(cb)\n }\n },\n push: (path: string, state: any) => {\n state = assignKey(state)\n tryNavigation(() => {\n opts.pushState(path, state)\n notify()\n })\n },\n replace: (path: string, state: any) => {\n state = assignKey(state)\n tryNavigation(() => {\n opts.replaceState(path, state)\n notify()\n })\n },\n go: (index) => {\n tryNavigation(() => {\n opts.go(index)\n notify()\n })\n },\n back: () => {\n tryNavigation(() => {\n opts.back()\n notify()\n })\n },\n forward: () => {\n tryNavigation(() => {\n opts.forward()\n notify()\n })\n },\n createHref: (str) => opts.createHref(str),\n block: (blocker) => {\n blockers.push(blocker)\n\n if (blockers.length === 1) {\n addEventListener(beforeUnloadEvent, beforeUnloadListener, {\n capture: true,\n })\n }\n\n return () => {\n blockers = blockers.filter((b) => b !== blocker)\n\n if (!blockers.length) {\n stopBlocking()\n }\n }\n },\n flush: () => opts.flush?.(),\n destroy: () => opts.destroy?.(),\n notify,\n }\n}\n\nfunction assignKey(state: HistoryState | undefined) {\n if (!state) {\n state = {} as HistoryState\n }\n return {\n ...state,\n key: createRandomKey(),\n }\n}\n\n/**\n * Creates a history object that can be used to interact with the browser's\n * navigation. This is a lightweight API wrapping the browser's native methods.\n * It is designed to work with TanStack Router, but could be used as a standalone API as well.\n * IMPORTANT: This API implements history throttling via a microtask to prevent\n * excessive calls to the history API. In some browsers, calling history.pushState or\n * history.replaceState in quick succession can cause the browser to ignore subsequent\n * calls. This API smooths out those differences and ensures that your application\n * state will *eventually* match the browser state. In most cases, this is not a problem,\n * but if you need to ensure that the browser state is up to date, you can use the\n * `history.flush` method to immediately flush all pending state changes to the browser URL.\n * @param opts\n * @param opts.getHref A function that returns the current href (path + search + hash)\n * @param opts.createHref A function that takes a path and returns a href (path + search + hash)\n * @returns A history instance\n */\nexport function createBrowserHistory(opts?: {\n parseLocation?: () => HistoryLocation\n createHref?: (path: string) => string\n window?: any\n}): RouterHistory {\n const win =\n opts?.window ??\n (typeof document !== 'undefined' ? window : (undefined as any))\n\n const originalPushState = win.history.pushState\n const originalReplaceState = win.history.replaceState\n\n const createHref = opts?.createHref ?? ((path) => path)\n const parseLocation =\n opts?.parseLocation ??\n (() =>\n parseHref(\n `${win.location.pathname}${win.location.search}${win.location.hash}`,\n win.history.state,\n ))\n\n let currentLocation = parseLocation()\n let rollbackLocation: HistoryLocation | undefined\n\n const getLocation = () => currentLocation\n\n let next:\n | undefined\n | {\n // This is the latest location that we were attempting to push/replace\n href: string\n // This is the latest state that we were attempting to push/replace\n state: any\n // This is the latest type that we were attempting to push/replace\n isPush: boolean\n }\n\n // 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 flushes the next update to the browser history\n const flush = () => {\n if (!next) {\n return\n }\n\n // We use the original push/replace calls here to ensure that\n // we do not notify subscribers about this push/replace call\n const caller = next.isPush ? originalPushState : originalReplaceState\n caller.call(win.history, next.state, '', next.href)\n // Reset the nextIsPush flag and clear the scheduled update\n next = undefined\n scheduled = undefined\n rollbackLocation = undefined\n }\n\n // This function queues up a call to update the browser history\n const queueHistoryAction = (\n type: 'push' | 'replace',\n destHref: string,\n state: any,\n ) => {\n const href = createHref(destHref)\n\n if (!scheduled) {\n rollbackLocation = currentLocation\n }\n\n // Update the location in memory\n currentLocation = parseHref(destHref, state)\n\n // Keep track of the next location we need to flush to the URL\n next = {\n href,\n state,\n isPush: next?.isPush || type === 'push',\n }\n\n if (!scheduled) {\n // Schedule an update to the browser history\n scheduled = Promise.resolve().then(() => flush())\n }\n }\n\n const onPushPop = () => {\n currentLocation = parseLocation()\n history.notify()\n }\n\n const history = createHistory({\n getLocation,\n pushState: (href, state) => queueHistoryAction('push', href, state),\n replaceState: (href, state) => queueHistoryAction('replace', href, state),\n back: () => win.history.back(),\n forward: () => win.history.forward(),\n go: (n) => win.history.go(n),\n createHref: (href) => createHref(href),\n flush,\n destroy: () => {\n win.history.pushState = originalPushState\n win.history.replaceState = originalReplaceState\n win.removeEventListener(pushStateEvent, onPushPop)\n win.removeEventListener(popStateEvent, onPushPop)\n },\n onBlocked: (onUpdate) => {\n // If a navigation is blocked, we need to rollback the location\n // that we optimistically updated in memory.\n if (rollbackLocation && currentLocation !== rollbackLocation) {\n currentLocation = rollbackLocation\n // Notify subscribers\n onUpdate()\n }\n },\n })\n\n win.addEventListener(pushStateEvent, onPushPop)\n win.addEventListener(popStateEvent, onPushPop)\n\n win.history.pushState = function (...args: Array<any>) {\n const res = originalPushState.apply(win.history, args)\n onPushPop()\n return res\n }\n\n win.history.replaceState = function (...args: Array<any>) {\n const res = originalReplaceState.apply(win.history, args)\n onPushPop()\n return res\n }\n\n return history\n}\n\nexport function createHashHistory(opts?: { window?: any }): RouterHistory {\n const win =\n opts?.window ??\n (typeof document !== 'undefined' ? window : (undefined as any))\n return createBrowserHistory({\n window: win,\n parseLocation: () => {\n const hashHref = win.location.hash.split('#').slice(1).join('#') ?? '/'\n return parseHref(hashHref, win.history.state)\n },\n createHref: (href) =>\n `${win.location.pathname}${win.location.search}#${href}`,\n })\n}\n\nexport function createMemoryHistory(\n opts: {\n initialEntries: Array<string>\n initialIndex?: number\n } = {\n initialEntries: ['/'],\n },\n): RouterHistory {\n const entries = opts.initialEntries\n let index = opts.initialIndex ?? entries.length - 1\n let currentState = {\n key: createRandomKey(),\n } as HistoryState\n\n const getLocation = () => parseHref(entries[index]!, currentState)\n\n return createHistory({\n getLocation,\n\n pushState: (path, state) => {\n currentState = state\n entries.push(path)\n index++\n },\n replaceState: (path, state) => {\n currentState = state\n entries[index] = path\n },\n back: () => {\n currentState = assignKey(currentState)\n index--\n },\n forward: () => {\n currentState = assignKey(currentState)\n index = Math.min(index + 1, entries.length - 1)\n },\n go: (n) => {\n currentState = assignKey(currentState)\n index = Math.min(Math.max(index + n, 0), entries.length - 1)\n },\n createHref: (path) => path,\n })\n}\n\nfunction parseHref(\n href: string,\n state: HistoryState | undefined,\n): HistoryLocation {\n const hashIndex = href.indexOf('#')\n const 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":[],"mappings":"AAwCA,MAAM,iBAAiB;AACvB,MAAM,gBAAgB;AACtB,MAAM,oBAAoB;AAE1B,MAAM,uBAAuB,CAAC,UAAiB;AAC7C,QAAM,eAAe;AAErB,SAAQ,MAAM,cAAc;AAC9B;AAEA,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SAAS,CAAC;AAAA,IAChD;AAAA,IACA,IAAI,CAAC,MAAM;AACT,qBAAe,UAAU,YAAY;AAC7B,cAAA,KAAK,IAAI,KAAK,IAAI,QAAQ,GAAG,CAAC,GAAG,QAAQ,SAAS,CAAC;AAAA,IAC7D;AAAA,IACA,YAAY,CAAC,SAAS;AAAA,EAAA,CACvB;AACH;AAEA,SAAS,UACP,MACA,OACiB;AACX,QAAA,YAAY,KAAK,QAAQ,GAAG;AAC5B,QAAA,cAAc,KAAK,QAAQ,GAAG;AAE7B,SAAA;AAAA,IACL;AAAA,IACA,UAAU,KAAK;AAAA,MACb;AAAA,MACA,YAAY,IACR,cAAc,IACZ,KAAK,IAAI,WAAW,WAAW,IAC/B,YACF,cAAc,IACZ,cACA,KAAK;AAAA,IACb;AAAA,IACA,MAAM,YAAY,KAAK,KAAK,UAAU,SAAS,IAAI;AAAA,IACnD,QACE,cAAc,KACV,KAAK,MAAM,aAAa,cAAc,KAAK,SAAY,SAAS,IAChE;AAAA,IACN,OAAO,SAAS,CAAC;AAAA,EAAA;AAErB;AAGA,SAAS,kBAAkB;AACjB,UAAA,KAAK,WAAW,GAAG,SAAS,EAAE,EAAE,UAAU,CAAC;AACrD;"}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -70,7 +70,7 @@ export function createHistory(opts: {
|
|
|
70
70
|
const subscribers = new Set<() => void>()
|
|
71
71
|
let blockers: Array<BlockerFn> = []
|
|
72
72
|
|
|
73
|
-
const
|
|
73
|
+
const notify = () => {
|
|
74
74
|
location = opts.getLocation()
|
|
75
75
|
subscribers.forEach((subscriber) => subscriber())
|
|
76
76
|
}
|
|
@@ -80,7 +80,7 @@ export function createHistory(opts: {
|
|
|
80
80
|
for (const blocker of blockers) {
|
|
81
81
|
const allowed = await blocker()
|
|
82
82
|
if (!allowed) {
|
|
83
|
-
opts.onBlocked?.(
|
|
83
|
+
opts.onBlocked?.(notify)
|
|
84
84
|
return
|
|
85
85
|
}
|
|
86
86
|
}
|
|
@@ -104,32 +104,32 @@ export function createHistory(opts: {
|
|
|
104
104
|
state = assignKey(state)
|
|
105
105
|
tryNavigation(() => {
|
|
106
106
|
opts.pushState(path, state)
|
|
107
|
-
|
|
107
|
+
notify()
|
|
108
108
|
})
|
|
109
109
|
},
|
|
110
110
|
replace: (path: string, state: any) => {
|
|
111
111
|
state = assignKey(state)
|
|
112
112
|
tryNavigation(() => {
|
|
113
113
|
opts.replaceState(path, state)
|
|
114
|
-
|
|
114
|
+
notify()
|
|
115
115
|
})
|
|
116
116
|
},
|
|
117
117
|
go: (index) => {
|
|
118
118
|
tryNavigation(() => {
|
|
119
119
|
opts.go(index)
|
|
120
|
-
|
|
120
|
+
notify()
|
|
121
121
|
})
|
|
122
122
|
},
|
|
123
123
|
back: () => {
|
|
124
124
|
tryNavigation(() => {
|
|
125
125
|
opts.back()
|
|
126
|
-
|
|
126
|
+
notify()
|
|
127
127
|
})
|
|
128
128
|
},
|
|
129
129
|
forward: () => {
|
|
130
130
|
tryNavigation(() => {
|
|
131
131
|
opts.forward()
|
|
132
|
-
|
|
132
|
+
notify()
|
|
133
133
|
})
|
|
134
134
|
},
|
|
135
135
|
createHref: (str) => opts.createHref(str),
|
|
@@ -152,7 +152,7 @@ export function createHistory(opts: {
|
|
|
152
152
|
},
|
|
153
153
|
flush: () => opts.flush?.(),
|
|
154
154
|
destroy: () => opts.destroy?.(),
|
|
155
|
-
notify
|
|
155
|
+
notify,
|
|
156
156
|
}
|
|
157
157
|
}
|
|
158
158
|
|
|
@@ -191,6 +191,9 @@ export function createBrowserHistory(opts?: {
|
|
|
191
191
|
opts?.window ??
|
|
192
192
|
(typeof document !== 'undefined' ? window : (undefined as any))
|
|
193
193
|
|
|
194
|
+
const originalPushState = win.history.pushState
|
|
195
|
+
const originalReplaceState = win.history.replaceState
|
|
196
|
+
|
|
194
197
|
const createHref = opts?.createHref ?? ((path) => path)
|
|
195
198
|
const parseLocation =
|
|
196
199
|
opts?.parseLocation ??
|
|
@@ -216,39 +219,24 @@ export function createBrowserHistory(opts?: {
|
|
|
216
219
|
isPush: boolean
|
|
217
220
|
}
|
|
218
221
|
|
|
219
|
-
// Because we are proactively updating the location
|
|
220
|
-
// in memory before actually updating the browser history,
|
|
221
|
-
// we need to track when we are doing this so we don't
|
|
222
|
-
// notify subscribers twice on the last update.
|
|
223
|
-
let tracking = true
|
|
224
|
-
|
|
225
222
|
// We need to track the current scheduled update to prevent
|
|
226
223
|
// multiple updates from being scheduled at the same time.
|
|
227
224
|
let scheduled: Promise<void> | undefined
|
|
228
225
|
|
|
229
|
-
// This function is a wrapper to prevent any of the callback's
|
|
230
|
-
// side effects from causing a subscriber notification
|
|
231
|
-
const untrack = (fn: () => void) => {
|
|
232
|
-
tracking = false
|
|
233
|
-
fn()
|
|
234
|
-
tracking = true
|
|
235
|
-
}
|
|
236
|
-
|
|
237
226
|
// This function flushes the next update to the browser history
|
|
238
227
|
const flush = () => {
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
})
|
|
228
|
+
if (!next) {
|
|
229
|
+
return
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
// We use the original push/replace calls here to ensure that
|
|
233
|
+
// we do not notify subscribers about this push/replace call
|
|
234
|
+
const caller = next.isPush ? originalPushState : originalReplaceState
|
|
235
|
+
caller.call(win.history, next.state, '', next.href)
|
|
236
|
+
// Reset the nextIsPush flag and clear the scheduled update
|
|
237
|
+
next = undefined
|
|
238
|
+
scheduled = undefined
|
|
239
|
+
rollbackLocation = undefined
|
|
252
240
|
}
|
|
253
241
|
|
|
254
242
|
// This function queues up a call to update the browser history
|
|
@@ -284,9 +272,6 @@ export function createBrowserHistory(opts?: {
|
|
|
284
272
|
history.notify()
|
|
285
273
|
}
|
|
286
274
|
|
|
287
|
-
const originalPushState = win.history.pushState
|
|
288
|
-
const originalReplaceState = win.history.replaceState
|
|
289
|
-
|
|
290
275
|
const history = createHistory({
|
|
291
276
|
getLocation,
|
|
292
277
|
pushState: (href, state) => queueHistoryAction('push', href, state),
|
|
@@ -318,13 +303,13 @@ export function createBrowserHistory(opts?: {
|
|
|
318
303
|
|
|
319
304
|
win.history.pushState = function (...args: Array<any>) {
|
|
320
305
|
const res = originalPushState.apply(win.history, args)
|
|
321
|
-
|
|
306
|
+
onPushPop()
|
|
322
307
|
return res
|
|
323
308
|
}
|
|
324
309
|
|
|
325
310
|
win.history.replaceState = function (...args: Array<any>) {
|
|
326
311
|
const res = originalReplaceState.apply(win.history, args)
|
|
327
|
-
|
|
312
|
+
onPushPop()
|
|
328
313
|
return res
|
|
329
314
|
}
|
|
330
315
|
|
|
@@ -375,12 +360,15 @@ export function createMemoryHistory(
|
|
|
375
360
|
entries[index] = path
|
|
376
361
|
},
|
|
377
362
|
back: () => {
|
|
363
|
+
currentState = assignKey(currentState)
|
|
378
364
|
index--
|
|
379
365
|
},
|
|
380
366
|
forward: () => {
|
|
367
|
+
currentState = assignKey(currentState)
|
|
381
368
|
index = Math.min(index + 1, entries.length - 1)
|
|
382
369
|
},
|
|
383
370
|
go: (n) => {
|
|
371
|
+
currentState = assignKey(currentState)
|
|
384
372
|
index = Math.min(Math.max(index + n, 0), entries.length - 1)
|
|
385
373
|
},
|
|
386
374
|
createHref: (path) => path,
|