@tanstack/history 1.161.4 → 1.161.6

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/esm/index.js CHANGED
@@ -1,425 +1,406 @@
1
- const stateIndexKey = "__TSR_index";
2
- const popStateEvent = "popstate";
3
- const beforeUnloadEvent = "beforeunload";
1
+ //#region src/index.ts
2
+ var stateIndexKey = "__TSR_index";
3
+ var popStateEvent = "popstate";
4
+ var beforeUnloadEvent = "beforeunload";
4
5
  function createHistory(opts) {
5
- let location = opts.getLocation();
6
- const subscribers = /* @__PURE__ */ new Set();
7
- const notify = (action) => {
8
- location = opts.getLocation();
9
- subscribers.forEach((subscriber) => subscriber({ location, action }));
10
- };
11
- const handleIndexChange = (action) => {
12
- if (opts.notifyOnIndexChange ?? true) notify(action);
13
- else location = opts.getLocation();
14
- };
15
- const tryNavigation = async ({
16
- task,
17
- navigateOpts,
18
- ...actionInfo
19
- }) => {
20
- const ignoreBlocker = navigateOpts?.ignoreBlocker ?? false;
21
- if (ignoreBlocker) {
22
- task();
23
- return;
24
- }
25
- const blockers = opts.getBlockers?.() ?? [];
26
- const isPushOrReplace = actionInfo.type === "PUSH" || actionInfo.type === "REPLACE";
27
- if (typeof document !== "undefined" && blockers.length && isPushOrReplace) {
28
- for (const blocker of blockers) {
29
- const nextLocation = parseHref(actionInfo.path, actionInfo.state);
30
- const isBlocked = await blocker.blockerFn({
31
- currentLocation: location,
32
- nextLocation,
33
- action: actionInfo.type
34
- });
35
- if (isBlocked) {
36
- opts.onBlocked?.();
37
- return;
38
- }
39
- }
40
- }
41
- task();
42
- };
43
- return {
44
- get location() {
45
- return location;
46
- },
47
- get length() {
48
- return opts.getLength();
49
- },
50
- subscribers,
51
- subscribe: (cb) => {
52
- subscribers.add(cb);
53
- return () => {
54
- subscribers.delete(cb);
55
- };
56
- },
57
- push: (path, state, navigateOpts) => {
58
- const currentIndex = location.state[stateIndexKey];
59
- state = assignKeyAndIndex(currentIndex + 1, state);
60
- tryNavigation({
61
- task: () => {
62
- opts.pushState(path, state);
63
- notify({ type: "PUSH" });
64
- },
65
- navigateOpts,
66
- type: "PUSH",
67
- path,
68
- state
69
- });
70
- },
71
- replace: (path, state, navigateOpts) => {
72
- const currentIndex = location.state[stateIndexKey];
73
- state = assignKeyAndIndex(currentIndex, state);
74
- tryNavigation({
75
- task: () => {
76
- opts.replaceState(path, state);
77
- notify({ type: "REPLACE" });
78
- },
79
- navigateOpts,
80
- type: "REPLACE",
81
- path,
82
- state
83
- });
84
- },
85
- go: (index, navigateOpts) => {
86
- tryNavigation({
87
- task: () => {
88
- opts.go(index);
89
- handleIndexChange({ type: "GO", index });
90
- },
91
- navigateOpts,
92
- type: "GO"
93
- });
94
- },
95
- back: (navigateOpts) => {
96
- tryNavigation({
97
- task: () => {
98
- opts.back(navigateOpts?.ignoreBlocker ?? false);
99
- handleIndexChange({ type: "BACK" });
100
- },
101
- navigateOpts,
102
- type: "BACK"
103
- });
104
- },
105
- forward: (navigateOpts) => {
106
- tryNavigation({
107
- task: () => {
108
- opts.forward(navigateOpts?.ignoreBlocker ?? false);
109
- handleIndexChange({ type: "FORWARD" });
110
- },
111
- navigateOpts,
112
- type: "FORWARD"
113
- });
114
- },
115
- canGoBack: () => location.state[stateIndexKey] !== 0,
116
- createHref: (str) => opts.createHref(str),
117
- block: (blocker) => {
118
- if (!opts.setBlockers) return () => {
119
- };
120
- const blockers = opts.getBlockers?.() ?? [];
121
- opts.setBlockers([...blockers, blocker]);
122
- return () => {
123
- const blockers2 = opts.getBlockers?.() ?? [];
124
- opts.setBlockers?.(blockers2.filter((b) => b !== blocker));
125
- };
126
- },
127
- flush: () => opts.flush?.(),
128
- destroy: () => opts.destroy?.(),
129
- notify
130
- };
6
+ let location = opts.getLocation();
7
+ const subscribers = /* @__PURE__ */ new Set();
8
+ const notify = (action) => {
9
+ location = opts.getLocation();
10
+ subscribers.forEach((subscriber) => subscriber({
11
+ location,
12
+ action
13
+ }));
14
+ };
15
+ const handleIndexChange = (action) => {
16
+ if (opts.notifyOnIndexChange ?? true) notify(action);
17
+ else location = opts.getLocation();
18
+ };
19
+ const tryNavigation = async ({ task, navigateOpts, ...actionInfo }) => {
20
+ if (navigateOpts?.ignoreBlocker ?? false) {
21
+ task();
22
+ return;
23
+ }
24
+ const blockers = opts.getBlockers?.() ?? [];
25
+ const isPushOrReplace = actionInfo.type === "PUSH" || actionInfo.type === "REPLACE";
26
+ if (typeof document !== "undefined" && blockers.length && isPushOrReplace) for (const blocker of blockers) {
27
+ const nextLocation = parseHref(actionInfo.path, actionInfo.state);
28
+ if (await blocker.blockerFn({
29
+ currentLocation: location,
30
+ nextLocation,
31
+ action: actionInfo.type
32
+ })) {
33
+ opts.onBlocked?.();
34
+ return;
35
+ }
36
+ }
37
+ task();
38
+ };
39
+ return {
40
+ get location() {
41
+ return location;
42
+ },
43
+ get length() {
44
+ return opts.getLength();
45
+ },
46
+ subscribers,
47
+ subscribe: (cb) => {
48
+ subscribers.add(cb);
49
+ return () => {
50
+ subscribers.delete(cb);
51
+ };
52
+ },
53
+ push: (path, state, navigateOpts) => {
54
+ const currentIndex = location.state[stateIndexKey];
55
+ state = assignKeyAndIndex(currentIndex + 1, state);
56
+ tryNavigation({
57
+ task: () => {
58
+ opts.pushState(path, state);
59
+ notify({ type: "PUSH" });
60
+ },
61
+ navigateOpts,
62
+ type: "PUSH",
63
+ path,
64
+ state
65
+ });
66
+ },
67
+ replace: (path, state, navigateOpts) => {
68
+ const currentIndex = location.state[stateIndexKey];
69
+ state = assignKeyAndIndex(currentIndex, state);
70
+ tryNavigation({
71
+ task: () => {
72
+ opts.replaceState(path, state);
73
+ notify({ type: "REPLACE" });
74
+ },
75
+ navigateOpts,
76
+ type: "REPLACE",
77
+ path,
78
+ state
79
+ });
80
+ },
81
+ go: (index, navigateOpts) => {
82
+ tryNavigation({
83
+ task: () => {
84
+ opts.go(index);
85
+ handleIndexChange({
86
+ type: "GO",
87
+ index
88
+ });
89
+ },
90
+ navigateOpts,
91
+ type: "GO"
92
+ });
93
+ },
94
+ back: (navigateOpts) => {
95
+ tryNavigation({
96
+ task: () => {
97
+ opts.back(navigateOpts?.ignoreBlocker ?? false);
98
+ handleIndexChange({ type: "BACK" });
99
+ },
100
+ navigateOpts,
101
+ type: "BACK"
102
+ });
103
+ },
104
+ forward: (navigateOpts) => {
105
+ tryNavigation({
106
+ task: () => {
107
+ opts.forward(navigateOpts?.ignoreBlocker ?? false);
108
+ handleIndexChange({ type: "FORWARD" });
109
+ },
110
+ navigateOpts,
111
+ type: "FORWARD"
112
+ });
113
+ },
114
+ canGoBack: () => location.state[stateIndexKey] !== 0,
115
+ createHref: (str) => opts.createHref(str),
116
+ block: (blocker) => {
117
+ if (!opts.setBlockers) return () => {};
118
+ const blockers = opts.getBlockers?.() ?? [];
119
+ opts.setBlockers([...blockers, blocker]);
120
+ return () => {
121
+ const blockers = opts.getBlockers?.() ?? [];
122
+ opts.setBlockers?.(blockers.filter((b) => b !== blocker));
123
+ };
124
+ },
125
+ flush: () => opts.flush?.(),
126
+ destroy: () => opts.destroy?.(),
127
+ notify
128
+ };
131
129
  }
