@tanstack/react-router 0.0.1-beta.223 → 0.0.1-beta.224
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/cjs/RouterProvider.js +56 -958
- package/build/cjs/RouterProvider.js.map +1 -1
- package/build/cjs/fileRoute.js.map +1 -1
- package/build/cjs/route.js.map +1 -1
- package/build/cjs/router.js +953 -39
- package/build/cjs/router.js.map +1 -1
- package/build/cjs/scroll-restoration.js +5 -9
- package/build/cjs/scroll-restoration.js.map +1 -1
- package/build/esm/index.js +887 -884
- package/build/esm/index.js.map +1 -1
- package/build/stats-html.html +1 -1
- package/build/stats-react.json +369 -363
- package/build/types/RouterProvider.d.ts +3 -22
- package/build/types/fileRoute.d.ts +2 -2
- package/build/types/route.d.ts +1 -0
- package/build/types/router.d.ts +50 -5
- package/build/umd/index.development.js +887 -884
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +2 -2
- package/build/umd/index.production.js.map +1 -1
- package/package.json +2 -2
- package/src/RouterProvider.tsx +57 -1314
- package/src/fileRoute.ts +1 -1
- package/src/route.ts +22 -0
- package/src/router.ts +1320 -45
- package/src/scroll-restoration.tsx +5 -5
package/build/cjs/router.js
CHANGED
|
@@ -12,28 +12,155 @@
|
|
|
12
12
|
|
|
13
13
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
14
14
|
|
|
15
|
+
var history = require('@tanstack/history');
|
|
15
16
|
var searchParams = require('./searchParams.js');
|
|
17
|
+
var utils = require('./utils.js');
|
|
18
|
+
var RouterProvider = require('./RouterProvider.js');
|
|
19
|
+
var path = require('./path.js');
|
|
20
|
+
var invariant = require('tiny-invariant');
|
|
21
|
+
var redirects = require('./redirects.js');
|
|
22
|
+
var warning = require('tiny-warning');
|
|
16
23
|
|
|
17
|
-
|
|
24
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
25
|
+
|
|
26
|
+
var invariant__default = /*#__PURE__*/_interopDefaultLegacy(invariant);
|
|
27
|
+
var warning__default = /*#__PURE__*/_interopDefaultLegacy(warning);
|
|
18
28
|
|
|
19
29
|
//
|
|
20
30
|
|
|
21
31
|
const componentTypes = ['component', 'errorComponent', 'pendingComponent'];
|
|
32
|
+
const preloadWarning = 'Error preloading route! ☝️';
|
|
22
33
|
class Router {
|
|
23
|
-
//
|
|
24
|
-
|
|
25
|
-
|
|
34
|
+
// Option-independent properties
|
|
35
|
+
tempLocationKey = `${Math.round(Math.random() * 10000000)}`;
|
|
36
|
+
resetNextScroll = true;
|
|
37
|
+
navigateTimeout = null;
|
|
38
|
+
latestLoadPromise = Promise.resolve();
|
|
39
|
+
subscribers = new Set();
|
|
40
|
+
pendingMatches = [];
|
|
41
|
+
injectedHtml = [];
|
|
42
|
+
|
|
43
|
+
// Must build in constructor
|
|
44
|
+
|
|
26
45
|
constructor(options) {
|
|
27
|
-
this.
|
|
46
|
+
this.updateOptions({
|
|
28
47
|
defaultPreloadDelay: 50,
|
|
29
48
|
context: undefined,
|
|
30
49
|
...options,
|
|
31
50
|
stringifySearch: options?.stringifySearch ?? searchParams.defaultStringifySearch,
|
|
32
51
|
parseSearch: options?.parseSearch ?? searchParams.defaultParseSearch
|
|
33
|
-
};
|
|
34
|
-
this.routeTree = this.options.routeTree;
|
|
52
|
+
});
|
|
35
53
|
}
|
|
36
|
-
|
|
54
|
+
startReactTransition = () => {
|
|
55
|
+
warning__default["default"](false, 'startReactTransition implementation is missing. If you see this, please file an issue.');
|
|
56
|
+
};
|
|
57
|
+
setState = () => {
|
|
58
|
+
warning__default["default"](false, 'setState implementation is missing. If you see this, please file an issue.');
|
|
59
|
+
};
|
|
60
|
+
updateOptions = newOptions => {
|
|
61
|
+
this.options;
|
|
62
|
+
this.options = {
|
|
63
|
+
...this.options,
|
|
64
|
+
...newOptions
|
|
65
|
+
};
|
|
66
|
+
this.basepath = `/${path.trimPath(newOptions.basepath ?? '') ?? ''}`;
|
|
67
|
+
if (!this.history || this.options.history && this.options.history !== this.history) {
|
|
68
|
+
this.history = this.options.history ?? history.createBrowserHistory();
|
|
69
|
+
this.latestLocation = this.parseLocation();
|
|
70
|
+
}
|
|
71
|
+
if (this.options.routeTree !== this.routeTree) {
|
|
72
|
+
this.routeTree = this.options.routeTree;
|
|
73
|
+
this.buildRouteTree();
|
|
74
|
+
}
|
|
75
|
+
if (!this.state) {
|
|
76
|
+
this.state = RouterProvider.getInitialRouterState(this.latestLocation);
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
buildRouteTree = () => {
|
|
80
|
+
this.routesById = {};
|
|
81
|
+
this.routesByPath = {};
|
|
82
|
+
const recurseRoutes = childRoutes => {
|
|
83
|
+
childRoutes.forEach((childRoute, i) => {
|
|
84
|
+
// if (typeof childRoute === 'function') {
|
|
85
|
+
// childRoute = (childRoute as any)()
|
|
86
|
+
// }
|
|
87
|
+
childRoute.init({
|
|
88
|
+
originalIndex: i
|
|
89
|
+
});
|
|
90
|
+
const existingRoute = this.routesById[childRoute.id];
|
|
91
|
+
invariant__default["default"](!existingRoute, `Duplicate routes found with id: ${String(childRoute.id)}`);
|
|
92
|
+
this.routesById[childRoute.id] = childRoute;
|
|
93
|
+
if (!childRoute.isRoot && childRoute.path) {
|
|
94
|
+
const trimmedFullPath = path.trimPathRight(childRoute.fullPath);
|
|
95
|
+
if (!this.routesByPath[trimmedFullPath] || childRoute.fullPath.endsWith('/')) {
|
|
96
|
+
this.routesByPath[trimmedFullPath] = childRoute;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
const children = childRoute.children;
|
|
100
|
+
if (children?.length) {
|
|
101
|
+
recurseRoutes(children);
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
};
|
|
105
|
+
recurseRoutes([this.routeTree]);
|
|
106
|
+
this.flatRoutes = Object.values(this.routesByPath).map((d, i) => {
|
|
107
|
+
const trimmed = path.trimPath(d.fullPath);
|
|
108
|
+
const parsed = path.parsePathname(trimmed);
|
|
109
|
+
while (parsed.length > 1 && parsed[0]?.value === '/') {
|
|
110
|
+
parsed.shift();
|
|
111
|
+
}
|
|
112
|
+
const score = parsed.map(d => {
|
|
113
|
+
if (d.type === 'param') {
|
|
114
|
+
return 0.5;
|
|
115
|
+
}
|
|
116
|
+
if (d.type === 'wildcard') {
|
|
117
|
+
return 0.25;
|
|
118
|
+
}
|
|
119
|
+
return 1;
|
|
120
|
+
});
|
|
121
|
+
return {
|
|
122
|
+
child: d,
|
|
123
|
+
trimmed,
|
|
124
|
+
parsed,
|
|
125
|
+
index: i,
|
|
126
|
+
score
|
|
127
|
+
};
|
|
128
|
+
}).sort((a, b) => {
|
|
129
|
+
let isIndex = a.trimmed === '/' ? 1 : b.trimmed === '/' ? -1 : 0;
|
|
130
|
+
if (isIndex !== 0) return isIndex;
|
|
131
|
+
const length = Math.min(a.score.length, b.score.length);
|
|
132
|
+
|
|
133
|
+
// Sort by length of score
|
|
134
|
+
if (a.score.length !== b.score.length) {
|
|
135
|
+
return b.score.length - a.score.length;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// Sort by min available score
|
|
139
|
+
for (let i = 0; i < length; i++) {
|
|
140
|
+
if (a.score[i] !== b.score[i]) {
|
|
141
|
+
return b.score[i] - a.score[i];
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// Sort by min available parsed value
|
|
146
|
+
for (let i = 0; i < length; i++) {
|
|
147
|
+
if (a.parsed[i].value !== b.parsed[i].value) {
|
|
148
|
+
return a.parsed[i].value > b.parsed[i].value ? 1 : -1;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Sort by length of trimmed full path
|
|
153
|
+
if (a.trimmed !== b.trimmed) {
|
|
154
|
+
return a.trimmed > b.trimmed ? 1 : -1;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// Sort by original index
|
|
158
|
+
return a.index - b.index;
|
|
159
|
+
}).map((d, i) => {
|
|
160
|
+
d.child.rank = i;
|
|
161
|
+
return d.child;
|
|
162
|
+
});
|
|
163
|
+
};
|
|
37
164
|
subscribe = (eventType, fn) => {
|
|
38
165
|
const listener = {
|
|
39
166
|
eventType,
|
|
@@ -51,11 +178,823 @@ class Router {
|
|
|
51
178
|
}
|
|
52
179
|
});
|
|
53
180
|
};
|
|
181
|
+
checkLatest = promise => {
|
|
182
|
+
return this.latestLoadPromise !== promise ? this.latestLoadPromise : undefined;
|
|
183
|
+
};
|
|
184
|
+
parseLocation = previousLocation => {
|
|
185
|
+
const parse = ({
|
|
186
|
+
pathname,
|
|
187
|
+
search,
|
|
188
|
+
hash,
|
|
189
|
+
state
|
|
190
|
+
}) => {
|
|
191
|
+
const parsedSearch = this.options.parseSearch(search);
|
|
192
|
+
return {
|
|
193
|
+
pathname: pathname,
|
|
194
|
+
searchStr: search,
|
|
195
|
+
search: utils.replaceEqualDeep(previousLocation?.search, parsedSearch),
|
|
196
|
+
hash: hash.split('#').reverse()[0] ?? '',
|
|
197
|
+
href: `${pathname}${search}${hash}`,
|
|
198
|
+
state: utils.replaceEqualDeep(previousLocation?.state, state)
|
|
199
|
+
};
|
|
200
|
+
};
|
|
201
|
+
const location = parse(this.history.location);
|
|
202
|
+
let {
|
|
203
|
+
__tempLocation,
|
|
204
|
+
__tempKey
|
|
205
|
+
} = location.state;
|
|
206
|
+
if (__tempLocation && (!__tempKey || __tempKey === this.tempLocationKey)) {
|
|
207
|
+
// Sync up the location keys
|
|
208
|
+
const parsedTempLocation = parse(__tempLocation);
|
|
209
|
+
parsedTempLocation.state.key = location.state.key;
|
|
210
|
+
delete parsedTempLocation.state.__tempLocation;
|
|
211
|
+
return {
|
|
212
|
+
...parsedTempLocation,
|
|
213
|
+
maskedLocation: location
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
return location;
|
|
217
|
+
};
|
|
218
|
+
resolvePathWithBase = (from, path$1) => {
|
|
219
|
+
return path.resolvePath(this.basepath, from, path.cleanPath(path$1));
|
|
220
|
+
};
|
|
221
|
+
get looseRoutesById() {
|
|
222
|
+
return this.routesById;
|
|
223
|
+
}
|
|
224
|
+
matchRoutes = (pathname, locationSearch, opts) => {
|
|
225
|
+
let routeParams = {};
|
|
226
|
+
let foundRoute = this.flatRoutes.find(route => {
|
|
227
|
+
const matchedParams = path.matchPathname(this.basepath, path.trimPathRight(pathname), {
|
|
228
|
+
to: route.fullPath,
|
|
229
|
+
caseSensitive: route.options.caseSensitive ?? this.options.caseSensitive,
|
|
230
|
+
fuzzy: false
|
|
231
|
+
});
|
|
232
|
+
if (matchedParams) {
|
|
233
|
+
routeParams = matchedParams;
|
|
234
|
+
return true;
|
|
235
|
+
}
|
|
236
|
+
return false;
|
|
237
|
+
});
|
|
238
|
+
let routeCursor = foundRoute || this.routesById['__root__'];
|
|
239
|
+
let matchedRoutes = [routeCursor];
|
|
240
|
+
// let includingLayouts = true
|
|
241
|
+
while (routeCursor?.parentRoute) {
|
|
242
|
+
routeCursor = routeCursor.parentRoute;
|
|
243
|
+
if (routeCursor) matchedRoutes.unshift(routeCursor);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// Existing matches are matches that are already loaded along with
|
|
247
|
+
// pending matches that are still loading
|
|
248
|
+
|
|
249
|
+
const parseErrors = matchedRoutes.map(route => {
|
|
250
|
+
let parsedParamsError;
|
|
251
|
+
if (route.options.parseParams) {
|
|
252
|
+
try {
|
|
253
|
+
const parsedParams = route.options.parseParams(routeParams);
|
|
254
|
+
// Add the parsed params to the accumulated params bag
|
|
255
|
+
Object.assign(routeParams, parsedParams);
|
|
256
|
+
} catch (err) {
|
|
257
|
+
parsedParamsError = new RouterProvider.PathParamError(err.message, {
|
|
258
|
+
cause: err
|
|
259
|
+
});
|
|
260
|
+
if (opts?.throwOnError) {
|
|
261
|
+
throw parsedParamsError;
|
|
262
|
+
}
|
|
263
|
+
return parsedParamsError;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
return;
|
|
267
|
+
});
|
|
268
|
+
const matches = matchedRoutes.map((route, index) => {
|
|
269
|
+
const interpolatedPath = path.interpolatePath(route.path, routeParams);
|
|
270
|
+
const matchId = path.interpolatePath(route.id, routeParams, true);
|
|
271
|
+
|
|
272
|
+
// Waste not, want not. If we already have a match for this route,
|
|
273
|
+
// reuse it. This is important for layout routes, which might stick
|
|
274
|
+
// around between navigation actions that only change leaf routes.
|
|
275
|
+
const existingMatch = RouterProvider.getRouteMatch(this.state, matchId);
|
|
276
|
+
const cause = this.state.matches.find(d => d.id === matchId) ? 'stay' : 'enter';
|
|
277
|
+
if (existingMatch) {
|
|
278
|
+
return {
|
|
279
|
+
...existingMatch,
|
|
280
|
+
cause
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// Create a fresh route match
|
|
285
|
+
const hasLoaders = !!(route.options.loader || componentTypes.some(d => route.options[d]?.preload));
|
|
286
|
+
const routeMatch = {
|
|
287
|
+
id: matchId,
|
|
288
|
+
routeId: route.id,
|
|
289
|
+
params: routeParams,
|
|
290
|
+
pathname: path.joinPaths([this.basepath, interpolatedPath]),
|
|
291
|
+
updatedAt: Date.now(),
|
|
292
|
+
routeSearch: {},
|
|
293
|
+
search: {},
|
|
294
|
+
status: hasLoaders ? 'pending' : 'success',
|
|
295
|
+
isFetching: false,
|
|
296
|
+
invalid: false,
|
|
297
|
+
error: undefined,
|
|
298
|
+
paramsError: parseErrors[index],
|
|
299
|
+
searchError: undefined,
|
|
300
|
+
loadPromise: Promise.resolve(),
|
|
301
|
+
context: undefined,
|
|
302
|
+
abortController: new AbortController(),
|
|
303
|
+
shouldReloadDeps: undefined,
|
|
304
|
+
fetchedAt: 0,
|
|
305
|
+
cause
|
|
306
|
+
};
|
|
307
|
+
return routeMatch;
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
// Take each match and resolve its search params and context
|
|
311
|
+
// This has to happen after the matches are created or found
|
|
312
|
+
// so that we can use the parent match's search params and context
|
|
313
|
+
matches.forEach((match, i) => {
|
|
314
|
+
const parentMatch = matches[i - 1];
|
|
315
|
+
const route = this.looseRoutesById[match.routeId];
|
|
316
|
+
const searchInfo = (() => {
|
|
317
|
+
// Validate the search params and stabilize them
|
|
318
|
+
const parentSearchInfo = {
|
|
319
|
+
search: parentMatch?.search ?? locationSearch,
|
|
320
|
+
routeSearch: parentMatch?.routeSearch ?? locationSearch
|
|
321
|
+
};
|
|
322
|
+
try {
|
|
323
|
+
const validator = typeof route.options.validateSearch === 'object' ? route.options.validateSearch.parse : route.options.validateSearch;
|
|
324
|
+
let routeSearch = validator?.(parentSearchInfo.search) ?? {};
|
|
325
|
+
let search = {
|
|
326
|
+
...parentSearchInfo.search,
|
|
327
|
+
...routeSearch
|
|
328
|
+
};
|
|
329
|
+
routeSearch = utils.replaceEqualDeep(match.routeSearch, routeSearch);
|
|
330
|
+
search = utils.replaceEqualDeep(match.search, search);
|
|
331
|
+
return {
|
|
332
|
+
routeSearch,
|
|
333
|
+
search,
|
|
334
|
+
searchDidChange: match.routeSearch !== routeSearch
|
|
335
|
+
};
|
|
336
|
+
} catch (err) {
|
|
337
|
+
match.searchError = new RouterProvider.SearchParamError(err.message, {
|
|
338
|
+
cause: err
|
|
339
|
+
});
|
|
340
|
+
if (opts?.throwOnError) {
|
|
341
|
+
throw match.searchError;
|
|
342
|
+
}
|
|
343
|
+
return parentSearchInfo;
|
|
344
|
+
}
|
|
345
|
+
})();
|
|
346
|
+
Object.assign(match, searchInfo);
|
|
347
|
+
});
|
|
348
|
+
return matches;
|
|
349
|
+
};
|
|
350
|
+
cancelMatch = id => {
|
|
351
|
+
RouterProvider.getRouteMatch(this.state, id)?.abortController?.abort();
|
|
352
|
+
};
|
|
353
|
+
cancelMatches = () => {
|
|
354
|
+
this.state.matches.forEach(match => {
|
|
355
|
+
this.cancelMatch(match.id);
|
|
356
|
+
});
|
|
357
|
+
};
|
|
358
|
+
buildLocation = opts => {
|
|
359
|
+
const build = (dest = {}, matches) => {
|
|
360
|
+
const from = this.latestLocation;
|
|
361
|
+
const fromPathname = dest.from ?? from.pathname;
|
|
362
|
+
let pathname = this.resolvePathWithBase(fromPathname, `${dest.to ?? ''}`);
|
|
363
|
+
const fromMatches = this.matchRoutes(fromPathname, from.search);
|
|
364
|
+
const stayingMatches = matches?.filter(d => fromMatches?.find(e => e.routeId === d.routeId));
|
|
365
|
+
const prevParams = {
|
|
366
|
+
...utils.last(fromMatches)?.params
|
|
367
|
+
};
|
|
368
|
+
let nextParams = (dest.params ?? true) === true ? prevParams : utils.functionalUpdate(dest.params, prevParams);
|
|
369
|
+
if (nextParams) {
|
|
370
|
+
matches?.map(d => this.looseRoutesById[d.routeId].options.stringifyParams).filter(Boolean).forEach(fn => {
|
|
371
|
+
nextParams = {
|
|
372
|
+
...nextParams,
|
|
373
|
+
...fn(nextParams)
|
|
374
|
+
};
|
|
375
|
+
});
|
|
376
|
+
}
|
|
377
|
+
pathname = path.interpolatePath(pathname, nextParams ?? {});
|
|
378
|
+
const preSearchFilters = stayingMatches?.map(match => this.looseRoutesById[match.routeId].options.preSearchFilters ?? []).flat().filter(Boolean) ?? [];
|
|
379
|
+
const postSearchFilters = stayingMatches?.map(match => this.looseRoutesById[match.routeId].options.postSearchFilters ?? []).flat().filter(Boolean) ?? [];
|
|
380
|
+
|
|
381
|
+
// Pre filters first
|
|
382
|
+
const preFilteredSearch = preSearchFilters?.length ? preSearchFilters?.reduce((prev, next) => next(prev), from.search) : from.search;
|
|
383
|
+
|
|
384
|
+
// Then the link/navigate function
|
|
385
|
+
const destSearch = dest.search === true ? preFilteredSearch // Preserve resolvedFrom true
|
|
386
|
+
: dest.search ? utils.functionalUpdate(dest.search, preFilteredSearch) ?? {} // Updater
|
|
387
|
+
: preSearchFilters?.length ? preFilteredSearch // Preserve resolvedFrom filters
|
|
388
|
+
: {};
|
|
389
|
+
|
|
390
|
+
// Then post filters
|
|
391
|
+
const postFilteredSearch = postSearchFilters?.length ? postSearchFilters.reduce((prev, next) => next(prev), destSearch) : destSearch;
|
|
392
|
+
const search = utils.replaceEqualDeep(from.search, postFilteredSearch);
|
|
393
|
+
const searchStr = this.options.stringifySearch(search);
|
|
394
|
+
const hash = dest.hash === true ? from.hash : dest.hash ? utils.functionalUpdate(dest.hash, from.hash) : from.hash;
|
|
395
|
+
const hashStr = hash ? `#${hash}` : '';
|
|
396
|
+
let nextState = dest.state === true ? from.state : dest.state ? utils.functionalUpdate(dest.state, from.state) : from.state;
|
|
397
|
+
nextState = utils.replaceEqualDeep(from.state, nextState);
|
|
398
|
+
return {
|
|
399
|
+
pathname,
|
|
400
|
+
search,
|
|
401
|
+
searchStr,
|
|
402
|
+
state: nextState,
|
|
403
|
+
hash,
|
|
404
|
+
href: this.history.createHref(`${pathname}${searchStr}${hashStr}`),
|
|
405
|
+
unmaskOnReload: dest.unmaskOnReload
|
|
406
|
+
};
|
|
407
|
+
};
|
|
408
|
+
const buildWithMatches = (dest = {}, maskedDest) => {
|
|
409
|
+
let next = build(dest);
|
|
410
|
+
let maskedNext = maskedDest ? build(maskedDest) : undefined;
|
|
411
|
+
if (!maskedNext) {
|
|
412
|
+
let params = {};
|
|
413
|
+
let foundMask = this.options.routeMasks?.find(d => {
|
|
414
|
+
const match = path.matchPathname(this.basepath, next.pathname, {
|
|
415
|
+
to: d.from,
|
|
416
|
+
caseSensitive: false,
|
|
417
|
+
fuzzy: false
|
|
418
|
+
});
|
|
419
|
+
if (match) {
|
|
420
|
+
params = match;
|
|
421
|
+
return true;
|
|
422
|
+
}
|
|
423
|
+
return false;
|
|
424
|
+
});
|
|
425
|
+
if (foundMask) {
|
|
426
|
+
foundMask = {
|
|
427
|
+
...foundMask,
|
|
428
|
+
from: path.interpolatePath(foundMask.from, params)
|
|
429
|
+
};
|
|
430
|
+
maskedDest = foundMask;
|
|
431
|
+
maskedNext = build(maskedDest);
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
const nextMatches = this.matchRoutes(next.pathname, next.search);
|
|
435
|
+
const maskedMatches = maskedNext ? this.matchRoutes(maskedNext.pathname, maskedNext.search) : undefined;
|
|
436
|
+
const maskedFinal = maskedNext ? build(maskedDest, maskedMatches) : undefined;
|
|
437
|
+
const final = build(dest, nextMatches);
|
|
438
|
+
if (maskedFinal) {
|
|
439
|
+
final.maskedLocation = maskedFinal;
|
|
440
|
+
}
|
|
441
|
+
return final;
|
|
442
|
+
};
|
|
443
|
+
if (opts.mask) {
|
|
444
|
+
return buildWithMatches(opts, {
|
|
445
|
+
...utils.pick(opts, ['from']),
|
|
446
|
+
...opts.mask
|
|
447
|
+
});
|
|
448
|
+
}
|
|
449
|
+
return buildWithMatches(opts);
|
|
450
|
+
};
|
|
451
|
+
commitLocation = async ({
|
|
452
|
+
startTransition,
|
|
453
|
+
...next
|
|
454
|
+
}) => {
|
|
455
|
+
if (this.navigateTimeout) clearTimeout(this.navigateTimeout);
|
|
456
|
+
const isSameUrl = this.latestLocation.href === next.href;
|
|
457
|
+
|
|
458
|
+
// If the next urls are the same and we're not replacing,
|
|
459
|
+
// do nothing
|
|
460
|
+
if (!isSameUrl || !next.replace) {
|
|
461
|
+
let {
|
|
462
|
+
maskedLocation,
|
|
463
|
+
...nextHistory
|
|
464
|
+
} = next;
|
|
465
|
+
if (maskedLocation) {
|
|
466
|
+
nextHistory = {
|
|
467
|
+
...maskedLocation,
|
|
468
|
+
state: {
|
|
469
|
+
...maskedLocation.state,
|
|
470
|
+
__tempKey: undefined,
|
|
471
|
+
__tempLocation: {
|
|
472
|
+
...nextHistory,
|
|
473
|
+
search: nextHistory.searchStr,
|
|
474
|
+
state: {
|
|
475
|
+
...nextHistory.state,
|
|
476
|
+
__tempKey: undefined,
|
|
477
|
+
__tempLocation: undefined,
|
|
478
|
+
key: undefined
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
};
|
|
483
|
+
if (nextHistory.unmaskOnReload ?? this.options.unmaskOnReload ?? false) {
|
|
484
|
+
nextHistory.state.__tempKey = this.tempLocationKey;
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
const apply = () => {
|
|
488
|
+
this.history[next.replace ? 'replace' : 'push'](nextHistory.href, nextHistory.state);
|
|
489
|
+
};
|
|
490
|
+
if (startTransition ?? true) {
|
|
491
|
+
this.startReactTransition(apply);
|
|
492
|
+
} else {
|
|
493
|
+
apply();
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
this.resetNextScroll = next.resetScroll ?? true;
|
|
497
|
+
return this.latestLoadPromise;
|
|
498
|
+
};
|
|
499
|
+
buildAndCommitLocation = ({
|
|
500
|
+
replace,
|
|
501
|
+
resetScroll,
|
|
502
|
+
startTransition,
|
|
503
|
+
...rest
|
|
504
|
+
} = {}) => {
|
|
505
|
+
const location = this.buildLocation(rest);
|
|
506
|
+
return this.commitLocation({
|
|
507
|
+
...location,
|
|
508
|
+
startTransition,
|
|
509
|
+
replace,
|
|
510
|
+
resetScroll
|
|
511
|
+
});
|
|
512
|
+
};
|
|
513
|
+
navigate = ({
|
|
514
|
+
from,
|
|
515
|
+
to = '',
|
|
516
|
+
...rest
|
|
517
|
+
}) => {
|
|
518
|
+
// If this link simply reloads the current route,
|
|
519
|
+
// make sure it has a new key so it will trigger a data refresh
|
|
520
|
+
|
|
521
|
+
// If this `to` is a valid external URL, return
|
|
522
|
+
// null for LinkUtils
|
|
523
|
+
const toString = String(to);
|
|
524
|
+
const fromString = typeof from === 'undefined' ? from : String(from);
|
|
525
|
+
let isExternal;
|
|
526
|
+
try {
|
|
527
|
+
new URL(`${toString}`);
|
|
528
|
+
isExternal = true;
|
|
529
|
+
} catch (e) {}
|
|
530
|
+
invariant__default["default"](!isExternal, 'Attempting to navigate to external url with this.navigate!');
|
|
531
|
+
return this.buildAndCommitLocation({
|
|
532
|
+
...rest,
|
|
533
|
+
from: fromString,
|
|
534
|
+
to: toString
|
|
535
|
+
});
|
|
536
|
+
};
|
|
537
|
+
loadMatches = async ({
|
|
538
|
+
checkLatest,
|
|
539
|
+
matches,
|
|
540
|
+
preload
|
|
541
|
+
}) => {
|
|
542
|
+
let latestPromise;
|
|
543
|
+
let firstBadMatchIndex;
|
|
544
|
+
|
|
545
|
+
// Check each match middleware to see if the route can be accessed
|
|
546
|
+
try {
|
|
547
|
+
for (let [index, match] of matches.entries()) {
|
|
548
|
+
const parentMatch = matches[index - 1];
|
|
549
|
+
const route = this.looseRoutesById[match.routeId];
|
|
550
|
+
const handleError = (err, code) => {
|
|
551
|
+
err.routerCode = code;
|
|
552
|
+
firstBadMatchIndex = firstBadMatchIndex ?? index;
|
|
553
|
+
if (redirects.isRedirect(err)) {
|
|
554
|
+
throw err;
|
|
555
|
+
}
|
|
556
|
+
try {
|
|
557
|
+
route.options.onError?.(err);
|
|
558
|
+
} catch (errorHandlerErr) {
|
|
559
|
+
err = errorHandlerErr;
|
|
560
|
+
if (redirects.isRedirect(errorHandlerErr)) {
|
|
561
|
+
throw errorHandlerErr;
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
matches[index] = match = {
|
|
565
|
+
...match,
|
|
566
|
+
error: err,
|
|
567
|
+
status: 'error',
|
|
568
|
+
updatedAt: Date.now()
|
|
569
|
+
};
|
|
570
|
+
};
|
|
571
|
+
try {
|
|
572
|
+
if (match.paramsError) {
|
|
573
|
+
handleError(match.paramsError, 'PARSE_PARAMS');
|
|
574
|
+
}
|
|
575
|
+
if (match.searchError) {
|
|
576
|
+
handleError(match.searchError, 'VALIDATE_SEARCH');
|
|
577
|
+
}
|
|
578
|
+
const parentContext = parentMatch?.context ?? this.options.context ?? {};
|
|
579
|
+
const beforeLoadContext = (await route.options.beforeLoad?.({
|
|
580
|
+
search: match.search,
|
|
581
|
+
abortController: match.abortController,
|
|
582
|
+
params: match.params,
|
|
583
|
+
preload: !!preload,
|
|
584
|
+
context: parentContext,
|
|
585
|
+
location: this.state.location,
|
|
586
|
+
// TOOD: just expose state and router, etc
|
|
587
|
+
navigate: opts => this.navigate({
|
|
588
|
+
...opts,
|
|
589
|
+
from: match.pathname
|
|
590
|
+
}),
|
|
591
|
+
buildLocation: this.buildLocation,
|
|
592
|
+
cause: match.cause
|
|
593
|
+
})) ?? {};
|
|
594
|
+
const context = {
|
|
595
|
+
...parentContext,
|
|
596
|
+
...beforeLoadContext
|
|
597
|
+
};
|
|
598
|
+
matches[index] = match = {
|
|
599
|
+
...match,
|
|
600
|
+
context: utils.replaceEqualDeep(match.context, context)
|
|
601
|
+
};
|
|
602
|
+
} catch (err) {
|
|
603
|
+
handleError(err, 'BEFORE_LOAD');
|
|
604
|
+
break;
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
} catch (err) {
|
|
608
|
+
if (redirects.isRedirect(err)) {
|
|
609
|
+
if (!preload) this.navigate(err);
|
|
610
|
+
return matches;
|
|
611
|
+
}
|
|
612
|
+
throw err;
|
|
613
|
+
}
|
|
614
|
+
const validResolvedMatches = matches.slice(0, firstBadMatchIndex);
|
|
615
|
+
const matchPromises = [];
|
|
616
|
+
validResolvedMatches.forEach((match, index) => {
|
|
617
|
+
matchPromises.push((async () => {
|
|
618
|
+
const parentMatchPromise = matchPromises[index - 1];
|
|
619
|
+
const route = this.looseRoutesById[match.routeId];
|
|
620
|
+
const handleIfRedirect = err => {
|
|
621
|
+
if (redirects.isRedirect(err)) {
|
|
622
|
+
if (!preload) {
|
|
623
|
+
this.navigate(err);
|
|
624
|
+
}
|
|
625
|
+
return true;
|
|
626
|
+
}
|
|
627
|
+
return false;
|
|
628
|
+
};
|
|
629
|
+
let loadPromise;
|
|
630
|
+
matches[index] = match = {
|
|
631
|
+
...match,
|
|
632
|
+
fetchedAt: Date.now(),
|
|
633
|
+
invalid: false
|
|
634
|
+
};
|
|
635
|
+
if (match.isFetching) {
|
|
636
|
+
loadPromise = RouterProvider.getRouteMatch(this.state, match.id)?.loadPromise;
|
|
637
|
+
} else {
|
|
638
|
+
const loaderContext = {
|
|
639
|
+
params: match.params,
|
|
640
|
+
search: match.search,
|
|
641
|
+
preload: !!preload,
|
|
642
|
+
parentMatchPromise,
|
|
643
|
+
abortController: match.abortController,
|
|
644
|
+
context: match.context,
|
|
645
|
+
location: this.state.location,
|
|
646
|
+
navigate: opts => this.navigate({
|
|
647
|
+
...opts,
|
|
648
|
+
from: match.pathname
|
|
649
|
+
}),
|
|
650
|
+
cause: match.cause
|
|
651
|
+
};
|
|
652
|
+
|
|
653
|
+
// Default to reloading the route all the time
|
|
654
|
+
let shouldReload = true;
|
|
655
|
+
let shouldReloadDeps = typeof route.options.shouldReload === 'function' ? route.options.shouldReload?.(loaderContext) : !!(route.options.shouldReload ?? true);
|
|
656
|
+
if (match.cause === 'enter') {
|
|
657
|
+
match.shouldReloadDeps = shouldReloadDeps;
|
|
658
|
+
} else if (match.cause === 'stay') {
|
|
659
|
+
if (typeof shouldReloadDeps === 'object') {
|
|
660
|
+
// compare the deps to see if they've changed
|
|
661
|
+
shouldReload = !utils.deepEqual(shouldReloadDeps, match.shouldReloadDeps);
|
|
662
|
+
match.shouldReloadDeps = shouldReloadDeps;
|
|
663
|
+
} else {
|
|
664
|
+
shouldReload = !!shouldReloadDeps;
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
// If the user doesn't want the route to reload, just
|
|
669
|
+
// resolve with the existing loader data
|
|
670
|
+
|
|
671
|
+
if (!shouldReload) {
|
|
672
|
+
loadPromise = Promise.resolve(match.loaderData);
|
|
673
|
+
} else {
|
|
674
|
+
// Otherwise, load the route
|
|
675
|
+
matches[index] = match = {
|
|
676
|
+
...match,
|
|
677
|
+
isFetching: true
|
|
678
|
+
};
|
|
679
|
+
const componentsPromise = Promise.all(componentTypes.map(async type => {
|
|
680
|
+
const component = route.options[type];
|
|
681
|
+
if (component?.preload) {
|
|
682
|
+
await component.preload();
|
|
683
|
+
}
|
|
684
|
+
}));
|
|
685
|
+
const loaderPromise = route.options.loader?.(loaderContext);
|
|
686
|
+
loadPromise = Promise.all([componentsPromise, loaderPromise]).then(d => d[1]);
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
matches[index] = match = {
|
|
690
|
+
...match,
|
|
691
|
+
loadPromise
|
|
692
|
+
};
|
|
693
|
+
if (!preload) {
|
|
694
|
+
this.setState(s => ({
|
|
695
|
+
...s,
|
|
696
|
+
matches: s.matches.map(d => d.id === match.id ? match : d)
|
|
697
|
+
}));
|
|
698
|
+
}
|
|
699
|
+
try {
|
|
700
|
+
const loaderData = await loadPromise;
|
|
701
|
+
if (latestPromise = checkLatest()) return await latestPromise;
|
|
702
|
+
matches[index] = match = {
|
|
703
|
+
...match,
|
|
704
|
+
error: undefined,
|
|
705
|
+
status: 'success',
|
|
706
|
+
isFetching: false,
|
|
707
|
+
updatedAt: Date.now(),
|
|
708
|
+
loaderData,
|
|
709
|
+
loadPromise: undefined
|
|
710
|
+
};
|
|
711
|
+
} catch (error) {
|
|
712
|
+
if (latestPromise = checkLatest()) return await latestPromise;
|
|
713
|
+
if (handleIfRedirect(error)) return;
|
|
714
|
+
try {
|
|
715
|
+
route.options.onError?.(error);
|
|
716
|
+
} catch (onErrorError) {
|
|
717
|
+
error = onErrorError;
|
|
718
|
+
if (handleIfRedirect(onErrorError)) return;
|
|
719
|
+
}
|
|
720
|
+
matches[index] = match = {
|
|
721
|
+
...match,
|
|
722
|
+
error,
|
|
723
|
+
status: 'error',
|
|
724
|
+
isFetching: false,
|
|
725
|
+
updatedAt: Date.now()
|
|
726
|
+
};
|
|
727
|
+
}
|
|
728
|
+
if (!preload) {
|
|
729
|
+
this.setState(s => ({
|
|
730
|
+
...s,
|
|
731
|
+
matches: s.matches.map(d => d.id === match.id ? match : d)
|
|
732
|
+
}));
|
|
733
|
+
}
|
|
734
|
+
})());
|
|
735
|
+
});
|
|
736
|
+
await Promise.all(matchPromises);
|
|
737
|
+
return matches;
|
|
738
|
+
};
|
|
739
|
+
load = async () => {
|
|
740
|
+
const promise = new Promise(async (resolve, reject) => {
|
|
741
|
+
const next = this.latestLocation;
|
|
742
|
+
const prevLocation = this.state.resolvedLocation;
|
|
743
|
+
const pathDidChange = prevLocation.href !== next.href;
|
|
744
|
+
let latestPromise;
|
|
745
|
+
|
|
746
|
+
// Cancel any pending matches
|
|
747
|
+
this.cancelMatches();
|
|
748
|
+
this.emit({
|
|
749
|
+
type: 'onBeforeLoad',
|
|
750
|
+
fromLocation: prevLocation,
|
|
751
|
+
toLocation: next,
|
|
752
|
+
pathChanged: pathDidChange
|
|
753
|
+
});
|
|
754
|
+
|
|
755
|
+
// Match the routes
|
|
756
|
+
let matches = this.matchRoutes(next.pathname, next.search, {
|
|
757
|
+
debug: true
|
|
758
|
+
});
|
|
759
|
+
this.pendingMatches = matches;
|
|
760
|
+
const previousMatches = this.state.matches;
|
|
761
|
+
|
|
762
|
+
// Ingest the new matches
|
|
763
|
+
this.setState(s => ({
|
|
764
|
+
...s,
|
|
765
|
+
status: 'pending',
|
|
766
|
+
location: next,
|
|
767
|
+
matches
|
|
768
|
+
}));
|
|
769
|
+
try {
|
|
770
|
+
try {
|
|
771
|
+
// Load the matches
|
|
772
|
+
await this.loadMatches({
|
|
773
|
+
matches,
|
|
774
|
+
checkLatest: () => this.checkLatest(promise)
|
|
775
|
+
});
|
|
776
|
+
} catch (err) {
|
|
777
|
+
// swallow this error, since we'll display the
|
|
778
|
+
// errors on the route components
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
// Only apply the latest transition
|
|
782
|
+
if (latestPromise = this.checkLatest(promise)) {
|
|
783
|
+
return latestPromise;
|
|
784
|
+
}
|
|
785
|
+
const exitingMatchIds = previousMatches.filter(id => !this.pendingMatches.includes(id));
|
|
786
|
+
const enteringMatchIds = this.pendingMatches.filter(id => !previousMatches.includes(id));
|
|
787
|
+
const stayingMatchIds = previousMatches.filter(id => this.pendingMatches.includes(id))
|
|
788
|
+
|
|
789
|
+
// setState((s) => ({
|
|
790
|
+
// ...s,
|
|
791
|
+
// status: 'idle',
|
|
792
|
+
// resolvedLocation: s.location,
|
|
793
|
+
// }))
|
|
794
|
+
|
|
795
|
+
//
|
|
796
|
+
;
|
|
797
|
+
[[exitingMatchIds, 'onLeave'], [enteringMatchIds, 'onEnter'], [stayingMatchIds, 'onTransition']].forEach(([matches, hook]) => {
|
|
798
|
+
matches.forEach(match => {
|
|
799
|
+
this.looseRoutesById[match.routeId].options[hook]?.(match);
|
|
800
|
+
});
|
|
801
|
+
});
|
|
802
|
+
this.emit({
|
|
803
|
+
type: 'onLoad',
|
|
804
|
+
fromLocation: prevLocation,
|
|
805
|
+
toLocation: next,
|
|
806
|
+
pathChanged: pathDidChange
|
|
807
|
+
});
|
|
808
|
+
resolve();
|
|
809
|
+
} catch (err) {
|
|
810
|
+
// Only apply the latest transition
|
|
811
|
+
if (latestPromise = this.checkLatest(promise)) {
|
|
812
|
+
return latestPromise;
|
|
813
|
+
}
|
|
814
|
+
reject(err);
|
|
815
|
+
}
|
|
816
|
+
});
|
|
817
|
+
this.latestLoadPromise = promise;
|
|
818
|
+
return this.latestLoadPromise;
|
|
819
|
+
};
|
|
820
|
+
preloadRoute = async (navigateOpts = this.state.location) => {
|
|
821
|
+
let next = this.buildLocation(navigateOpts);
|
|
822
|
+
let matches = this.matchRoutes(next.pathname, next.search, {
|
|
823
|
+
throwOnError: true
|
|
824
|
+
});
|
|
825
|
+
await this.loadMatches({
|
|
826
|
+
matches,
|
|
827
|
+
preload: true,
|
|
828
|
+
checkLatest: () => undefined
|
|
829
|
+
});
|
|
830
|
+
return [utils.last(matches), matches];
|
|
831
|
+
};
|
|
832
|
+
buildLink = dest => {
|
|
833
|
+
// If this link simply reloads the current route,
|
|
834
|
+
// make sure it has a new key so it will trigger a data refresh
|
|
835
|
+
|
|
836
|
+
// If this `to` is a valid external URL, return
|
|
837
|
+
// null for LinkUtils
|
|
838
|
+
|
|
839
|
+
const {
|
|
840
|
+
to,
|
|
841
|
+
preload: userPreload,
|
|
842
|
+
preloadDelay: userPreloadDelay,
|
|
843
|
+
activeOptions,
|
|
844
|
+
disabled,
|
|
845
|
+
target,
|
|
846
|
+
replace,
|
|
847
|
+
resetScroll,
|
|
848
|
+
startTransition
|
|
849
|
+
} = dest;
|
|
850
|
+
try {
|
|
851
|
+
new URL(`${to}`);
|
|
852
|
+
return {
|
|
853
|
+
type: 'external',
|
|
854
|
+
href: to
|
|
855
|
+
};
|
|
856
|
+
} catch (e) {}
|
|
857
|
+
const nextOpts = dest;
|
|
858
|
+
const next = this.buildLocation(nextOpts);
|
|
859
|
+
const preload = userPreload ?? this.options.defaultPreload;
|
|
860
|
+
const preloadDelay = userPreloadDelay ?? this.options.defaultPreloadDelay ?? 0;
|
|
861
|
+
|
|
862
|
+
// Compare path/hash for matches
|
|
863
|
+
const currentPathSplit = this.latestLocation.pathname.split('/');
|
|
864
|
+
const nextPathSplit = next.pathname.split('/');
|
|
865
|
+
const pathIsFuzzyEqual = nextPathSplit.every((d, i) => d === currentPathSplit[i]);
|
|
866
|
+
// Combine the matches based on user this.options
|
|
867
|
+
const pathTest = activeOptions?.exact ? this.latestLocation.pathname === next.pathname : pathIsFuzzyEqual;
|
|
868
|
+
const hashTest = activeOptions?.includeHash ? this.latestLocation.hash === next.hash : true;
|
|
869
|
+
const searchTest = activeOptions?.includeSearch ?? true ? utils.deepEqual(this.latestLocation.search, next.search, true) : true;
|
|
870
|
+
|
|
871
|
+
// The final "active" test
|
|
872
|
+
const isActive = pathTest && hashTest && searchTest;
|
|
873
|
+
|
|
874
|
+
// The click handler
|
|
875
|
+
const handleClick = e => {
|
|
876
|
+
if (!disabled && !isCtrlEvent(e) && !e.defaultPrevented && (!target || target === '_self') && e.button === 0) {
|
|
877
|
+
e.preventDefault();
|
|
878
|
+
|
|
879
|
+
// All is well? Navigate!
|
|
880
|
+
this.commitLocation({
|
|
881
|
+
...next,
|
|
882
|
+
replace,
|
|
883
|
+
resetScroll,
|
|
884
|
+
startTransition
|
|
885
|
+
});
|
|
886
|
+
}
|
|
887
|
+
};
|
|
888
|
+
|
|
889
|
+
// The click handler
|
|
890
|
+
const handleFocus = e => {
|
|
891
|
+
if (preload) {
|
|
892
|
+
this.preloadRoute(nextOpts).catch(err => {
|
|
893
|
+
console.warn(err);
|
|
894
|
+
console.warn(preloadWarning);
|
|
895
|
+
});
|
|
896
|
+
}
|
|
897
|
+
};
|
|
898
|
+
const handleTouchStart = e => {
|
|
899
|
+
this.preloadRoute(nextOpts).catch(err => {
|
|
900
|
+
console.warn(err);
|
|
901
|
+
console.warn(preloadWarning);
|
|
902
|
+
});
|
|
903
|
+
};
|
|
904
|
+
const handleEnter = e => {
|
|
905
|
+
const target = e.target || {};
|
|
906
|
+
if (preload) {
|
|
907
|
+
if (target.preloadTimeout) {
|
|
908
|
+
return;
|
|
909
|
+
}
|
|
910
|
+
target.preloadTimeout = setTimeout(() => {
|
|
911
|
+
target.preloadTimeout = null;
|
|
912
|
+
this.preloadRoute(nextOpts).catch(err => {
|
|
913
|
+
console.warn(err);
|
|
914
|
+
console.warn(preloadWarning);
|
|
915
|
+
});
|
|
916
|
+
}, preloadDelay);
|
|
917
|
+
}
|
|
918
|
+
};
|
|
919
|
+
const handleLeave = e => {
|
|
920
|
+
const target = e.target || {};
|
|
921
|
+
if (target.preloadTimeout) {
|
|
922
|
+
clearTimeout(target.preloadTimeout);
|
|
923
|
+
target.preloadTimeout = null;
|
|
924
|
+
}
|
|
925
|
+
};
|
|
926
|
+
return {
|
|
927
|
+
type: 'internal',
|
|
928
|
+
next,
|
|
929
|
+
handleFocus,
|
|
930
|
+
handleClick,
|
|
931
|
+
handleEnter,
|
|
932
|
+
handleLeave,
|
|
933
|
+
handleTouchStart,
|
|
934
|
+
isActive,
|
|
935
|
+
disabled
|
|
936
|
+
};
|
|
937
|
+
};
|
|
938
|
+
matchRoute = (location, opts) => {
|
|
939
|
+
location = {
|
|
940
|
+
...location,
|
|
941
|
+
to: location.to ? this.resolvePathWithBase(location.from || '', location.to) : undefined
|
|
942
|
+
};
|
|
943
|
+
const next = this.buildLocation(location);
|
|
944
|
+
if (opts?.pending && this.state.status !== 'pending') {
|
|
945
|
+
return false;
|
|
946
|
+
}
|
|
947
|
+
const baseLocation = opts?.pending ? this.latestLocation : this.state.resolvedLocation;
|
|
948
|
+
|
|
949
|
+
// const baseLocation = state.resolvedLocation
|
|
950
|
+
|
|
951
|
+
if (!baseLocation) {
|
|
952
|
+
return false;
|
|
953
|
+
}
|
|
954
|
+
const match = path.matchPathname(this.basepath, baseLocation.pathname, {
|
|
955
|
+
...opts,
|
|
956
|
+
to: next.pathname
|
|
957
|
+
});
|
|
958
|
+
if (!match) {
|
|
959
|
+
return false;
|
|
960
|
+
}
|
|
961
|
+
if (match && (opts?.includeSearch ?? true)) {
|
|
962
|
+
return utils.deepEqual(baseLocation.search, next.search, true) ? match : false;
|
|
963
|
+
}
|
|
964
|
+
return match;
|
|
965
|
+
};
|
|
966
|
+
injectHtml = async html => {
|
|
967
|
+
this.injectedHtml.push(html);
|
|
968
|
+
};
|
|
969
|
+
dehydrateData = (key, getData) => {
|
|
970
|
+
if (typeof document === 'undefined') {
|
|
971
|
+
const strKey = typeof key === 'string' ? key : JSON.stringify(key);
|
|
972
|
+
this.injectHtml(async () => {
|
|
973
|
+
const id = `__TSR_DEHYDRATED__${strKey}`;
|
|
974
|
+
const data = typeof getData === 'function' ? await getData() : getData;
|
|
975
|
+
return `<script id='${id}' suppressHydrationWarning>window["__TSR_DEHYDRATED__${utils.escapeJSON(strKey)}"] = ${JSON.stringify(data)}
|
|
976
|
+
;(() => {
|
|
977
|
+
var el = document.getElementById('${id}')
|
|
978
|
+
el.parentElement.removeChild(el)
|
|
979
|
+
})()
|
|
980
|
+
</script>`;
|
|
981
|
+
});
|
|
982
|
+
return () => this.hydrateData(key);
|
|
983
|
+
}
|
|
984
|
+
return () => undefined;
|
|
985
|
+
};
|
|
986
|
+
hydrateData = key => {
|
|
987
|
+
if (typeof document !== 'undefined') {
|
|
988
|
+
const strKey = typeof key === 'string' ? key : JSON.stringify(key);
|
|
989
|
+
return window[`__TSR_DEHYDRATED__${strKey}`];
|
|
990
|
+
}
|
|
991
|
+
return undefined;
|
|
992
|
+
};
|
|
54
993
|
|
|
55
994
|
// dehydrate = (): DehydratedRouter => {
|
|
56
995
|
// return {
|
|
57
996
|
// state: {
|
|
58
|
-
// dehydratedMatches: state.matches.map((d) =>
|
|
997
|
+
// dehydratedMatches: this.state.matches.map((d) =>
|
|
59
998
|
// pick(d, ['fetchedAt', 'invalid', 'id', 'status', 'updatedAt']),
|
|
60
999
|
// ),
|
|
61
1000
|
// },
|
|
@@ -80,8 +1019,8 @@ class Router {
|
|
|
80
1019
|
// const dehydratedState = ctx.router.state
|
|
81
1020
|
|
|
82
1021
|
// let matches = this.matchRoutes(
|
|
83
|
-
// state.location.pathname,
|
|
84
|
-
// state.location.search,
|
|
1022
|
+
// this.state.location.pathname,
|
|
1023
|
+
// this.state.location.search,
|
|
85
1024
|
// ).map((match) => {
|
|
86
1025
|
// const dehydratedMatch = dehydratedState.dehydratedMatches.find(
|
|
87
1026
|
// (d) => d.id === match.id,
|
|
@@ -114,34 +1053,6 @@ class Router {
|
|
|
114
1053
|
// .find((d) => d.id === matchId)
|
|
115
1054
|
// ?.__promisesByKey[key]?.resolve(value)
|
|
116
1055
|
// }
|
|
117
|
-
|
|
118
|
-
// setRouteMatch = (
|
|
119
|
-
// id: string,
|
|
120
|
-
// pending: boolean,
|
|
121
|
-
// updater: NonNullableUpdater<RouteMatch<TRouteTree>>,
|
|
122
|
-
// ) => {
|
|
123
|
-
// const key = pending ? 'pendingMatches' : 'matches'
|
|
124
|
-
|
|
125
|
-
// this.setState((prev) => {
|
|
126
|
-
// return {
|
|
127
|
-
// ...prev,
|
|
128
|
-
// [key]: prev[key].map((d) => {
|
|
129
|
-
// if (d.id === id) {
|
|
130
|
-
// return functionalUpdate(updater, d)
|
|
131
|
-
// }
|
|
132
|
-
|
|
133
|
-
// return d
|
|
134
|
-
// }),
|
|
135
|
-
// }
|
|
136
|
-
// })
|
|
137
|
-
// }
|
|
138
|
-
|
|
139
|
-
// setPendingRouteMatch = (
|
|
140
|
-
// id: string,
|
|
141
|
-
// updater: NonNullableUpdater<RouteMatch<TRouteTree>>,
|
|
142
|
-
// ) => {
|
|
143
|
-
// this.setRouteMatch(id, true, updater)
|
|
144
|
-
// }
|
|
145
1056
|
}
|
|
146
1057
|
|
|
147
1058
|
// A function that takes an import() argument which is a function and returns a new function that will
|
|
@@ -153,6 +1064,9 @@ function lazyFn(fn, key) {
|
|
|
153
1064
|
return imported[key || 'default'](...args);
|
|
154
1065
|
};
|
|
155
1066
|
}
|
|
1067
|
+
function isCtrlEvent(e) {
|
|
1068
|
+
return !!(e.metaKey || e.altKey || e.ctrlKey || e.shiftKey);
|
|
1069
|
+
}
|
|
156
1070
|
|
|
157
1071
|
exports.Router = Router;
|
|
158
1072
|
exports.componentTypes = componentTypes;
|