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