132
130
  function assignKeyAndIndex(index, state) {
133
- if (!state) {
134
- state = {};
135
- }
136
- const key = createRandomKey();
137
- return {
138
- ...state,
139
- key,
140
- // TODO: Remove in v2 - use __TSR_key instead
141
- __TSR_key: key,
142
- [stateIndexKey]: index
143
- };
131
+ if (!state) state = {};
132
+ const key = createRandomKey();
133
+ return {
134
+ ...state,
135
+ key,
136
+ __TSR_key: key,
137
+ [stateIndexKey]: index
138
+ };
144
139
  }
140
+ /**
141
+ * Creates a history object that can be used to interact with the browser's
142
+ * navigation. This is a lightweight API wrapping the browser's native methods.
143
+ * It is designed to work with TanStack Router, but could be used as a standalone API as well.
144
+ * IMPORTANT: This API implements history throttling via a microtask to prevent
145
+ * excessive calls to the history API. In some browsers, calling history.pushState or
146
+ * history.replaceState in quick succession can cause the browser to ignore subsequent
147
+ * calls. This API smooths out those differences and ensures that your application
148
+ * state will *eventually* match the browser state. In most cases, this is not a problem,
149
+ * but if you need to ensure that the browser state is up to date, you can use the
150
+ * `history.flush` method to immediately flush all pending state changes to the browser URL.
151
+ * @param opts
152
+ * @param opts.getHref A function that returns the current href (path + search + hash)
153
+ * @param opts.createHref A function that takes a path and returns a href (path + search + hash)
154
+ * @returns A history instance
155
+ */
145
156
  function createBrowserHistory(opts) {
146
- const win = opts?.window ?? (typeof document !== "undefined" ? window : void 0);
147
- const originalPushState = win.history.pushState;
148
- const originalReplaceState = win.history.replaceState;
149
- let blockers = [];
150
- const _getBlockers = () => blockers;
151
- const _setBlockers = (newBlockers) => blockers = newBlockers;
152
- const createHref = opts?.createHref ?? ((path) => path);
153
- const parseLocation = opts?.parseLocation ?? (() => parseHref(
154
- `${win.location.pathname}${win.location.search}${win.location.hash}`,
155
- win.history.state
156
- ));
157
- if (!win.history.state?.__TSR_key && !win.history.state?.key) {
158
- const addedKey = createRandomKey();
159
- win.history.replaceState(
160
- {
161
- [stateIndexKey]: 0,
162
- key: addedKey,
163
- // TODO: Remove in v2 - use __TSR_key instead
164
- __TSR_key: addedKey
165
- },
166
- ""
167
- );
168
- }
169
- let currentLocation = parseLocation();
170
- let rollbackLocation;
171
- let nextPopIsGo = false;
172
- let ignoreNextPop = false;
173
- let skipBlockerNextPop = false;
174
- let ignoreNextBeforeUnload = false;
175
- const getLocation = () => currentLocation;
176
- let next;
177
- let scheduled;
178
- const flush = () => {
179
- if (!next) {
180
- return;
181
- }
182
- history._ignoreSubscribers = true;
183
- (next.isPush ? win.history.pushState : win.history.replaceState)(
184
- next.state,
185
- "",
186
- next.href
187
- );
188
- history._ignoreSubscribers = false;
189
- next = void 0;
190
- scheduled = void 0;
191
- rollbackLocation = void 0;
192
- };
193
- const queueHistoryAction = (type, destHref, state) => {
194
- const href = createHref(destHref);
195
- if (!scheduled) {
196
- rollbackLocation = currentLocation;
197
- }
198
- currentLocation = parseHref(destHref, state);
199
- next = {
200
- href,
201
- state,
202
- isPush: next?.isPush || type === "push"
203
- };
204
- if (!scheduled) {
205
- scheduled = Promise.resolve().then(() => flush());
206
- }
207
- };
208
- const onPushPop = (type) => {
209
- currentLocation = parseLocation();
210
- history.notify({ type });
211
- };
212
- const onPushPopEvent = async () => {
213
- if (ignoreNextPop) {
214
- ignoreNextPop = false;
215
- return;
216
- }
217
- const nextLocation = parseLocation();
218
- const delta = nextLocation.state[stateIndexKey] - currentLocation.state[stateIndexKey];
219
- const isForward = delta === 1;
220
- const isBack = delta === -1;
221
- const isGo = !isForward && !isBack || nextPopIsGo;
222
- nextPopIsGo = false;
223
- const action = isGo ? "GO" : isBack ? "BACK" : "FORWARD";
224
- const notify = isGo ? {
225
- type: "GO",
226
- index: delta
227
- } : {
228
- type: isBack ? "BACK" : "FORWARD"
229
- };
230
- if (skipBlockerNextPop) {
231
- skipBlockerNextPop = false;
232
- } else {
233
- const blockers2 = _getBlockers();
234
- if (typeof document !== "undefined" && blockers2.length) {
235
- for (const blocker of blockers2) {
236
- const isBlocked = await blocker.blockerFn({
237
- currentLocation,
238
- nextLocation,
239
- action
240
- });
241
- if (isBlocked) {
242
- ignoreNextPop = true;
243
- win.history.go(1);
244
- history.notify(notify);
245
- return;
246
- }
247
- }
248
- }
249
- }
250
- currentLocation = parseLocation();
251
- history.notify(notify);
252
- };
253
- const onBeforeUnload = (e) => {
254
- if (ignoreNextBeforeUnload) {
255
- ignoreNextBeforeUnload = false;
256
- return;
257
- }
258
- let shouldBlock = false;
259
- const blockers2 = _getBlockers();
260
- if (typeof document !== "undefined" && blockers2.length) {
261
- for (const blocker of blockers2) {
262
- const shouldHaveBeforeUnload = blocker.enableBeforeUnload ?? true;
263
- if (shouldHaveBeforeUnload === true) {
264
- shouldBlock = true;
265
- break;
266
- }
267
- if (typeof shouldHaveBeforeUnload === "function" && shouldHaveBeforeUnload() === true) {
268
- shouldBlock = true;
269
- break;
270
- }
271
- }
272
- }
273
- if (shouldBlock) {
274
- e.preventDefault();
275
- return e.returnValue = "";
276
- }
277
- return;
278
- };
279
- const history = createHistory({
280
- getLocation,
281
- getLength: () => win.history.length,
282
- pushState: (href, state) => queueHistoryAction("push", href, state),
283
- replaceState: (href, state) => queueHistoryAction("replace", href, state),
284
- back: (ignoreBlocker) => {
285
- if (ignoreBlocker) skipBlockerNextPop = true;
286
- ignoreNextBeforeUnload = true;
287
- return win.history.back();
288
- },
289
- forward: (ignoreBlocker) => {
290
- if (ignoreBlocker) skipBlockerNextPop = true;
291
- ignoreNextBeforeUnload = true;
292
- win.history.forward();
293
- },
294
- go: (n) => {
295
- nextPopIsGo = true;
296
- win.history.go(n);
297
- },
298
- createHref: (href) => createHref(href),
299
- flush,
300
- destroy: () => {
301
- win.history.pushState = originalPushState;
302
- win.history.replaceState = originalReplaceState;
303
- win.removeEventListener(beforeUnloadEvent, onBeforeUnload, {
304
- capture: true
305
- });
306
- win.removeEventListener(popStateEvent, onPushPopEvent);
307
- },
308
- onBlocked: () => {
309
- if (rollbackLocation && currentLocation !== rollbackLocation) {
310
- currentLocation = rollbackLocation;
311
- }
312
- },
313
- getBlockers: _getBlockers,
314
- setBlockers: _setBlockers,
315
- notifyOnIndexChange: false
316
- });
317
- win.addEventListener(beforeUnloadEvent, onBeforeUnload, { capture: true });
318
- win.addEventListener(popStateEvent, onPushPopEvent);
319
- win.history.pushState = function(...args) {
320
- const res = originalPushState.apply(win.history, args);
321
- if (!history._ignoreSubscribers) onPushPop("PUSH");
322
- return res;
323
- };
324
- win.history.replaceState = function(...args) {
325
- const res = originalReplaceState.apply(win.history, args);
326
- if (!history._ignoreSubscribers) onPushPop("REPLACE");
327
- return res;
328
- };
329
- return history;
157
+ const win = opts?.window ?? (typeof document !== "undefined" ? window : void 0);
158
+ const originalPushState = win.history.pushState;
159
+ const originalReplaceState = win.history.replaceState;
160
+ let blockers = [];
161
+ const _getBlockers = () => blockers;
162
+ const _setBlockers = (newBlockers) => blockers = newBlockers;
163
+ const createHref = opts?.createHref ?? ((path) => path);
164
+ const parseLocation = opts?.parseLocation ?? (() => parseHref(`${win.location.pathname}${win.location.search}${win.location.hash}`, win.history.state));
165
+ if (!win.history.state?.__TSR_key && !win.history.state?.key) {
166
+ const addedKey = createRandomKey();
167
+ win.history.replaceState({
168
+ [stateIndexKey]: 0,
169
+ key: addedKey,
170
+ __TSR_key: addedKey
171
+ }, "");
172
+ }
173
+ let currentLocation = parseLocation();
174
+ let rollbackLocation;
175
+ let nextPopIsGo = false;
176
+ let ignoreNextPop = false;
177
+ let skipBlockerNextPop = false;
178
+ let ignoreNextBeforeUnload = false;
179
+ const getLocation = () => currentLocation;
180
+ let next;
181
+ let scheduled;
182
+ const flush = () => {
183
+ if (!next) return;
184
+ history._ignoreSubscribers = true;
185
+ (next.isPush ? win.history.pushState : win.history.replaceState)(next.state, "", next.href);
186
+ history._ignoreSubscribers = false;
187
+ next = void 0;
188
+ scheduled = void 0;
189
+ rollbackLocation = void 0;
190
+ };
191
+ const queueHistoryAction = (type, destHref, state) => {
192
+ const href = createHref(destHref);
193
+ if (!scheduled) rollbackLocation = currentLocation;
194
+ currentLocation = parseHref(destHref, state);
195
+ next = {
196
+ href,
197
+ state,
198
+ isPush: next?.isPush || type === "push"
199
+ };
200
+ if (!scheduled) scheduled = Promise.resolve().then(() => flush());
201
+ };
202
+ const onPushPop = (type) => {
203
+ currentLocation = parseLocation();
204
+ history.notify({ type });
205
+ };
206
+ const onPushPopEvent = async () => {
207
+ if (ignoreNextPop) {
208
+ ignoreNextPop = false;
209
+ return;
210
+ }
211
+ const nextLocation = parseLocation();
212
+ const delta = nextLocation.state[stateIndexKey] - currentLocation.state[stateIndexKey];
213
+ const isForward = delta === 1;
214
+ const isBack = delta === -1;
215
+ const isGo = !isForward && !isBack || nextPopIsGo;
216
+ nextPopIsGo = false;
217
+ const action = isGo ? "GO" : isBack ? "BACK" : "FORWARD";
218
+ const notify = isGo ? {
219
+ type: "GO",
220
+ index: delta
221
+ } : { type: isBack ? "BACK" : "FORWARD" };
222
+ if (skipBlockerNextPop) skipBlockerNextPop = false;
223
+ else {
224
+ const blockers = _getBlockers();
225
+ if (typeof document !== "undefined" && blockers.length) {
226
+ for (const blocker of blockers) if (await blocker.blockerFn({
227
+ currentLocation,
228
+ nextLocation,
229
+ action
230
+ })) {
231
+ ignoreNextPop = true;
232
+ win.history.go(1);
233
+ history.notify(notify);
234
+ return;
235
+ }
236
+ }
237
+ }
238
+ currentLocation = parseLocation();
239
+ history.notify(notify);
240
+ };
241
+ const onBeforeUnload = (e) => {
242
+ if (ignoreNextBeforeUnload) {
243
+ ignoreNextBeforeUnload = false;
244
+ return;
245
+ }
246
+ let shouldBlock = false;
247
+ const blockers = _getBlockers();
248
+ if (typeof document !== "undefined" && blockers.length) for (const blocker of blockers) {
249
+ const shouldHaveBeforeUnload = blocker.enableBeforeUnload ?? true;
250
+ if (shouldHaveBeforeUnload === true) {
251
+ shouldBlock = true;
252
+ break;
253
+ }
254
+ if (typeof shouldHaveBeforeUnload === "function" && shouldHaveBeforeUnload() === true) {
255
+ shouldBlock = true;
256
+ break;
257
+ }
258
+ }
259
+ if (shouldBlock) {
260
+ e.preventDefault();
261
+ return e.returnValue = "";
262
+ }
263
+ };
264
+ const history = createHistory({
265
+ getLocation,
266
+ getLength: () => win.history.length,
267
+ pushState: (href, state) => queueHistoryAction("push", href, state),
268
+ replaceState: (href, state) => queueHistoryAction("replace", href, state),
269
+ back: (ignoreBlocker) => {
270
+ if (ignoreBlocker) skipBlockerNextPop = true;
271
+ ignoreNextBeforeUnload = true;
272
+ return win.history.back();
273
+ },
274
+ forward: (ignoreBlocker) => {
275
+ if (ignoreBlocker) skipBlockerNextPop = true;
276
+ ignoreNextBeforeUnload = true;
277
+ win.history.forward();
278
+ },
279
+ go: (n) => {
280
+ nextPopIsGo = true;
281
+ win.history.go(n);
282
+ },
283
+ createHref: (href) => createHref(href),
284
+ flush,
285
+ destroy: () => {
286
+ win.history.pushState = originalPushState;
287
+ win.history.replaceState = originalReplaceState;
288
+ win.removeEventListener(beforeUnloadEvent, onBeforeUnload, { capture: true });
289
+ win.removeEventListener(popStateEvent, onPushPopEvent);
290
+ },
291
+ onBlocked: () => {
292
+ if (rollbackLocation && currentLocation !== rollbackLocation) currentLocation = rollbackLocation;
293
+ },
294
+ getBlockers: _getBlockers,
295
+ setBlockers: _setBlockers,
296
+ notifyOnIndexChange: false
297
+ });
298
+ win.addEventListener(beforeUnloadEvent, onBeforeUnload, { capture: true });
299
+ win.addEventListener(popStateEvent, onPushPopEvent);
300
+ win.history.pushState = function(...args) {
301
+ const res = originalPushState.apply(win.history, args);
302
+ if (!history._ignoreSubscribers) onPushPop("PUSH");
303
+ return res;
304
+ };
305
+ win.history.replaceState = function(...args) {
306
+ const res = originalReplaceState.apply(win.history, args);
307
+ if (!history._ignoreSubscribers) onPushPop("REPLACE");
308
+ return res;
309
+ };
310
+ return history;
330
311
  }
