@tanstack/history 0.0.1-beta.193

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.
@@ -0,0 +1,45 @@
1
+ {
2
+ "version": 2,
3
+ "tree": {
4
+ "name": "root",
5
+ "children": [
6
+ {
7
+ "name": "index.production.js",
8
+ "children": [
9
+ {
10
+ "name": "packages/history/src/index.ts",
11
+ "uid": "7fc4-865"
12
+ }
13
+ ]
14
+ }
15
+ ],
16
+ "isRoot": true
17
+ },
18
+ "nodeParts": {
19
+ "7fc4-865": {
20
+ "renderedLength": 9309,
21
+ "gzipLength": 2670,
22
+ "brotliLength": 0,
23
+ "mainUid": "7fc4-864"
24
+ }
25
+ },
26
+ "nodeMetas": {
27
+ "7fc4-864": {
28
+ "id": "/packages/history/src/index.ts",
29
+ "moduleParts": {
30
+ "index.production.js": "7fc4-865"
31
+ },
32
+ "imported": [],
33
+ "importedBy": [],
34
+ "isEntry": true
35
+ }
36
+ },
37
+ "env": {
38
+ "rollup": "2.79.1"
39
+ },
40
+ "options": {
41
+ "gzip": true,
42
+ "brotli": false,
43
+ "sourcemap": false
44
+ }
45
+ }
@@ -0,0 +1,64 @@
1
+ /**
2
+ * @tanstack/history/src/index.ts
3
+ *
4
+ * Copyright (c) TanStack
5
+ *
6
+ * This source code is licensed under the MIT license found in the
7
+ * LICENSE.md file in the root directory of this source tree.
8
+ *
9
+ * @license MIT
10
+ */
11
+ interface RouterHistory {
12
+ location: HistoryLocation;
13
+ subscribe: (cb: () => void) => () => void;
14
+ push: (path: string, state?: any) => void;
15
+ replace: (path: string, state?: any) => void;
16
+ go: (index: number) => void;
17
+ back: () => void;
18
+ forward: () => void;
19
+ createHref: (href: string) => string;
20
+ block: (blockerFn: BlockerFn) => () => void;
21
+ flush: () => void;
22
+ }
23
+ interface HistoryLocation extends ParsedPath {
24
+ state: HistoryState;
25
+ }
26
+ interface ParsedPath {
27
+ href: string;
28
+ pathname: string;
29
+ search: string;
30
+ hash: string;
31
+ }
32
+ interface HistoryState {
33
+ key: string;
34
+ __tempLocation?: HistoryLocation;
35
+ __tempKey?: string;
36
+ }
37
+ type BlockerFn = (retry: () => void, cancel: () => void) => void;
38
+ /**
39
+ * Creates a history object that can be used to interact with the browser's
40
+ * navigation. This is a lightweight API wrapping the browser's native methods.
41
+ * It is designed to work with TanStack Router, but could be used as a standalone API as well.
42
+ * IMPORTANT: This API implements history throttling via a microtask to prevent
43
+ * excessive calls to the history API. In some browsers, calling history.pushState or
44
+ * history.replaceState in quick succession can cause the browser to ignore subsequent
45
+ * calls. This API smooths out those differences and ensures that your application
46
+ * state will *eventually* match the browser state. In most cases, this is not a problem,
47
+ * but if you need to ensure that the browser state is up to date, you can use the
48
+ * `history.flush` method to immediately flush all pending state changes to the browser URL.
49
+ * @param opts
50
+ * @param opts.getHref A function that returns the current href (path + search + hash)
51
+ * @param opts.createHref A function that takes a path and returns a href (path + search + hash)
52
+ * @returns A history instance
53
+ */
54
+ declare function createBrowserHistory(opts?: {
55
+ getHref?: () => string;
56
+ createHref?: (path: string) => string;
57
+ }): RouterHistory;
58
+ declare function createHashHistory(): RouterHistory;
59
+ declare function createMemoryHistory(opts?: {
60
+ initialEntries: string[];
61
+ initialIndex?: number;
62
+ }): RouterHistory;
63
+
64
+ export { HistoryLocation, HistoryState, ParsedPath, RouterHistory, createBrowserHistory, createHashHistory, createMemoryHistory };
@@ -0,0 +1,307 @@
1
+ /**
2
+ * @tanstack/history/src/index.ts
3
+ *
4
+ * Copyright (c) TanStack
5
+ *
6
+ * This source code is licensed under the MIT license found in the
7
+ * LICENSE.md file in the root directory of this source tree.
8
+ *
9
+ * @license MIT
10
+ */
11
+ (function (global, factory) {
12
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
13
+ typeof define === 'function' && define.amd ? define(['exports'], factory) :
14
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.TanStackHistory = {}));
15
+ })(this, (function (exports) { 'use strict';
16
+
17
+ // While the public API was clearly inspired by the "history" npm package,
18
+ // This implementation attempts to be more lightweight by
19
+ // making assumptions about the way TanStack Router works
20
+
21
+ const pushStateEvent = 'pushstate';
22
+ const popStateEvent = 'popstate';
23
+ const beforeUnloadEvent = 'beforeunload';
24
+ const beforeUnloadListener = event => {
25
+ event.preventDefault();
26
+ // @ts-ignore
27
+ return event.returnValue = '';
28
+ };
29
+ const stopBlocking = () => {
30
+ removeEventListener(beforeUnloadEvent, beforeUnloadListener, {
31
+ capture: true
32
+ });
33
+ };
34
+ function createHistory(opts) {
35
+ let location = opts.getLocation();
36
+ let unsub = () => {};
37
+ let subscribers = new Set();
38
+ let blockers = [];
39
+ let queue = [];
40
+ const tryFlush = () => {
41
+ if (blockers.length) {
42
+ blockers[0]?.(tryFlush, () => {
43
+ blockers = [];
44
+ stopBlocking();
45
+ });
46
+ return;
47
+ }
48
+ while (queue.length) {
49
+ queue.shift()?.();
50
+ }
51
+ if (!opts.subscriber) {
52
+ onUpdate();
53
+ }
54
+ };
55
+ const queueTask = task => {
56
+ queue.push(task);
57
+ tryFlush();
58
+ };
59
+ const onUpdate = () => {
60
+ location = opts.getLocation();
61
+ subscribers.forEach(subscriber => subscriber());
62
+ };
63
+ return {
64
+ get location() {
65
+ return location;
66
+ },
67
+ subscribe: cb => {
68
+ if (subscribers.size === 0) {
69
+ unsub = typeof opts.subscriber === 'function' ? opts.subscriber(onUpdate) : () => {};
70
+ }
71
+ subscribers.add(cb);
72
+ return () => {
73
+ subscribers.delete(cb);
74
+ if (subscribers.size === 0) {
75
+ unsub();
76
+ }
77
+ };
78
+ },
79
+ push: (path, state) => {
80
+ assignKey(state);
81
+ queueTask(() => {
82
+ opts.pushState(path, state, onUpdate);
83
+ });
84
+ },
85
+ replace: (path, state) => {
86
+ assignKey(state);
87
+ queueTask(() => {
88
+ opts.replaceState(path, state, onUpdate);
89
+ });
90
+ },
91
+ go: index => {
92
+ queueTask(() => {
93
+ opts.go(index);
94
+ });
95
+ },
96
+ back: () => {
97
+ queueTask(() => {
98
+ opts.back();
99
+ });
100
+ },
101
+ forward: () => {
102
+ queueTask(() => {
103
+ opts.forward();
104
+ });
105
+ },
106
+ createHref: str => opts.createHref(str),
107
+ block: cb => {
108
+ blockers.push(cb);
109
+ if (blockers.length === 1) {
110
+ addEventListener(beforeUnloadEvent, beforeUnloadListener, {
111
+ capture: true
112
+ });
113
+ }
114
+ return () => {
115
+ blockers = blockers.filter(b => b !== cb);
116
+ if (!blockers.length) {
117
+ stopBlocking();
118
+ }
119
+ };
120
+ },
121
+ flush: () => opts.flush?.()
122
+ };
123
+ }
124
+ function assignKey(state) {
125
+ state.key = createRandomKey();
126
+ // if (state.__actualLocation) {
127
+ // state.__actualLocation.state = {
128
+ // ...state.__actualLocation.state,
129
+ // key,
130
+ // }
131
+ // }
132
+ }
133
+
134
+ /**
135
+ * Creates a history object that can be used to interact with the browser's
136
+ * navigation. This is a lightweight API wrapping the browser's native methods.
137
+ * It is designed to work with TanStack Router, but could be used as a standalone API as well.
138
+ * IMPORTANT: This API implements history throttling via a microtask to prevent
139
+ * excessive calls to the history API. In some browsers, calling history.pushState or
140
+ * history.replaceState in quick succession can cause the browser to ignore subsequent
141
+ * calls. This API smooths out those differences and ensures that your application
142
+ * state will *eventually* match the browser state. In most cases, this is not a problem,
143
+ * but if you need to ensure that the browser state is up to date, you can use the
144
+ * `history.flush` method to immediately flush all pending state changes to the browser URL.
145
+ * @param opts
146
+ * @param opts.getHref A function that returns the current href (path + search + hash)
147
+ * @param opts.createHref A function that takes a path and returns a href (path + search + hash)
148
+ * @returns A history instance
149
+ */
150
+ function createBrowserHistory(opts) {
151
+ const getHref = opts?.getHref ?? (() => `${window.location.pathname}${window.location.search}${window.location.hash}`);
152
+ const createHref = opts?.createHref ?? (path => path);
153
+ let currentLocation = parseLocation(getHref(), window.history.state);
154
+ const getLocation = () => currentLocation;
155
+ let next;
156
+
157
+ // Because we are proactively updating the location
158
+ // in memory before actually updating the browser history,
159
+ // we need to track when we are doing this so we don't
160
+ // notify subscribers twice on the last update.
161
+ let tracking = true;
162
+
163
+ // We need to track the current scheduled update to prevent
164
+ // multiple updates from being scheduled at the same time.
165
+ let scheduled;
166
+
167
+ // This function is a wrapper to prevent any of the callback's
168
+ // side effects from causing a subscriber notification
169
+ const untrack = fn => {
170
+ tracking = false;
171
+ fn();
172
+ tracking = true;
173
+ };
174
+
175
+ // This function flushes the next update to the browser history
176
+ const flush = () => {
177
+ // Do not notify subscribers about this push/replace call
178
+ untrack(() => {
179
+ if (!next) return;
180
+ window.history[next.isPush ? 'pushState' : 'replaceState'](next.state, '', next.href);
181
+ // Reset the nextIsPush flag and clear the scheduled update
182
+ next = undefined;
183
+ scheduled = undefined;
184
+ });
185
+ };
186
+
187
+ // This function queues up a call to update the browser history
188
+ const queueHistoryAction = (type, path, state, onUpdate) => {
189
+ const href = createHref(path);
190
+
191
+ // Update the location in memory
192
+ currentLocation = parseLocation(href, state);
193
+
194
+ // Keep track of the next location we need to flush to the URL
195
+ next = {
196
+ href,
197
+ state,
198
+ isPush: next?.isPush || type === 'push'
199
+ };
200
+ // Notify subscribers
201
+ onUpdate();
202
+ if (!scheduled) {
203
+ // Schedule an update to the browser history
204
+ scheduled = Promise.resolve().then(() => flush());
205
+ }
206
+ };
207
+ return createHistory({
208
+ getLocation,
209
+ subscriber: onUpdate => {
210
+ window.addEventListener(pushStateEvent, () => {
211
+ currentLocation = parseLocation(getHref(), window.history.state);
212
+ onUpdate();
213
+ });
214
+ window.addEventListener(popStateEvent, () => {
215
+ currentLocation = parseLocation(getHref(), window.history.state);
216
+ onUpdate();
217
+ });
218
+ var pushState = window.history.pushState;
219
+ window.history.pushState = function () {
220
+ let res = pushState.apply(history, arguments);
221
+ if (tracking) onUpdate();
222
+ return res;
223
+ };
224
+ var replaceState = window.history.replaceState;
225
+ window.history.replaceState = function () {
226
+ let res = replaceState.apply(history, arguments);
227
+ if (tracking) onUpdate();
228
+ return res;
229
+ };
230
+ return () => {
231
+ window.history.pushState = pushState;
232
+ window.history.replaceState = replaceState;
233
+ window.removeEventListener(pushStateEvent, onUpdate);
234
+ window.removeEventListener(popStateEvent, onUpdate);
235
+ };
236
+ },
237
+ pushState: (path, state, onUpdate) => queueHistoryAction('push', path, state, onUpdate),
238
+ replaceState: (path, state, onUpdate) => queueHistoryAction('replace', path, state, onUpdate),
239
+ back: () => window.history.back(),
240
+ forward: () => window.history.forward(),
241
+ go: n => window.history.go(n),
242
+ createHref: path => createHref(path),
243
+ flush
244
+ });
245
+ }
246
+ function createHashHistory() {
247
+ return createBrowserHistory({
248
+ getHref: () => window.location.hash.substring(1),
249
+ createHref: path => `#${path}`
250
+ });
251
+ }
252
+ function createMemoryHistory(opts = {
253
+ initialEntries: ['/']
254
+ }) {
255
+ const entries = opts.initialEntries;
256
+ let index = opts.initialIndex ?? entries.length - 1;
257
+ let currentState = {
258
+ key: createRandomKey()
259
+ };
260
+ const getLocation = () => parseLocation(entries[index], currentState);
261
+ return createHistory({
262
+ getLocation,
263
+ subscriber: false,
264
+ pushState: (path, state) => {
265
+ currentState = state;
266
+ entries.push(path);
267
+ index++;
268
+ },
269
+ replaceState: (path, state) => {
270
+ currentState = state;
271
+ entries[index] = path;
272
+ },
273
+ back: () => {
274
+ index--;
275
+ },
276
+ forward: () => {
277
+ index = Math.min(index + 1, entries.length - 1);
278
+ },
279
+ go: n => window.history.go(n),
280
+ createHref: path => path
281
+ });
282
+ }
283
+ function parseLocation(href, state) {
284
+ let hashIndex = href.indexOf('#');
285
+ let searchIndex = href.indexOf('?');
286
+ return {
287
+ href,
288
+ pathname: href.substring(0, hashIndex > 0 ? searchIndex > 0 ? Math.min(hashIndex, searchIndex) : hashIndex : searchIndex > 0 ? searchIndex : href.length),
289
+ hash: hashIndex > -1 ? href.substring(hashIndex) : '',
290
+ search: searchIndex > -1 ? href.slice(searchIndex, hashIndex === -1 ? undefined : hashIndex) : '',
291
+ state: state || {}
292
+ };
293
+ }
294
+
295
+ // Thanks co-pilot!
296
+ function createRandomKey() {
297
+ return (Math.random() + 1).toString(36).substring(7);
298
+ }
299
+
300
+ exports.createBrowserHistory = createBrowserHistory;
301
+ exports.createHashHistory = createHashHistory;
302
+ exports.createMemoryHistory = createMemoryHistory;
303
+
304
+ Object.defineProperty(exports, '__esModule', { value: true });
305
+
306
+ }));
307
+ //# sourceMappingURL=index.development.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.development.js","sources":["../../src/index.ts"],"sourcesContent":["// While the public API was clearly inspired by the \"history\" npm package,\n// This implementation attempts to be more lightweight by\n// making assumptions about the way TanStack Router works\n\nexport interface RouterHistory {\n location: HistoryLocation\n subscribe: (cb: () => void) => () => void\n push: (path: string, state?: any) => void\n replace: (path: string, state?: any) => void\n go: (index: number) => void\n back: () => void\n forward: () => void\n createHref: (href: string) => string\n block: (blockerFn: BlockerFn) => () => void\n flush: () => void\n}\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 __tempLocation?: HistoryLocation\n __tempKey?: string\n}\n\ntype BlockerFn = (retry: () => void, cancel: () => void) => void\n\nconst pushStateEvent = 'pushstate'\nconst popStateEvent = 'popstate'\nconst beforeUnloadEvent = 'beforeunload'\n\nconst beforeUnloadListener = (event: Event) => {\n event.preventDefault()\n // @ts-ignore\n return (event.returnValue = '')\n}\n\nconst stopBlocking = () => {\n removeEventListener(beforeUnloadEvent, beforeUnloadListener, {\n capture: true,\n })\n}\n\nfunction createHistory(opts: {\n getLocation: () => HistoryLocation\n subscriber: false | ((onUpdate: () => void) => () => void)\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}): RouterHistory {\n let location = opts.getLocation()\n let unsub = () => {}\n let subscribers = new Set<() => void>()\n let blockers: BlockerFn[] = []\n let queue: (() => void)[] = []\n\n const tryFlush = () => {\n if (blockers.length) {\n blockers[0]?.(tryFlush, () => {\n blockers = []\n stopBlocking()\n })\n return\n }\n\n while (queue.length) {\n queue.shift()?.()\n }\n\n if (!opts.subscriber) {\n onUpdate()\n }\n }\n\n const queueTask = (task: () => void) => {\n queue.push(task)\n tryFlush()\n }\n\n const onUpdate = () => {\n location = opts.getLocation()\n subscribers.forEach((subscriber) => subscriber())\n }\n\n return {\n get location() {\n return location\n },\n subscribe: (cb: () => void) => {\n if (subscribers.size === 0) {\n unsub =\n typeof opts.subscriber === 'function'\n ? opts.subscriber(onUpdate)\n : () => {}\n }\n subscribers.add(cb)\n\n return () => {\n subscribers.delete(cb)\n if (subscribers.size === 0) {\n unsub()\n }\n }\n },\n push: (path: string, state: any) => {\n assignKey(state)\n queueTask(() => {\n opts.pushState(path, state, onUpdate)\n })\n },\n replace: (path: string, state: any) => {\n assignKey(state)\n queueTask(() => {\n opts.replaceState(path, state, onUpdate)\n })\n },\n go: (index) => {\n queueTask(() => {\n opts.go(index)\n })\n },\n back: () => {\n queueTask(() => {\n opts.back()\n })\n },\n forward: () => {\n queueTask(() => {\n opts.forward()\n })\n },\n createHref: (str) => opts.createHref(str),\n block: (cb) => {\n blockers.push(cb)\n\n if (blockers.length === 1) {\n addEventListener(beforeUnloadEvent, beforeUnloadListener, {\n capture: true,\n })\n }\n\n return () => {\n blockers = blockers.filter((b) => b !== cb)\n\n if (!blockers.length) {\n stopBlocking()\n }\n }\n },\n flush: () => opts.flush?.(),\n }\n}\n\nfunction assignKey(state: HistoryState) {\n state.key = createRandomKey()\n // if (state.__actualLocation) {\n // state.__actualLocation.state = {\n // ...state.__actualLocation.state,\n // key,\n // }\n // }\n}\n\n/**\n * Creates a history object that can be used to interact with the browser's\n * navigation. This is a lightweight API wrapping the browser's native methods.\n * It is designed to work with TanStack Router, but could be used as a standalone API as well.\n * IMPORTANT: This API implements history throttling via a microtask to prevent\n * excessive calls to the history API. In some browsers, calling history.pushState or\n * history.replaceState in quick succession can cause the browser to ignore subsequent\n * calls. This API smooths out those differences and ensures that your application\n * state will *eventually* match the browser state. In most cases, this is not a problem,\n * but if you need to ensure that the browser state is up to date, you can use the\n * `history.flush` method to immediately flush all pending state changes to the browser URL.\n * @param opts\n * @param opts.getHref A function that returns the current href (path + search + hash)\n * @param opts.createHref A function that takes a path and returns a href (path + search + hash)\n * @returns A history instance\n */\nexport function createBrowserHistory(opts?: {\n getHref?: () => string\n createHref?: (path: string) => string\n}): RouterHistory {\n const getHref =\n opts?.getHref ??\n (() =>\n `${window.location.pathname}${window.location.search}${window.location.hash}`)\n\n const createHref = opts?.createHref ?? ((path) => path)\n\n let currentLocation = parseLocation(getHref(), window.history.state)\n\n const getLocation = () => currentLocation\n\n let next:\n | undefined\n | {\n // This is the latest location that we were attempting to push/replace\n href: string\n // This is the latest state that we were attempting to push/replace\n state: any\n // This is the latest type that we were attempting to push/replace\n isPush: boolean\n }\n\n // Because we are proactively updating the location\n // in memory before actually updating the browser history,\n // we need to track when we are doing this so we don't\n // notify subscribers twice on the last update.\n let tracking = true\n\n // We need to track the current scheduled update to prevent\n // multiple updates from being scheduled at the same time.\n let scheduled: Promise<void> | undefined\n\n // This function is a wrapper to prevent any of the callback's\n // side effects from causing a subscriber notification\n const untrack = (fn: () => void) => {\n tracking = false\n fn()\n tracking = true\n }\n\n // This function flushes the next update to the browser history\n const flush = () => {\n // Do not notify subscribers about this push/replace call\n untrack(() => {\n if (!next) return\n window.history[next.isPush ? 'pushState' : 'replaceState'](\n next.state,\n '',\n next.href,\n )\n // Reset the nextIsPush flag and clear the scheduled update\n next = undefined\n scheduled = undefined\n })\n }\n\n // This function queues up a call to update the browser history\n const queueHistoryAction = (\n type: 'push' | 'replace',\n path: string,\n state: any,\n onUpdate: () => void,\n ) => {\n const href = createHref(path)\n\n // Update the location in memory\n currentLocation = parseLocation(href, state)\n\n // Keep track of the next location we need to flush to the URL\n next = {\n href,\n state,\n isPush: next?.isPush || type === 'push',\n }\n // Notify subscribers\n onUpdate()\n\n if (!scheduled) {\n // Schedule an update to the browser history\n scheduled = Promise.resolve().then(() => flush())\n }\n }\n\n return createHistory({\n getLocation,\n subscriber: (onUpdate) => {\n window.addEventListener(pushStateEvent, () => {\n currentLocation = parseLocation(getHref(), window.history.state)\n onUpdate()\n })\n window.addEventListener(popStateEvent, () => {\n currentLocation = parseLocation(getHref(), window.history.state)\n onUpdate()\n })\n\n var pushState = window.history.pushState\n window.history.pushState = function () {\n let res = pushState.apply(history, arguments as any)\n if (tracking) onUpdate()\n return res\n }\n var replaceState = window.history.replaceState\n window.history.replaceState = function () {\n let res = replaceState.apply(history, arguments as any)\n if (tracking) onUpdate()\n return res\n }\n\n return () => {\n window.history.pushState = pushState\n window.history.replaceState = replaceState\n window.removeEventListener(pushStateEvent, onUpdate)\n window.removeEventListener(popStateEvent, onUpdate)\n }\n },\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 })\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 subscriber: false,\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","unsub","subscribers","Set","blockers","queue","tryFlush","length","shift","subscriber","onUpdate","queueTask","task","push","forEach","subscribe","cb","size","add","delete","path","state","assignKey","pushState","replace","replaceState","go","index","back","forward","createHref","str","block","addEventListener","filter","b","flush","key","createRandomKey","createBrowserHistory","getHref","window","pathname","search","hash","currentLocation","parseLocation","history","next","tracking","scheduled","untrack","fn","isPush","href","undefined","queueHistoryAction","type","Promise","resolve","then","res","apply","arguments","n","createHashHistory","substring","createMemoryHistory","initialEntries","entries","initialIndex","currentState","Math","min","hashIndex","indexOf","searchIndex","slice","random","toString"],"mappings":";;;;;;;;;;;;;;;;EAAA;EACA;EACA;;EAkCA,MAAMA,cAAc,GAAG,WAAW,CAAA;EAClC,MAAMC,aAAa,GAAG,UAAU,CAAA;EAChC,MAAMC,iBAAiB,GAAG,cAAc,CAAA;EAExC,MAAMC,oBAAoB,GAAIC,KAAY,IAAK;IAC7CA,KAAK,CAACC,cAAc,EAAE,CAAA;EACtB;EACA,EAAA,OAAQD,KAAK,CAACE,WAAW,GAAG,EAAE,CAAA;EAChC,CAAC,CAAA;EAED,MAAMC,YAAY,GAAGA,MAAM;EACzBC,EAAAA,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AAM,EAAE,GAAG,CAAC,EAAEC,QAAQ,CAAC,EAAE,CAAC,CAACb,SAAS,CAAC,CAAC,CAAC,CAAA;EACtD;;;;;;;;;;;;"}
@@ -0,0 +1,12 @@
1
+ /**
2
+ * @tanstack/history/src/index.ts
3
+ *
4
+ * Copyright (c) TanStack
5
+ *
6
+ * This source code is licensed under the MIT license found in the
7
+ * LICENSE.md file in the root directory of this source tree.
8
+ *
9
+ * @license MIT
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",r="popstate",n="beforeunload",o=e=>(e.preventDefault(),e.returnValue=""),i=()=>{removeEventListener(n,o,{capture:!0})};function s(e){let t=e.getLocation(),r=()=>{},s=new Set,c=[],h=[];const u=()=>{if(c.length)c[0]?.(u,(()=>{c=[],i()}));else{for(;h.length;)h.shift()?.();e.subscriber||f()}},d=e=>{h.push(e),u()},f=()=>{t=e.getLocation(),s.forEach((e=>e()))};return{get location(){return t},subscribe:t=>(0===s.size&&(r="function"==typeof e.subscriber?e.subscriber(f):()=>{}),s.add(t),()=>{s.delete(t),0===s.size&&r()}),push:(t,r)=>{a(r),d((()=>{e.pushState(t,r,f)}))},replace:(t,r)=>{a(r),d((()=>{e.replaceState(t,r,f)}))},go:t=>{d((()=>{e.go(t)}))},back:()=>{d((()=>{e.back()}))},forward:()=>{d((()=>{e.forward()}))},createHref:t=>e.createHref(t),block:e=>(c.push(e),1===c.length&&addEventListener(n,o,{capture:!0}),()=>{c=c.filter((t=>t!==e)),c.length||i()}),flush:()=>e.flush?.()}}function a(e){e.key=u()}function c(e){const n=e?.getHref??(()=>`${window.location.pathname}${window.location.search}${window.location.hash}`),o=e?.createHref??(e=>e);let i=h(n(),window.history.state);let a,c,u=!0;const d=()=>{u=!1,(()=>{a&&(window.history[a.isPush?"pushState":"replaceState"](a.state,"",a.href),a=void 0,c=void 0)})(),u=!0},f=(e,t,r,n)=>{const s=o(t);i=h(s,r),a={href:s,state:r,isPush:a?.isPush||"push"===e},n(),c||(c=Promise.resolve().then((()=>d())))};return s({getLocation:()=>i,subscriber:e=>{window.addEventListener(t,(()=>{i=h(n(),window.history.state),e()})),window.addEventListener(r,(()=>{i=h(n(),window.history.state),e()}));var o=window.history.pushState;window.history.pushState=function(){let t=o.apply(history,arguments);return u&&e(),t};var s=window.history.replaceState;return window.history.replaceState=function(){let t=s.apply(history,arguments);return u&&e(),t},()=>{window.history.pushState=o,window.history.replaceState=s,window.removeEventListener(t,e),window.removeEventListener(r,e)}},pushState:(e,t,r)=>f("push",e,t,r),replaceState:(e,t,r)=>f("replace",e,t,r),back:()=>window.history.back(),forward:()=>window.history.forward(),go:e=>window.history.go(e),createHref:e=>o(e),flush:d})}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(){return c({getHref:()=>window.location.hash.substring(1),createHref:e=>`#${e}`})},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),subscriber:!1,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=>window.history.go(e),createHref:e=>e})},Object.defineProperty(e,"__esModule",{value:!0})}));
12
+ //# sourceMappingURL=index.production.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.production.js","sources":["../../src/index.ts"],"sourcesContent":["// While the public API was clearly inspired by the \"history\" npm package,\n// This implementation attempts to be more lightweight by\n// making assumptions about the way TanStack Router works\n\nexport interface RouterHistory {\n location: HistoryLocation\n subscribe: (cb: () => void) => () => void\n push: (path: string, state?: any) => void\n replace: (path: string, state?: any) => void\n go: (index: number) => void\n back: () => void\n forward: () => void\n createHref: (href: string) => string\n block: (blockerFn: BlockerFn) => () => void\n flush: () => void\n}\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 __tempLocation?: HistoryLocation\n __tempKey?: string\n}\n\ntype BlockerFn = (retry: () => void, cancel: () => void) => void\n\nconst pushStateEvent = 'pushstate'\nconst popStateEvent = 'popstate'\nconst beforeUnloadEvent = 'beforeunload'\n\nconst beforeUnloadListener = (event: Event) => {\n event.preventDefault()\n // @ts-ignore\n return (event.returnValue = '')\n}\n\nconst stopBlocking = () => {\n removeEventListener(beforeUnloadEvent, beforeUnloadListener, {\n capture: true,\n })\n}\n\nfunction createHistory(opts: {\n getLocation: () => HistoryLocation\n subscriber: false | ((onUpdate: () => void) => () => void)\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}): RouterHistory {\n let location = opts.getLocation()\n let unsub = () => {}\n let subscribers = new Set<() => void>()\n let blockers: BlockerFn[] = []\n let queue: (() => void)[] = []\n\n const tryFlush = () => {\n if (blockers.length) {\n blockers[0]?.(tryFlush, () => {\n blockers = []\n stopBlocking()\n })\n return\n }\n\n while (queue.length) {\n queue.shift()?.()\n }\n\n if (!opts.subscriber) {\n onUpdate()\n }\n }\n\n const queueTask = (task: () => void) => {\n queue.push(task)\n tryFlush()\n }\n\n const onUpdate = () => {\n location = opts.getLocation()\n subscribers.forEach((subscriber) => subscriber())\n }\n\n return {\n get location() {\n return location\n },\n subscribe: (cb: () => void) => {\n if (subscribers.size === 0) {\n unsub =\n typeof opts.subscriber === 'function'\n ? opts.subscriber(onUpdate)\n : () => {}\n }\n subscribers.add(cb)\n\n return () => {\n subscribers.delete(cb)\n if (subscribers.size === 0) {\n unsub()\n }\n }\n },\n push: (path: string, state: any) => {\n assignKey(state)\n queueTask(() => {\n opts.pushState(path, state, onUpdate)\n })\n },\n replace: (path: string, state: any) => {\n assignKey(state)\n queueTask(() => {\n opts.replaceState(path, state, onUpdate)\n })\n },\n go: (index) => {\n queueTask(() => {\n opts.go(index)\n })\n },\n back: () => {\n queueTask(() => {\n opts.back()\n })\n },\n forward: () => {\n queueTask(() => {\n opts.forward()\n })\n },\n createHref: (str) => opts.createHref(str),\n block: (cb) => {\n blockers.push(cb)\n\n if (blockers.length === 1) {\n addEventListener(beforeUnloadEvent, beforeUnloadListener, {\n capture: true,\n })\n }\n\n return () => {\n blockers = blockers.filter((b) => b !== cb)\n\n if (!blockers.length) {\n stopBlocking()\n }\n }\n },\n flush: () => opts.flush?.(),\n }\n}\n\nfunction assignKey(state: HistoryState) {\n state.key = createRandomKey()\n // if (state.__actualLocation) {\n // state.__actualLocation.state = {\n // ...state.__actualLocation.state,\n // key,\n // }\n // }\n}\n\n/**\n * Creates a history object that can be used to interact with the browser's\n * navigation. This is a lightweight API wrapping the browser's native methods.\n * It is designed to work with TanStack Router, but could be used as a standalone API as well.\n * IMPORTANT: This API implements history throttling via a microtask to prevent\n * excessive calls to the history API. In some browsers, calling history.pushState or\n * history.replaceState in quick succession can cause the browser to ignore subsequent\n * calls. This API smooths out those differences and ensures that your application\n * state will *eventually* match the browser state. In most cases, this is not a problem,\n * but if you need to ensure that the browser state is up to date, you can use the\n * `history.flush` method to immediately flush all pending state changes to the browser URL.\n * @param opts\n * @param opts.getHref A function that returns the current href (path + search + hash)\n * @param opts.createHref A function that takes a path and returns a href (path + search + hash)\n * @returns A history instance\n */\nexport function createBrowserHistory(opts?: {\n getHref?: () => string\n createHref?: (path: string) => string\n}): RouterHistory {\n const getHref =\n opts?.getHref ??\n (() =>\n `${window.location.pathname}${window.location.search}${window.location.hash}`)\n\n const createHref = opts?.createHref ?? ((path) => path)\n\n let currentLocation = parseLocation(getHref(), window.history.state)\n\n const getLocation = () => currentLocation\n\n let next:\n | undefined\n | {\n // This is the latest location that we were attempting to push/replace\n href: string\n // This is the latest state that we were attempting to push/replace\n state: any\n // This is the latest type that we were attempting to push/replace\n isPush: boolean\n }\n\n // Because we are proactively updating the location\n // in memory before actually updating the browser history,\n // we need to track when we are doing this so we don't\n // notify subscribers twice on the last update.\n let tracking = true\n\n // We need to track the current scheduled update to prevent\n // multiple updates from being scheduled at the same time.\n let scheduled: Promise<void> | undefined\n\n // This function is a wrapper to prevent any of the callback's\n // side effects from causing a subscriber notification\n const untrack = (fn: () => void) => {\n tracking = false\n fn()\n tracking = true\n }\n\n // This function flushes the next update to the browser history\n const flush = () => {\n // Do not notify subscribers about this push/replace call\n untrack(() => {\n if (!next) return\n window.history[next.isPush ? 'pushState' : 'replaceState'](\n next.state,\n '',\n next.href,\n )\n // Reset the nextIsPush flag and clear the scheduled update\n next = undefined\n scheduled = undefined\n })\n }\n\n // This function queues up a call to update the browser history\n const queueHistoryAction = (\n type: 'push' | 'replace',\n path: string,\n state: any,\n onUpdate: () => void,\n ) => {\n const href = createHref(path)\n\n // Update the location in memory\n currentLocation = parseLocation(href, state)\n\n // Keep track of the next location we need to flush to the URL\n next = {\n href,\n state,\n isPush: next?.isPush || type === 'push',\n }\n // Notify subscribers\n onUpdate()\n\n if (!scheduled) {\n // Schedule an update to the browser history\n scheduled = Promise.resolve().then(() => flush())\n }\n }\n\n return createHistory({\n getLocation,\n subscriber: (onUpdate) => {\n window.addEventListener(pushStateEvent, () => {\n currentLocation = parseLocation(getHref(), window.history.state)\n onUpdate()\n })\n window.addEventListener(popStateEvent, () => {\n currentLocation = parseLocation(getHref(), window.history.state)\n onUpdate()\n })\n\n var pushState = window.history.pushState\n window.history.pushState = function () {\n let res = pushState.apply(history, arguments as any)\n if (tracking) onUpdate()\n return res\n }\n var replaceState = window.history.replaceState\n window.history.replaceState = function () {\n let res = replaceState.apply(history, arguments as any)\n if (tracking) onUpdate()\n return res\n }\n\n return () => {\n window.history.pushState = pushState\n window.history.replaceState = replaceState\n window.removeEventListener(pushStateEvent, onUpdate)\n window.removeEventListener(popStateEvent, onUpdate)\n }\n },\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 })\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 subscriber: false,\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","unsub","subscribers","Set","blockers","queue","tryFlush","length","shift","subscriber","onUpdate","queueTask","task","push","forEach","subscribe","cb","size","add","delete","path","state","assignKey","pushState","replace","replaceState","go","index","back","forward","createHref","str","block","addEventListener","filter","b","flush","key","createRandomKey","createBrowserHistory","getHref","window","pathname","search","hash","currentLocation","parseLocation","history","next","scheduled","tracking","isPush","href","undefined","fn","queueHistoryAction","type","Promise","resolve","then","res","apply","arguments","n","hashIndex","indexOf","searchIndex","substring","Math","min","slice","random","toString","initialEntries","entries","initialIndex","currentState"],"mappings":";;;;;;;;;;uPAoCA,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,GAWrB,IAAIC,EAAWD,EAAKE,cAChBC,EAAQA,OACRC,EAAc,IAAIC,IAClBC,EAAwB,GACxBC,EAAwB,GAE5B,MAAMC,EAAWA,KACf,GAAIF,EAASG,OACXH,EAAS,KAAKE,GAAU,KACtBF,EAAW,GACXV,GAAc,QAHlB,CAQA,KAAOW,EAAME,QACXF,EAAMG,OAANH,KAGGP,EAAKW,YACRC,GAPF,CAQA,EAGIC,EAAaC,IACjBP,EAAMQ,KAAKD,GACXN,GAAU,EAGNI,EAAWA,KACfX,EAAWD,EAAKE,cAChBE,EAAYY,SAASL,GAAeA,KAAa,EAGnD,MAAO,CACDV,eACF,OAAOA,CACR,EACDgB,UAAYC,IACe,IAArBd,EAAYe,OACdhB,EAC6B,mBAApBH,EAAKW,WACRX,EAAKW,WAAWC,GAChB,QAERR,EAAYgB,IAAIF,GAET,KACLd,EAAYiB,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.0.1-beta.193",
5
+ "license": "MIT",
6
+ "repository": "tanstack/history",
7
+ "homepage": "https://tanstack.com",
8
+ "description": "",
9
+ "publishConfig": {
10
+ "registry": "https://registry.npmjs.org/"
11
+ },
12
+ "keywords": [
13
+ "history",
14
+ "typescript"
15
+ ],
16
+ "funding": {
17
+ "type": "github",
18
+ "url": "https://github.com/sponsors/tannerlinsley"
19
+ },
20
+ "module": "build/esm/index.js",
21
+ "main": "build/cjs/index.js",
22
+ "browser": "build/umd/index.production.js",
23
+ "types": "build/types/index.d.ts",
24
+ "engines": {
25
+ "node": ">=12"
26
+ },
27
+ "files": [
28
+ "build/**",
29
+ "src"
30
+ ],
31
+ "sideEffects": false,
32
+ "scripts": {
33
+ "build": "rollup --config rollup.config.js"
34
+ }
35
+ }