312
+ /**
313
+ * Create a hash-based history implementation.
314
+ * Useful for static hosts or environments without server URL rewriting.
315
+ * @link https://tanstack.com/router/latest/docs/framework/react/guide/history-types
316
+ */
331
317
  function createHashHistory(opts) {
332
- const win = opts?.window ?? (typeof document !== "undefined" ? window : void 0);
333
- return createBrowserHistory({
334
- window: win,
335
- parseLocation: () => {
336
- const hashSplit = win.location.hash.split("#").slice(1);
337
- const pathPart = hashSplit[0] ?? "/";
338
- const searchPart = win.location.search;
339
- const hashEntries = hashSplit.slice(1);
340
- const hashPart = hashEntries.length === 0 ? "" : `#${hashEntries.join("#")}`;
341
- const hashHref = `${pathPart}${searchPart}${hashPart}`;
342
- return parseHref(hashHref, win.history.state);
343
- },
344
- createHref: (href) => `${win.location.pathname}${win.location.search}#${href}`
345
- });
318
+ const win = opts?.window ?? (typeof document !== "undefined" ? window : void 0);
319
+ return createBrowserHistory({
320
+ window: win,
321
+ parseLocation: () => {
322
+ const hashSplit = win.location.hash.split("#").slice(1);
323
+ const pathPart = hashSplit[0] ?? "/";
324
+ const searchPart = win.location.search;
325
+ const hashEntries = hashSplit.slice(1);
326
+ return parseHref(`${pathPart}${searchPart}${hashEntries.length === 0 ? "" : `#${hashEntries.join("#")}`}`, win.history.state);
327
+ },
328
+ createHref: (href) => `${win.location.pathname}${win.location.search}#${href}`
329
+ });
346
330
  }
347
- function createMemoryHistory(opts = {
348
- initialEntries: ["/"]
349
- }) {
350
- const entries = opts.initialEntries;
351
- let index = opts.initialIndex ? Math.min(Math.max(opts.initialIndex, 0), entries.length - 1) : entries.length - 1;
352
- const states = entries.map(
353
- (_entry, index2) => assignKeyAndIndex(index2, void 0)
354
- );
355
- const getLocation = () => parseHref(entries[index], states[index]);
356
- let blockers = [];
357
- const _getBlockers = () => blockers;
358
- const _setBlockers = (newBlockers) => blockers = newBlockers;
359
- return createHistory({
360
- getLocation,
361
- getLength: () => entries.length,
362
- pushState: (path, state) => {
363
- if (index < entries.length - 1) {
364
- entries.splice(index + 1);
365
- states.splice(index + 1);
366
- }
367
- states.push(state);
368
- entries.push(path);
369
- index = Math.max(entries.length - 1, 0);
370
- },
371
- replaceState: (path, state) => {
372
- states[index] = state;
373
- entries[index] = path;
374
- },
375
- back: () => {
376
- index = Math.max(index - 1, 0);
377
- },
378
- forward: () => {
379
- index = Math.min(index + 1, entries.length - 1);
380
- },
381
- go: (n) => {
382
- index = Math.min(Math.max(index + n, 0), entries.length - 1);
383
- },
384
- createHref: (path) => path,
385
- getBlockers: _getBlockers,
386
- setBlockers: _setBlockers
387
- });
331
+ /**
332
+ * Create an in-memory history implementation.
333
+ * Ideal for server rendering, tests, and non-DOM environments.
334
+ * @link https://tanstack.com/router/latest/docs/framework/react/guide/history-types
335
+ */
336
+ function createMemoryHistory(opts = { initialEntries: ["/"] }) {
337
+ const entries = opts.initialEntries;
338
+ let index = opts.initialIndex ? Math.min(Math.max(opts.initialIndex, 0), entries.length - 1) : entries.length - 1;
339
+ const states = entries.map((_entry, index) => assignKeyAndIndex(index, void 0));
340
+ const getLocation = () => parseHref(entries[index], states[index]);
341
+ let blockers = [];
342
+ const _getBlockers = () => blockers;
343
+ const _setBlockers = (newBlockers) => blockers = newBlockers;
344
+ return createHistory({
345
+ getLocation,
346
+ getLength: () => entries.length,
347
+ pushState: (path, state) => {
348
+ if (index < entries.length - 1) {
349
+ entries.splice(index + 1);
350
+ states.splice(index + 1);
351
+ }
352
+ states.push(state);
353
+ entries.push(path);
354
+ index = Math.max(entries.length - 1, 0);
355
+ },
356
+ replaceState: (path, state) => {
357
+ states[index] = state;
358
+ entries[index] = path;
359
+ },
360
+ back: () => {
361
+ index = Math.max(index - 1, 0);
362
+ },
363
+ forward: () => {
364
+ index = Math.min(index + 1, entries.length - 1);
365
+ },
366
+ go: (n) => {
367
+ index = Math.min(Math.max(index + n, 0), entries.length - 1);
368
+ },
369
+ createHref: (path) => path,
370
+ getBlockers: _getBlockers,
371
+ setBlockers: _setBlockers
372
+ });
388
373
  }
374
+ /**
375
+ * Sanitize a path to prevent open redirect vulnerabilities.
376
+ * Removes control characters and collapses leading double slashes.
377
+ */
389
378
  function sanitizePath(path) {
390
- let sanitized = path.replace(/[\x00-\x1f\x7f]/g, "");
391
- if (sanitized.startsWith("//")) {
392
- sanitized = "/" + sanitized.replace(/^\/+/, "");
393
- }
394
- return sanitized;
379
+ let sanitized = path.replace(/[\x00-\x1f\x7f]/g, "");
380
+ if (sanitized.startsWith("//")) sanitized = "/" + sanitized.replace(/^\/+/, "");
381
+ return sanitized;
395
382
  }
396
383
  function parseHref(href, state) {
397
- const sanitizedHref = sanitizePath(href);
398
- const hashIndex = sanitizedHref.indexOf("#");
399
- const searchIndex = sanitizedHref.indexOf("?");
400
- const addedKey = createRandomKey();
401
- return {
402
- href: sanitizedHref,
403
- pathname: sanitizedHref.substring(
404
- 0,
405
- hashIndex > 0 ? searchIndex > 0 ? Math.min(hashIndex, searchIndex) : hashIndex : searchIndex > 0 ? searchIndex : sanitizedHref.length
406
- ),
407
- hash: hashIndex > -1 ? sanitizedHref.substring(hashIndex) : "",
408
- search: searchIndex > -1 ? sanitizedHref.slice(
409
- searchIndex,
410
- hashIndex === -1 ? void 0 : hashIndex
411
- ) : "",
412
- state: state || { [stateIndexKey]: 0, key: addedKey, __TSR_key: addedKey }
413
- };
384
+ const sanitizedHref = sanitizePath(href);
385
+ const hashIndex = sanitizedHref.indexOf("#");
386
+ const searchIndex = sanitizedHref.indexOf("?");
387
+ const addedKey = createRandomKey();
388
+ return {
389
+ href: sanitizedHref,
390
+ pathname: sanitizedHref.substring(0, hashIndex > 0 ? searchIndex > 0 ? Math.min(hashIndex, searchIndex) : hashIndex : searchIndex > 0 ? searchIndex : sanitizedHref.length),
391
+ hash: hashIndex > -1 ? sanitizedHref.substring(hashIndex) : "",
392
+ search: searchIndex > -1 ? sanitizedHref.slice(searchIndex, hashIndex === -1 ? void 0 : hashIndex) : "",
393
+ state: state || {
394
+ [stateIndexKey]: 0,
395
+ key: addedKey,
396
+ __TSR_key: addedKey
397
+ }
398
+ };
414
399
  }
415
400
  function createRandomKey() {
416
- return (Math.random() + 1).toString(36).substring(7);
401
+ return (Math.random() + 1).toString(36).substring(7);
417
402
  }
418
- export {
419
- createBrowserHistory,
420
- createHashHistory,
421
- createHistory,
422
- createMemoryHistory,
423
- parseHref
424
- };
425
- //# sourceMappingURL=index.js.map
403
+ //#endregion
404
+ export { createBrowserHistory, createHashHistory, createHistory, createMemoryHistory, parseHref };
405
+
406
+ //# sourceMappingURL=index.js.map