@tanstack/solid-router 1.114.23 → 1.114.25

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.
@@ -1,1814 +1,10 @@
1
- import { createBrowserHistory, createMemoryHistory, parseHref, } from '@tanstack/history';
2
- import { Store, batch } from '@tanstack/solid-store';
3
- import invariant from 'tiny-invariant';
4
- import { cleanPath, createControlledPromise, deepEqual, defaultParseSearch, defaultStringifySearch, functionalUpdate, getLocationChangeInfo, interpolatePath, isNotFound, isRedirect, isResolvedRedirect, joinPaths, last, matchPathname, parsePathname, pick, replaceEqualDeep, resolvePath, rootRouteId, setupScrollRestoration, trimPath, trimPathLeft, trimPathRight, } from '@tanstack/router-core';
5
- export const componentTypes = [
6
- 'component',
7
- 'errorComponent',
8
- 'pendingComponent',
9
- 'notFoundComponent',
10
- ];
11
- function routeNeedsPreload(route) {
12
- for (const componentType of componentTypes) {
13
- if (route.options[componentType]?.preload) {
14
- return true;
15
- }
16
- }
17
- return false;
18
- }
19
- function validateSearch(validateSearch, input) {
20
- if (validateSearch == null)
21
- return {};
22
- if ('~standard' in validateSearch) {
23
- const result = validateSearch['~standard'].validate(input);
24
- if (result instanceof Promise)
25
- throw new SearchParamError('Async validation not supported');
26
- if (result.issues)
27
- throw new SearchParamError(JSON.stringify(result.issues, undefined, 2), {
28
- cause: result,
29
- });
30
- return result.value;
31
- }
32
- if ('parse' in validateSearch) {
33
- return validateSearch.parse(input);
34
- }
35
- if (typeof validateSearch === 'function') {
36
- return validateSearch(input);
37
- }
38
- return {};
39
- }
40
- export function createRouter(options) {
1
+ import { RouterCore } from '@tanstack/router-core';
2
+ export const createRouter = (options) => {
41
3
  return new Router(options);
42
- }
43
- export class Router {
44
- /**
45
- * @deprecated Use the `createRouter` function instead
46
- */
4
+ };
5
+ export class Router extends RouterCore {
47
6
  constructor(options) {
48
- // Option-independent properties
49
- this.tempLocationKey = `${Math.round(Math.random() * 10000000)}`;
50
- this.resetNextScroll = true;
51
- this.shouldViewTransition = undefined;
52
- this.isViewTransitionTypesSupported = undefined;
53
- this.subscribers = new Set();
54
- this.isScrollRestoring = false;
55
- this.isScrollRestorationSetup = false;
56
- // These are default implementations that can optionally be overridden
57
- // by the router provider once rendered. We provide these so that the
58
- // router can be used in a non-react environment if necessary
59
- this.startTransition = (fn) => fn();
60
- this.update = (newOptions) => {
61
- if (newOptions.notFoundRoute) {
62
- console.warn('The notFoundRoute API is deprecated and will be removed in the next major version. See https://tanstack.com/router/v1/docs/guide/not-found-errors#migrating-from-notfoundroute for more info.');
63
- }
64
- const previousOptions = this.options;
65
- this.options = {
66
- ...this.options,
67
- ...newOptions,
68
- };
69
- this.isServer = this.options.isServer ?? typeof document === 'undefined';
70
- this.pathParamsDecodeCharMap = this.options.pathParamsAllowedCharacters
71
- ? new Map(this.options.pathParamsAllowedCharacters.map((char) => [
72
- encodeURIComponent(char),
73
- char,
74
- ]))
75
- : undefined;
76
- if (!this.basepath ||
77
- (newOptions.basepath && newOptions.basepath !== previousOptions.basepath)) {
78
- if (newOptions.basepath === undefined ||
79
- newOptions.basepath === '' ||
80
- newOptions.basepath === '/') {
81
- this.basepath = '/';
82
- }
83
- else {
84
- this.basepath = `/${trimPath(newOptions.basepath)}`;
85
- }
86
- }
87
- if (!this.history ||
88
- (this.options.history && this.options.history !== this.history)) {
89
- this.history =
90
- this.options.history ??
91
- (this.isServer
92
- ? createMemoryHistory({
93
- initialEntries: [this.basepath || '/'],
94
- })
95
- : createBrowserHistory());
96
- this.latestLocation = this.parseLocation();
97
- }
98
- if (this.options.routeTree !== this.routeTree) {
99
- this.routeTree = this.options.routeTree;
100
- this.buildRouteTree();
101
- }
102
- if (!this.__store) {
103
- this.__store = new Store(getInitialRouterState(this.latestLocation), {
104
- onUpdate: () => {
105
- this.__store.state = {
106
- ...this.state,
107
- cachedMatches: this.state.cachedMatches.filter((d) => !['redirected'].includes(d.status)),
108
- };
109
- },
110
- });
111
- setupScrollRestoration(this);
112
- }
113
- if (typeof window !== 'undefined' &&
114
- 'CSS' in window &&
115
- typeof window.CSS?.supports === 'function') {
116
- this.isViewTransitionTypesSupported = window.CSS.supports('selector(:active-view-transition-type(a)');
117
- }
118
- };
119
- this.buildRouteTree = () => {
120
- this.routesById = {};
121
- this.routesByPath = {};
122
- const notFoundRoute = this.options.notFoundRoute;
123
- if (notFoundRoute) {
124
- notFoundRoute.init({
125
- originalIndex: 99999999999,
126
- defaultSsr: this.options.defaultSsr,
127
- });
128
- this.routesById[notFoundRoute.id] = notFoundRoute;
129
- }
130
- const recurseRoutes = (childRoutes) => {
131
- childRoutes.forEach((childRoute, i) => {
132
- childRoute.init({
133
- originalIndex: i,
134
- defaultSsr: this.options.defaultSsr,
135
- });
136
- const existingRoute = this.routesById[childRoute.id];
137
- invariant(!existingRoute, `Duplicate routes found with id: ${String(childRoute.id)}`);
138
- this.routesById[childRoute.id] = childRoute;
139
- if (!childRoute.isRoot && childRoute.path) {
140
- const trimmedFullPath = trimPathRight(childRoute.fullPath);
141
- if (!this.routesByPath[trimmedFullPath] ||
142
- childRoute.fullPath.endsWith('/')) {
143
- ;
144
- this.routesByPath[trimmedFullPath] = childRoute;
145
- }
146
- }
147
- const children = childRoute.children;
148
- if (children?.length) {
149
- recurseRoutes(children);
150
- }
151
- });
152
- };
153
- recurseRoutes([this.routeTree]);
154
- const scoredRoutes = [];
155
- const routes = Object.values(this.routesById);
156
- routes.forEach((d, i) => {
157
- if (d.isRoot || !d.path) {
158
- return;
159
- }
160
- const trimmed = trimPathLeft(d.fullPath);
161
- const parsed = parsePathname(trimmed);
162
- while (parsed.length > 1 && parsed[0]?.value === '/') {
163
- parsed.shift();
164
- }
165
- const scores = parsed.map((segment) => {
166
- if (segment.value === '/') {
167
- return 0.75;
168
- }
169
- if (segment.type === 'param') {
170
- return 0.5;
171
- }
172
- if (segment.type === 'wildcard') {
173
- return 0.25;
174
- }
175
- return 1;
176
- });
177
- scoredRoutes.push({ child: d, trimmed, parsed, index: i, scores });
178
- });
179
- this.flatRoutes = scoredRoutes
180
- .sort((a, b) => {
181
- const minLength = Math.min(a.scores.length, b.scores.length);
182
- // Sort by min available score
183
- for (let i = 0; i < minLength; i++) {
184
- if (a.scores[i] !== b.scores[i]) {
185
- return b.scores[i] - a.scores[i];
186
- }
187
- }
188
- // Sort by length of score
189
- if (a.scores.length !== b.scores.length) {
190
- return b.scores.length - a.scores.length;
191
- }
192
- // Sort by min available parsed value
193
- for (let i = 0; i < minLength; i++) {
194
- if (a.parsed[i].value !== b.parsed[i].value) {
195
- return a.parsed[i].value > b.parsed[i].value ? 1 : -1;
196
- }
197
- }
198
- // Sort by original index
199
- return a.index - b.index;
200
- })
201
- .map((d, i) => {
202
- d.child.rank = i;
203
- return d.child;
204
- });
205
- };
206
- this.subscribe = (eventType, fn) => {
207
- const listener = {
208
- eventType,
209
- fn,
210
- };
211
- this.subscribers.add(listener);
212
- return () => {
213
- this.subscribers.delete(listener);
214
- };
215
- };
216
- this.emit = (routerEvent) => {
217
- this.subscribers.forEach((listener) => {
218
- if (listener.eventType === routerEvent.type) {
219
- listener.fn(routerEvent);
220
- }
221
- });
222
- };
223
- this.parseLocation = (previousLocation, locationToParse) => {
224
- const parse = ({ pathname, search, hash, state, }) => {
225
- const parsedSearch = this.options.parseSearch(search);
226
- const searchStr = this.options.stringifySearch(parsedSearch);
227
- return {
228
- pathname,
229
- searchStr,
230
- search: replaceEqualDeep(previousLocation?.search, parsedSearch),
231
- hash: hash.split('#').reverse()[0] ?? '',
232
- href: `${pathname}${searchStr}${hash}`,
233
- state: replaceEqualDeep(previousLocation?.state, state),
234
- };
235
- };
236
- const location = parse(locationToParse ?? this.history.location);
237
- const { __tempLocation, __tempKey } = location.state;
238
- if (__tempLocation && (!__tempKey || __tempKey === this.tempLocationKey)) {
239
- // Sync up the location keys
240
- const parsedTempLocation = parse(__tempLocation);
241
- parsedTempLocation.state.key = location.state.key;
242
- delete parsedTempLocation.state.__tempLocation;
243
- return {
244
- ...parsedTempLocation,
245
- maskedLocation: location,
246
- };
247
- }
248
- return location;
249
- };
250
- this.resolvePathWithBase = (from, path) => {
251
- const resolvedPath = resolvePath({
252
- basepath: this.basepath,
253
- base: from,
254
- to: cleanPath(path),
255
- trailingSlash: this.options.trailingSlash,
256
- caseSensitive: this.options.caseSensitive,
257
- });
258
- return resolvedPath;
259
- };
260
- /**
261
- @deprecated use the following signature instead
262
- ```ts
263
- matchRoutes (
264
- next: ParsedLocation,
265
- opts?: { preload?: boolean; throwOnError?: boolean },
266
- ): Array<AnyRouteMatch>;
267
- ```
268
- */
269
- this.matchRoutes = (pathnameOrNext, locationSearchOrOpts, opts) => {
270
- if (typeof pathnameOrNext === 'string') {
271
- return this.matchRoutesInternal({
272
- pathname: pathnameOrNext,
273
- search: locationSearchOrOpts,
274
- }, opts);
275
- }
276
- else {
277
- return this.matchRoutesInternal(pathnameOrNext, locationSearchOrOpts);
278
- }
279
- };
280
- this.getMatchedRoutes = (next, dest) => {
281
- let routeParams = {};
282
- const trimmedPath = trimPathRight(next.pathname);
283
- const getMatchedParams = (route) => {
284
- const result = matchPathname(this.basepath, trimmedPath, {
285
- to: route.fullPath,
286
- caseSensitive: route.options.caseSensitive ?? this.options.caseSensitive,
287
- fuzzy: true,
288
- });
289
- return result;
290
- };
291
- let foundRoute = dest?.to !== undefined ? this.routesByPath[dest.to] : undefined;
292
- if (foundRoute) {
293
- routeParams = getMatchedParams(foundRoute);
294
- }
295
- else {
296
- foundRoute = this.flatRoutes.find((route) => {
297
- const matchedParams = getMatchedParams(route);
298
- if (matchedParams) {
299
- routeParams = matchedParams;
300
- return true;
301
- }
302
- return false;
303
- });
304
- }
305
- let routeCursor = foundRoute || this.routesById[rootRouteId];
306
- const matchedRoutes = [routeCursor];
307
- while (routeCursor.parentRoute) {
308
- routeCursor = routeCursor.parentRoute;
309
- matchedRoutes.unshift(routeCursor);
310
- }
311
- return { matchedRoutes, routeParams, foundRoute };
312
- };
313
- this.cancelMatch = (id) => {
314
- const match = this.getMatch(id);
315
- if (!match)
316
- return;
317
- match.abortController.abort();
318
- clearTimeout(match.pendingTimeout);
319
- };
320
- this.cancelMatches = () => {
321
- this.state.pendingMatches?.forEach((match) => {
322
- this.cancelMatch(match.id);
323
- });
324
- };
325
- this.buildLocation = (opts) => {
326
- const build = (dest = {}, matchedRoutesResult) => {
327
- const fromMatches = dest._fromLocation
328
- ? this.matchRoutes(dest._fromLocation, { _buildLocation: true })
329
- : this.state.matches;
330
- const fromMatch = dest.from != null
331
- ? fromMatches.find((d) => matchPathname(this.basepath, trimPathRight(d.pathname), {
332
- to: dest.from,
333
- caseSensitive: false,
334
- fuzzy: false,
335
- }))
336
- : undefined;
337
- const fromPath = fromMatch?.pathname || this.latestLocation.pathname;
338
- invariant(dest.from == null || fromMatch != null, 'Could not find match for from: ' + dest.from);
339
- const fromSearch = this.state.pendingMatches?.length
340
- ? last(this.state.pendingMatches)?.search
341
- : last(fromMatches)?.search || this.latestLocation.search;
342
- const stayingMatches = matchedRoutesResult?.matchedRoutes.filter((d) => fromMatches.find((e) => e.routeId === d.id));
343
- let pathname;
344
- if (dest.to) {
345
- const resolvePathTo = fromMatch?.fullPath ||
346
- last(fromMatches)?.fullPath ||
347
- this.latestLocation.pathname;
348
- pathname = this.resolvePathWithBase(resolvePathTo, `${dest.to}`);
349
- }
350
- else {
351
- const fromRouteByFromPathRouteId = this.routesById[stayingMatches?.find((route) => {
352
- const interpolatedPath = interpolatePath({
353
- path: route.fullPath,
354
- params: matchedRoutesResult?.routeParams ?? {},
355
- decodeCharMap: this.pathParamsDecodeCharMap,
356
- }).interpolatedPath;
357
- const pathname = joinPaths([this.basepath, interpolatedPath]);
358
- return pathname === fromPath;
359
- })?.id];
360
- pathname = this.resolvePathWithBase(fromPath, fromRouteByFromPathRouteId?.to ?? fromPath);
361
- }
362
- const prevParams = { ...last(fromMatches)?.params };
363
- let nextParams = (dest.params ?? true) === true
364
- ? prevParams
365
- : {
366
- ...prevParams,
367
- ...functionalUpdate(dest.params, prevParams),
368
- };
369
- if (Object.keys(nextParams).length > 0) {
370
- matchedRoutesResult?.matchedRoutes
371
- .map((route) => {
372
- return (route.options.params?.stringify ?? route.options.stringifyParams);
373
- })
374
- .filter(Boolean)
375
- .forEach((fn) => {
376
- nextParams = { ...nextParams, ...fn(nextParams) };
377
- });
378
- }
379
- pathname = interpolatePath({
380
- path: pathname,
381
- params: nextParams ?? {},
382
- leaveWildcards: false,
383
- leaveParams: opts.leaveParams,
384
- decodeCharMap: this.pathParamsDecodeCharMap,
385
- }).interpolatedPath;
386
- let search = fromSearch;
387
- if (opts._includeValidateSearch && this.options.search?.strict) {
388
- let validatedSearch = {};
389
- matchedRoutesResult?.matchedRoutes.forEach((route) => {
390
- try {
391
- if (route.options.validateSearch) {
392
- validatedSearch = {
393
- ...validatedSearch,
394
- ...(validateSearch(route.options.validateSearch, {
395
- ...validatedSearch,
396
- ...search,
397
- }) ?? {}),
398
- };
399
- }
400
- }
401
- catch {
402
- // ignore errors here because they are already handled in matchRoutes
403
- }
404
- });
405
- search = validatedSearch;
406
- }
407
- const applyMiddlewares = (search) => {
408
- const allMiddlewares = matchedRoutesResult?.matchedRoutes.reduce((acc, route) => {
409
- const middlewares = [];
410
- if ('search' in route.options) {
411
- if (route.options.search?.middlewares) {
412
- middlewares.push(...route.options.search.middlewares);
413
- }
414
- }
415
- // TODO remove preSearchFilters and postSearchFilters in v2
416
- else if (route.options.preSearchFilters ||
417
- route.options.postSearchFilters) {
418
- const legacyMiddleware = ({ search, next, }) => {
419
- let nextSearch = search;
420
- if ('preSearchFilters' in route.options &&
421
- route.options.preSearchFilters) {
422
- nextSearch = route.options.preSearchFilters.reduce((prev, next) => next(prev), search);
423
- }
424
- const result = next(nextSearch);
425
- if ('postSearchFilters' in route.options &&
426
- route.options.postSearchFilters) {
427
- return route.options.postSearchFilters.reduce((prev, next) => next(prev), result);
428
- }
429
- return result;
430
- };
431
- middlewares.push(legacyMiddleware);
432
- }
433
- if (opts._includeValidateSearch && route.options.validateSearch) {
434
- const validate = ({ search, next }) => {
435
- const result = next(search);
436
- try {
437
- const validatedSearch = {
438
- ...result,
439
- ...(validateSearch(route.options.validateSearch, result) ?? {}),
440
- };
441
- return validatedSearch;
442
- }
443
- catch {
444
- // ignore errors here because they are already handled in matchRoutes
445
- return result;
446
- }
447
- };
448
- middlewares.push(validate);
449
- }
450
- return acc.concat(middlewares);
451
- }, []) ?? [];
452
- // the chain ends here since `next` is not called
453
- const final = ({ search }) => {
454
- if (!dest.search) {
455
- return {};
456
- }
457
- if (dest.search === true) {
458
- return search;
459
- }
460
- return functionalUpdate(dest.search, search);
461
- };
462
- allMiddlewares.push(final);
463
- const applyNext = (index, currentSearch) => {
464
- // no more middlewares left, return the current search
465
- if (index >= allMiddlewares.length) {
466
- return currentSearch;
467
- }
468
- const middleware = allMiddlewares[index];
469
- const next = (newSearch) => {
470
- return applyNext(index + 1, newSearch);
471
- };
472
- return middleware({ search: currentSearch, next });
473
- };
474
- // Start applying middlewares
475
- return applyNext(0, search);
476
- };
477
- search = applyMiddlewares(search);
478
- search = replaceEqualDeep(fromSearch, search);
479
- const searchStr = this.options.stringifySearch(search);
480
- const hash = dest.hash === true
481
- ? this.latestLocation.hash
482
- : dest.hash
483
- ? functionalUpdate(dest.hash, this.latestLocation.hash)
484
- : undefined;
485
- const hashStr = hash ? `#${hash}` : '';
486
- let nextState = dest.state === true
487
- ? this.latestLocation.state
488
- : dest.state
489
- ? functionalUpdate(dest.state, this.latestLocation.state)
490
- : {};
491
- nextState = replaceEqualDeep(this.latestLocation.state, nextState);
492
- return {
493
- pathname,
494
- search,
495
- searchStr,
496
- state: nextState,
497
- hash: hash ?? '',
498
- href: `${pathname}${searchStr}${hashStr}`,
499
- unmaskOnReload: dest.unmaskOnReload,
500
- };
501
- };
502
- const buildWithMatches = (dest = {}, maskedDest) => {
503
- const next = build(dest);
504
- let maskedNext = maskedDest ? build(maskedDest) : undefined;
505
- if (!maskedNext) {
506
- let params = {};
507
- const foundMask = this.options.routeMasks?.find((d) => {
508
- const match = matchPathname(this.basepath, next.pathname, {
509
- to: d.from,
510
- caseSensitive: false,
511
- fuzzy: false,
512
- });
513
- if (match) {
514
- params = match;
515
- return true;
516
- }
517
- return false;
518
- });
519
- if (foundMask) {
520
- const { from: _from, ...maskProps } = foundMask;
521
- maskedDest = {
522
- ...pick(opts, ['from']),
523
- ...maskProps,
524
- params,
525
- };
526
- maskedNext = build(maskedDest);
527
- }
528
- }
529
- const nextMatches = this.getMatchedRoutes(next, dest);
530
- const final = build(dest, nextMatches);
531
- if (maskedNext) {
532
- const maskedMatches = this.getMatchedRoutes(maskedNext, maskedDest);
533
- const maskedFinal = build(maskedDest, maskedMatches);
534
- final.maskedLocation = maskedFinal;
535
- }
536
- return final;
537
- };
538
- if (opts.mask) {
539
- return buildWithMatches(opts, {
540
- ...pick(opts, ['from']),
541
- ...opts.mask,
542
- });
543
- }
544
- return buildWithMatches(opts);
545
- };
546
- this.commitLocation = ({ viewTransition, ignoreBlocker, ...next }) => {
547
- const isSameState = () => {
548
- // the following props are ignored but may still be provided when navigating,
549
- // temporarily add the previous values to the next state so they don't affect
550
- // the comparison
551
- const ignoredProps = [
552
- 'key',
553
- '__TSR_index',
554
- '__hashScrollIntoViewOptions',
555
- ];
556
- ignoredProps.forEach((prop) => {
557
- ;
558
- next.state[prop] = this.latestLocation.state[prop];
559
- });
560
- const isEqual = deepEqual(next.state, this.latestLocation.state);
561
- ignoredProps.forEach((prop) => {
562
- delete next.state[prop];
563
- });
564
- return isEqual;
565
- };
566
- const isSameUrl = this.latestLocation.href === next.href;
567
- const previousCommitPromise = this.commitLocationPromise;
568
- this.commitLocationPromise = createControlledPromise(() => {
569
- previousCommitPromise?.resolve();
570
- });
571
- // Don't commit to history if nothing changed
572
- if (isSameUrl && isSameState()) {
573
- this.load();
574
- }
575
- else {
576
- // eslint-disable-next-line prefer-const
577
- let { maskedLocation, hashScrollIntoView, ...nextHistory } = next;
578
- if (maskedLocation) {
579
- nextHistory = {
580
- ...maskedLocation,
581
- state: {
582
- ...maskedLocation.state,
583
- __tempKey: undefined,
584
- __tempLocation: {
585
- ...nextHistory,
586
- search: nextHistory.searchStr,
587
- state: {
588
- ...nextHistory.state,
589
- __tempKey: undefined,
590
- __tempLocation: undefined,
591
- key: undefined,
592
- },
593
- },
594
- },
595
- };
596
- if (nextHistory.unmaskOnReload ??
597
- this.options.unmaskOnReload ??
598
- false) {
599
- nextHistory.state.__tempKey = this.tempLocationKey;
600
- }
601
- }
602
- nextHistory.state.__hashScrollIntoViewOptions =
603
- hashScrollIntoView ?? this.options.defaultHashScrollIntoView ?? true;
604
- this.shouldViewTransition = viewTransition;
605
- this.history[next.replace ? 'replace' : 'push'](nextHistory.href, nextHistory.state, { ignoreBlocker });
606
- }
607
- this.resetNextScroll = next.resetScroll ?? true;
608
- if (!this.history.subscribers.size) {
609
- this.load();
610
- }
611
- return this.commitLocationPromise;
612
- };
613
- this.buildAndCommitLocation = ({ replace, resetScroll, hashScrollIntoView, viewTransition, ignoreBlocker, href, ...rest } = {}) => {
614
- if (href) {
615
- const currentIndex = this.history.location.state.__TSR_index;
616
- const parsed = parseHref(href, {
617
- __TSR_index: replace ? currentIndex : currentIndex + 1,
618
- });
619
- rest.to = parsed.pathname;
620
- rest.search = this.options.parseSearch(parsed.search);
621
- // remove the leading `#` from the hash
622
- rest.hash = parsed.hash.slice(1);
623
- }
624
- const location = this.buildLocation({
625
- ...rest,
626
- _includeValidateSearch: true,
627
- });
628
- return this.commitLocation({
629
- ...location,
630
- viewTransition,
631
- replace,
632
- resetScroll,
633
- hashScrollIntoView,
634
- ignoreBlocker,
635
- });
636
- };
637
- this.navigate = ({ to, reloadDocument, href, ...rest }) => {
638
- if (reloadDocument) {
639
- if (!href) {
640
- const location = this.buildLocation({ to, ...rest });
641
- href = this.history.createHref(location.href);
642
- }
643
- if (rest.replace) {
644
- window.location.replace(href);
645
- }
646
- else {
647
- window.location.href = href;
648
- }
649
- return;
650
- }
651
- return this.buildAndCommitLocation({
652
- ...rest,
653
- href,
654
- to: to,
655
- });
656
- };
657
- this.load = async (opts) => {
658
- this.latestLocation = this.parseLocation(this.latestLocation);
659
- let redirect;
660
- let notFound;
661
- let loadPromise;
662
- // eslint-disable-next-line prefer-const
663
- loadPromise = new Promise((resolve) => {
664
- this.startTransition(async () => {
665
- try {
666
- const next = this.latestLocation;
667
- const prevLocation = this.state.resolvedLocation;
668
- // Cancel any pending matches
669
- this.cancelMatches();
670
- let pendingMatches;
671
- batch(() => {
672
- // this call breaks a route context of destination route after a redirect
673
- // we should be fine not eagerly calling this since we call it later
674
- // this.clearExpiredCache()
675
- // Match the routes
676
- pendingMatches = this.matchRoutes(next);
677
- // Ingest the new matches
678
- this.__store.setState((s) => ({
679
- ...s,
680
- status: 'pending',
681
- isLoading: true,
682
- location: next,
683
- pendingMatches,
684
- // If a cached moved to pendingMatches, remove it from cachedMatches
685
- cachedMatches: s.cachedMatches.filter((d) => {
686
- return !pendingMatches.find((e) => e.id === d.id);
687
- }),
688
- }));
689
- });
690
- if (!this.state.redirect) {
691
- this.emit({
692
- type: 'onBeforeNavigate',
693
- ...getLocationChangeInfo({
694
- resolvedLocation: prevLocation,
695
- location: next,
696
- }),
697
- });
698
- }
699
- this.emit({
700
- type: 'onBeforeLoad',
701
- ...getLocationChangeInfo({
702
- resolvedLocation: prevLocation,
703
- location: next,
704
- }),
705
- });
706
- await this.loadMatches({
707
- sync: opts?.sync,
708
- matches: pendingMatches,
709
- location: next,
710
- // eslint-disable-next-line @typescript-eslint/require-await
711
- onReady: async () => {
712
- // eslint-disable-next-line @typescript-eslint/require-await
713
- this.startViewTransition(async () => {
714
- // this.viewTransitionPromise = createControlledPromise<true>()
715
- // Commit the pending matches. If a previous match was
716
- // removed, place it in the cachedMatches
717
- let exitingMatches;
718
- let enteringMatches;
719
- let stayingMatches;
720
- batch(() => {
721
- this.__store.setState((s) => {
722
- const previousMatches = s.matches;
723
- const newMatches = s.pendingMatches || s.matches;
724
- exitingMatches = previousMatches.filter((match) => !newMatches.find((d) => d.id === match.id));
725
- enteringMatches = newMatches.filter((match) => !previousMatches.find((d) => d.id === match.id));
726
- stayingMatches = previousMatches.filter((match) => newMatches.find((d) => d.id === match.id));
727
- return {
728
- ...s,
729
- isLoading: false,
730
- loadedAt: Date.now(),
731
- matches: newMatches,
732
- pendingMatches: undefined,
733
- cachedMatches: [
734
- ...s.cachedMatches,
735
- ...exitingMatches.filter((d) => d.status !== 'error'),
736
- ],
737
- };
738
- });
739
- this.clearExpiredCache();
740
- });
741
- [
742
- [exitingMatches, 'onLeave'],
743
- [enteringMatches, 'onEnter'],
744
- [stayingMatches, 'onStay'],
745
- ].forEach(([matches, hook]) => {
746
- matches.forEach((match) => {
747
- this.looseRoutesById[match.routeId].options[hook]?.(match);
748
- });
749
- });
750
- });
751
- },
752
- });
753
- }
754
- catch (err) {
755
- if (isResolvedRedirect(err)) {
756
- redirect = err;
757
- if (!this.isServer) {
758
- this.navigate({
759
- ...redirect,
760
- replace: true,
761
- ignoreBlocker: true,
762
- });
763
- }
764
- }
765
- else if (isNotFound(err)) {
766
- notFound = err;
767
- }
768
- this.__store.setState((s) => ({
769
- ...s,
770
- statusCode: redirect
771
- ? redirect.statusCode
772
- : notFound
773
- ? 404
774
- : s.matches.some((d) => d.status === 'error')
775
- ? 500
776
- : 200,
777
- redirect,
778
- }));
779
- }
780
- if (this.latestLoadPromise === loadPromise) {
781
- this.commitLocationPromise?.resolve();
782
- this.latestLoadPromise = undefined;
783
- this.commitLocationPromise = undefined;
784
- }
785
- resolve();
786
- });
787
- });
788
- this.latestLoadPromise = loadPromise;
789
- await loadPromise;
790
- while (this.latestLoadPromise &&
791
- loadPromise !== this.latestLoadPromise) {
792
- await this.latestLoadPromise;
793
- }
794
- if (this.hasNotFoundMatch()) {
795
- this.__store.setState((s) => ({
796
- ...s,
797
- statusCode: 404,
798
- }));
799
- }
800
- };
801
- this.startViewTransition = (fn) => {
802
- // Determine if we should start a view transition from the navigation
803
- // or from the router default
804
- const shouldViewTransition = this.shouldViewTransition ?? this.options.defaultViewTransition;
805
- // Reset the view transition flag
806
- delete this.shouldViewTransition;
807
- // Attempt to start a view transition (or just apply the changes if we can't)
808
- if (shouldViewTransition &&
809
- typeof document !== 'undefined' &&
810
- 'startViewTransition' in document &&
811
- typeof document.startViewTransition === 'function') {
812
- // lib.dom.ts doesn't support viewTransition types variant yet.
813
- // TODO: Fix this when dom types are updated
814
- let startViewTransitionParams;
815
- if (typeof shouldViewTransition === 'object' &&
816
- this.isViewTransitionTypesSupported) {
817
- startViewTransitionParams = {
818
- update: fn,
819
- types: shouldViewTransition.types,
820
- };
821
- }
822
- else {
823
- startViewTransitionParams = fn;
824
- }
825
- document.startViewTransition(startViewTransitionParams);
826
- }
827
- else {
828
- fn();
829
- }
830
- };
831
- this.updateMatch = (id, updater) => {
832
- let updated;
833
- const isPending = this.state.pendingMatches?.find((d) => d.id === id);
834
- const isMatched = this.state.matches.find((d) => d.id === id);
835
- const isCached = this.state.cachedMatches.find((d) => d.id === id);
836
- const matchesKey = isPending
837
- ? 'pendingMatches'
838
- : isMatched
839
- ? 'matches'
840
- : isCached
841
- ? 'cachedMatches'
842
- : '';
843
- if (matchesKey) {
844
- this.__store.setState((s) => ({
845
- ...s,
846
- [matchesKey]: s[matchesKey]?.map((d) => d.id === id ? (updated = updater(d)) : d),
847
- }));
848
- }
849
- return updated;
850
- };
851
- this.getMatch = (matchId) => {
852
- return [
853
- ...this.state.cachedMatches,
854
- ...(this.state.pendingMatches ?? []),
855
- ...this.state.matches,
856
- ].find((d) => d.id === matchId);
857
- };
858
- this.loadMatches = async ({ location, matches, preload: allPreload, onReady, updateMatch = this.updateMatch, sync, }) => {
859
- let firstBadMatchIndex;
860
- let rendered = false;
861
- const triggerOnReady = async () => {
862
- if (!rendered) {
863
- rendered = true;
864
- await onReady?.();
865
- }
866
- };
867
- const resolvePreload = (matchId) => {
868
- return !!(allPreload && !this.state.matches.find((d) => d.id === matchId));
869
- };
870
- if (!this.isServer && !this.state.matches.length) {
871
- triggerOnReady();
872
- }
873
- const handleRedirectAndNotFound = (match, err) => {
874
- if (isResolvedRedirect(err)) {
875
- if (!err.reloadDocument) {
876
- throw err;
877
- }
878
- }
879
- if (isRedirect(err) || isNotFound(err)) {
880
- updateMatch(match.id, (prev) => ({
881
- ...prev,
882
- status: isRedirect(err)
883
- ? 'redirected'
884
- : isNotFound(err)
885
- ? 'notFound'
886
- : 'error',
887
- isFetching: false,
888
- error: err,
889
- beforeLoadPromise: undefined,
890
- loaderPromise: undefined,
891
- }));
892
- if (!err.routeId) {
893
- ;
894
- err.routeId = match.routeId;
895
- }
896
- match.beforeLoadPromise?.resolve();
897
- match.loaderPromise?.resolve();
898
- match.loadPromise?.resolve();
899
- if (isRedirect(err)) {
900
- rendered = true;
901
- err = this.resolveRedirect({ ...err, _fromLocation: location });
902
- throw err;
903
- }
904
- else if (isNotFound(err)) {
905
- this._handleNotFound(matches, err, {
906
- updateMatch,
907
- });
908
- this.serverSsr?.onMatchSettled({
909
- router: this,
910
- match: this.getMatch(match.id),
911
- });
912
- throw err;
913
- }
914
- }
915
- };
916
- try {
917
- await new Promise((resolveAll, rejectAll) => {
918
- ;
919
- (async () => {
920
- try {
921
- const handleSerialError = (index, err, routerCode) => {
922
- const { id: matchId, routeId } = matches[index];
923
- const route = this.looseRoutesById[routeId];
924
- // Much like suspense, we use a promise here to know if
925
- // we've been outdated by a new loadMatches call and
926
- // should abort the current async operation
927
- if (err instanceof Promise) {
928
- throw err;
929
- }
930
- err.routerCode = routerCode;
931
- firstBadMatchIndex = firstBadMatchIndex ?? index;
932
- handleRedirectAndNotFound(this.getMatch(matchId), err);
933
- try {
934
- route.options.onError?.(err);
935
- }
936
- catch (errorHandlerErr) {
937
- err = errorHandlerErr;
938
- handleRedirectAndNotFound(this.getMatch(matchId), err);
939
- }
940
- updateMatch(matchId, (prev) => {
941
- prev.beforeLoadPromise?.resolve();
942
- prev.loadPromise?.resolve();
943
- return {
944
- ...prev,
945
- error: err,
946
- status: 'error',
947
- isFetching: false,
948
- updatedAt: Date.now(),
949
- abortController: new AbortController(),
950
- beforeLoadPromise: undefined,
951
- };
952
- });
953
- };
954
- for (const [index, { id: matchId, routeId }] of matches.entries()) {
955
- const existingMatch = this.getMatch(matchId);
956
- const parentMatchId = matches[index - 1]?.id;
957
- const route = this.looseRoutesById[routeId];
958
- const pendingMs = route.options.pendingMs ?? this.options.defaultPendingMs;
959
- const shouldPending = !!(onReady &&
960
- !this.isServer &&
961
- !resolvePreload(matchId) &&
962
- (route.options.loader || route.options.beforeLoad) &&
963
- typeof pendingMs === 'number' &&
964
- pendingMs !== Infinity &&
965
- (route.options.pendingComponent ??
966
- this.options.defaultPendingComponent));
967
- let executeBeforeLoad = true;
968
- if (
969
- // If we are in the middle of a load, either of these will be present
970
- // (not to be confused with `loadPromise`, which is always defined)
971
- existingMatch.beforeLoadPromise ||
972
- existingMatch.loaderPromise) {
973
- if (shouldPending) {
974
- setTimeout(() => {
975
- try {
976
- // Update the match and prematurely resolve the loadMatches promise so that
977
- // the pending component can start rendering
978
- triggerOnReady();
979
- }
980
- catch { }
981
- }, pendingMs);
982
- }
983
- // Wait for the beforeLoad to resolve before we continue
984
- await existingMatch.beforeLoadPromise;
985
- executeBeforeLoad = this.getMatch(matchId).status !== 'success';
986
- }
987
- if (executeBeforeLoad) {
988
- // If we are not in the middle of a load OR the previous load failed, start it
989
- try {
990
- updateMatch(matchId, (prev) => {
991
- // explicitly capture the previous loadPromise
992
- const prevLoadPromise = prev.loadPromise;
993
- return {
994
- ...prev,
995
- loadPromise: createControlledPromise(() => {
996
- prevLoadPromise?.resolve();
997
- }),
998
- beforeLoadPromise: createControlledPromise(),
999
- };
1000
- });
1001
- const abortController = new AbortController();
1002
- let pendingTimeout;
1003
- if (shouldPending) {
1004
- // If we might show a pending component, we need to wait for the
1005
- // pending promise to resolve before we start showing that state
1006
- pendingTimeout = setTimeout(() => {
1007
- try {
1008
- // Update the match and prematurely resolve the loadMatches promise so that
1009
- // the pending component can start rendering
1010
- triggerOnReady();
1011
- }
1012
- catch { }
1013
- }, pendingMs);
1014
- }
1015
- const { paramsError, searchError } = this.getMatch(matchId);
1016
- if (paramsError) {
1017
- handleSerialError(index, paramsError, 'PARSE_PARAMS');
1018
- }
1019
- if (searchError) {
1020
- handleSerialError(index, searchError, 'VALIDATE_SEARCH');
1021
- }
1022
- const getParentMatchContext = () => parentMatchId
1023
- ? this.getMatch(parentMatchId).context
1024
- : (this.options.context ?? {});
1025
- updateMatch(matchId, (prev) => ({
1026
- ...prev,
1027
- isFetching: 'beforeLoad',
1028
- fetchCount: prev.fetchCount + 1,
1029
- abortController,
1030
- pendingTimeout,
1031
- context: {
1032
- ...getParentMatchContext(),
1033
- ...prev.__routeContext,
1034
- },
1035
- }));
1036
- const { search, params, context, cause } = this.getMatch(matchId);
1037
- const preload = resolvePreload(matchId);
1038
- const beforeLoadFnContext = {
1039
- search,
1040
- abortController,
1041
- params,
1042
- preload,
1043
- context,
1044
- location,
1045
- navigate: (opts) => this.navigate({ ...opts, _fromLocation: location }),
1046
- buildLocation: this.buildLocation,
1047
- cause: preload ? 'preload' : cause,
1048
- matches,
1049
- };
1050
- const beforeLoadContext = (await route.options.beforeLoad?.(beforeLoadFnContext)) ??
1051
- {};
1052
- if (isRedirect(beforeLoadContext) ||
1053
- isNotFound(beforeLoadContext)) {
1054
- handleSerialError(index, beforeLoadContext, 'BEFORE_LOAD');
1055
- }
1056
- updateMatch(matchId, (prev) => {
1057
- return {
1058
- ...prev,
1059
- __beforeLoadContext: beforeLoadContext,
1060
- context: {
1061
- ...getParentMatchContext(),
1062
- ...prev.__routeContext,
1063
- ...beforeLoadContext,
1064
- },
1065
- abortController,
1066
- };
1067
- });
1068
- }
1069
- catch (err) {
1070
- handleSerialError(index, err, 'BEFORE_LOAD');
1071
- }
1072
- updateMatch(matchId, (prev) => {
1073
- prev.beforeLoadPromise?.resolve();
1074
- return {
1075
- ...prev,
1076
- beforeLoadPromise: undefined,
1077
- isFetching: false,
1078
- };
1079
- });
1080
- }
1081
- }
1082
- const validResolvedMatches = matches.slice(0, firstBadMatchIndex);
1083
- const matchPromises = [];
1084
- validResolvedMatches.forEach(({ id: matchId, routeId }, index) => {
1085
- matchPromises.push((async () => {
1086
- const { loaderPromise: prevLoaderPromise } = this.getMatch(matchId);
1087
- let loaderShouldRunAsync = false;
1088
- let loaderIsRunningAsync = false;
1089
- if (prevLoaderPromise) {
1090
- await prevLoaderPromise;
1091
- const match = this.getMatch(matchId);
1092
- if (match.error) {
1093
- handleRedirectAndNotFound(match, match.error);
1094
- }
1095
- }
1096
- else {
1097
- const parentMatchPromise = matchPromises[index - 1];
1098
- const route = this.looseRoutesById[routeId];
1099
- const getLoaderContext = () => {
1100
- const { params, loaderDeps, abortController, context, cause, } = this.getMatch(matchId);
1101
- const preload = resolvePreload(matchId);
1102
- return {
1103
- params,
1104
- deps: loaderDeps,
1105
- preload: !!preload,
1106
- parentMatchPromise,
1107
- abortController: abortController,
1108
- context,
1109
- location,
1110
- navigate: (opts) => this.navigate({ ...opts, _fromLocation: location }),
1111
- cause: preload ? 'preload' : cause,
1112
- route,
1113
- };
1114
- };
1115
- // This is where all of the stale-while-revalidate magic happens
1116
- const age = Date.now() - this.getMatch(matchId).updatedAt;
1117
- const preload = resolvePreload(matchId);
1118
- const staleAge = preload
1119
- ? (route.options.preloadStaleTime ??
1120
- this.options.defaultPreloadStaleTime ??
1121
- 30000) // 30 seconds for preloads by default
1122
- : (route.options.staleTime ??
1123
- this.options.defaultStaleTime ??
1124
- 0);
1125
- const shouldReloadOption = route.options.shouldReload;
1126
- // Default to reloading the route all the time
1127
- // Allow shouldReload to get the last say,
1128
- // if provided.
1129
- const shouldReload = typeof shouldReloadOption === 'function'
1130
- ? shouldReloadOption(getLoaderContext())
1131
- : shouldReloadOption;
1132
- updateMatch(matchId, (prev) => ({
1133
- ...prev,
1134
- loaderPromise: createControlledPromise(),
1135
- preload: !!preload &&
1136
- !this.state.matches.find((d) => d.id === matchId),
1137
- }));
1138
- const runLoader = async () => {
1139
- try {
1140
- // If the Matches component rendered
1141
- // the pending component and needs to show it for
1142
- // a minimum duration, we''ll wait for it to resolve
1143
- // before committing to the match and resolving
1144
- // the loadPromise
1145
- const potentialPendingMinPromise = async () => {
1146
- const latestMatch = this.getMatch(matchId);
1147
- if (latestMatch.minPendingPromise) {
1148
- await latestMatch.minPendingPromise;
1149
- }
1150
- };
1151
- // Actually run the loader and handle the result
1152
- try {
1153
- this.loadRouteChunk(route);
1154
- updateMatch(matchId, (prev) => ({
1155
- ...prev,
1156
- isFetching: 'loader',
1157
- }));
1158
- // Kick off the loader!
1159
- const loaderData = await route.options.loader?.(getLoaderContext());
1160
- handleRedirectAndNotFound(this.getMatch(matchId), loaderData);
1161
- // Lazy option can modify the route options,
1162
- // so we need to wait for it to resolve before
1163
- // we can use the options
1164
- await route._lazyPromise;
1165
- await potentialPendingMinPromise();
1166
- const assetContext = {
1167
- matches,
1168
- match: this.getMatch(matchId),
1169
- params: this.getMatch(matchId).params,
1170
- loaderData,
1171
- };
1172
- const headFnContent = route.options.head?.(assetContext);
1173
- const meta = headFnContent?.meta;
1174
- const links = headFnContent?.links;
1175
- const headScripts = headFnContent?.scripts;
1176
- const scripts = route.options.scripts?.(assetContext);
1177
- const headers = route.options.headers?.({
1178
- loaderData,
1179
- });
1180
- updateMatch(matchId, (prev) => ({
1181
- ...prev,
1182
- error: undefined,
1183
- status: 'success',
1184
- isFetching: false,
1185
- updatedAt: Date.now(),
1186
- loaderData,
1187
- meta,
1188
- links,
1189
- headScripts,
1190
- headers,
1191
- scripts,
1192
- }));
1193
- }
1194
- catch (e) {
1195
- let error = e;
1196
- await potentialPendingMinPromise();
1197
- handleRedirectAndNotFound(this.getMatch(matchId), e);
1198
- try {
1199
- route.options.onError?.(e);
1200
- }
1201
- catch (onErrorError) {
1202
- error = onErrorError;
1203
- handleRedirectAndNotFound(this.getMatch(matchId), onErrorError);
1204
- }
1205
- updateMatch(matchId, (prev) => ({
1206
- ...prev,
1207
- error,
1208
- status: 'error',
1209
- isFetching: false,
1210
- }));
1211
- }
1212
- this.serverSsr?.onMatchSettled({
1213
- router: this,
1214
- match: this.getMatch(matchId),
1215
- });
1216
- // Last but not least, wait for the the components
1217
- // to be preloaded before we resolve the match
1218
- await route._componentsPromise;
1219
- }
1220
- catch (err) {
1221
- updateMatch(matchId, (prev) => ({
1222
- ...prev,
1223
- loaderPromise: undefined,
1224
- }));
1225
- handleRedirectAndNotFound(this.getMatch(matchId), err);
1226
- }
1227
- };
1228
- // If the route is successful and still fresh, just resolve
1229
- const { status, invalid } = this.getMatch(matchId);
1230
- loaderShouldRunAsync =
1231
- status === 'success' &&
1232
- (invalid || (shouldReload ?? age > staleAge));
1233
- if (preload && route.options.preload === false) {
1234
- // Do nothing
1235
- }
1236
- else if (loaderShouldRunAsync && !sync) {
1237
- loaderIsRunningAsync = true;
1238
- (async () => {
1239
- try {
1240
- await runLoader();
1241
- const { loaderPromise, loadPromise } = this.getMatch(matchId);
1242
- loaderPromise?.resolve();
1243
- loadPromise?.resolve();
1244
- updateMatch(matchId, (prev) => ({
1245
- ...prev,
1246
- loaderPromise: undefined,
1247
- }));
1248
- }
1249
- catch (err) {
1250
- if (isResolvedRedirect(err)) {
1251
- await this.navigate(err);
1252
- }
1253
- }
1254
- })();
1255
- }
1256
- else if (status !== 'success' ||
1257
- (loaderShouldRunAsync && sync)) {
1258
- await runLoader();
1259
- }
1260
- }
1261
- if (!loaderIsRunningAsync) {
1262
- const { loaderPromise, loadPromise } = this.getMatch(matchId);
1263
- loaderPromise?.resolve();
1264
- loadPromise?.resolve();
1265
- }
1266
- updateMatch(matchId, (prev) => ({
1267
- ...prev,
1268
- isFetching: loaderIsRunningAsync ? prev.isFetching : false,
1269
- loaderPromise: loaderIsRunningAsync
1270
- ? prev.loaderPromise
1271
- : undefined,
1272
- invalid: false,
1273
- }));
1274
- return this.getMatch(matchId);
1275
- })());
1276
- });
1277
- await Promise.all(matchPromises);
1278
- resolveAll();
1279
- }
1280
- catch (err) {
1281
- rejectAll(err);
1282
- }
1283
- })();
1284
- });
1285
- await triggerOnReady();
1286
- }
1287
- catch (err) {
1288
- if (isRedirect(err) || isNotFound(err)) {
1289
- if (isNotFound(err) && !allPreload) {
1290
- await triggerOnReady();
1291
- }
1292
- throw err;
1293
- }
1294
- }
1295
- return matches;
1296
- };
1297
- this.invalidate = (opts) => {
1298
- const invalidate = (d) => {
1299
- if (opts?.filter?.(d) ?? true) {
1300
- return {
1301
- ...d,
1302
- invalid: true,
1303
- ...(d.status === 'error'
1304
- ? { status: 'pending', error: undefined }
1305
- : {}),
1306
- };
1307
- }
1308
- return d;
1309
- };
1310
- this.__store.setState((s) => ({
1311
- ...s,
1312
- matches: s.matches.map(invalidate),
1313
- cachedMatches: s.cachedMatches.map(invalidate),
1314
- pendingMatches: s.pendingMatches?.map(invalidate),
1315
- }));
1316
- return this.load({ sync: opts?.sync });
1317
- };
1318
- this.resolveRedirect = (err) => {
1319
- const redirect = err;
1320
- if (!redirect.href) {
1321
- redirect.href = this.buildLocation(redirect).href;
1322
- }
1323
- return redirect;
1324
- };
1325
- this.clearCache = (opts) => {
1326
- const filter = opts?.filter;
1327
- if (filter !== undefined) {
1328
- this.__store.setState((s) => {
1329
- return {
1330
- ...s,
1331
- cachedMatches: s.cachedMatches.filter((m) => !filter(m)),
1332
- };
1333
- });
1334
- }
1335
- else {
1336
- this.__store.setState((s) => {
1337
- return {
1338
- ...s,
1339
- cachedMatches: [],
1340
- };
1341
- });
1342
- }
1343
- };
1344
- this.clearExpiredCache = () => {
1345
- // This is where all of the garbage collection magic happens
1346
- const filter = (d) => {
1347
- const route = this.looseRoutesById[d.routeId];
1348
- if (!route.options.loader) {
1349
- return true;
1350
- }
1351
- // If the route was preloaded, use the preloadGcTime
1352
- // otherwise, use the gcTime
1353
- const gcTime = (d.preload
1354
- ? (route.options.preloadGcTime ?? this.options.defaultPreloadGcTime)
1355
- : (route.options.gcTime ?? this.options.defaultGcTime)) ??
1356
- 5 * 60 * 1000;
1357
- return !(d.status !== 'error' && Date.now() - d.updatedAt < gcTime);
1358
- };
1359
- this.clearCache({ filter });
1360
- };
1361
- this.loadRouteChunk = (route) => {
1362
- if (route._lazyPromise === undefined) {
1363
- if (route.lazyFn) {
1364
- route._lazyPromise = route.lazyFn().then((lazyRoute) => {
1365
- // explicitly don't copy over the lazy route's id
1366
- const { id: _id, ...options } = lazyRoute.options;
1367
- Object.assign(route.options, options);
1368
- });
1369
- }
1370
- else {
1371
- route._lazyPromise = Promise.resolve();
1372
- }
1373
- }
1374
- // If for some reason lazy resolves more lazy components...
1375
- // We'll wait for that before pre attempt to preload any
1376
- // components themselves.
1377
- if (route._componentsPromise === undefined) {
1378
- route._componentsPromise = route._lazyPromise.then(() => Promise.all(componentTypes.map(async (type) => {
1379
- const component = route.options[type];
1380
- if (component?.preload) {
1381
- await component.preload();
1382
- }
1383
- })));
1384
- }
1385
- return route._componentsPromise;
1386
- };
1387
- this.preloadRoute = async (opts) => {
1388
- const next = this.buildLocation(opts);
1389
- let matches = this.matchRoutes(next, {
1390
- throwOnError: true,
1391
- preload: true,
1392
- dest: opts,
1393
- });
1394
- const activeMatchIds = new Set([...this.state.matches, ...(this.state.pendingMatches ?? [])].map((d) => d.id));
1395
- const loadedMatchIds = new Set([
1396
- ...activeMatchIds,
1397
- ...this.state.cachedMatches.map((d) => d.id),
1398
- ]);
1399
- // If the matches are already loaded, we need to add them to the cachedMatches
1400
- batch(() => {
1401
- matches.forEach((match) => {
1402
- if (!loadedMatchIds.has(match.id)) {
1403
- this.__store.setState((s) => ({
1404
- ...s,
1405
- cachedMatches: [...s.cachedMatches, match],
1406
- }));
1407
- }
1408
- });
1409
- });
1410
- try {
1411
- matches = await this.loadMatches({
1412
- matches,
1413
- location: next,
1414
- preload: true,
1415
- updateMatch: (id, updater) => {
1416
- // Don't update the match if it's currently loaded
1417
- if (activeMatchIds.has(id)) {
1418
- matches = matches.map((d) => (d.id === id ? updater(d) : d));
1419
- }
1420
- else {
1421
- this.updateMatch(id, updater);
1422
- }
1423
- },
1424
- });
1425
- return matches;
1426
- }
1427
- catch (err) {
1428
- if (isRedirect(err)) {
1429
- if (err.reloadDocument) {
1430
- return undefined;
1431
- }
1432
- return await this.preloadRoute({
1433
- ...err,
1434
- _fromLocation: next,
1435
- });
1436
- }
1437
- if (!isNotFound(err)) {
1438
- // Preload errors are not fatal, but we should still log them
1439
- console.error(err);
1440
- }
1441
- return undefined;
1442
- }
1443
- };
1444
- this.matchRoute = (location, opts) => {
1445
- const matchLocation = {
1446
- ...location,
1447
- to: location.to
1448
- ? this.resolvePathWithBase((location.from || ''), location.to)
1449
- : undefined,
1450
- params: location.params || {},
1451
- leaveParams: true,
1452
- };
1453
- const next = this.buildLocation(matchLocation);
1454
- if (opts?.pending && this.state.status !== 'pending') {
1455
- return false;
1456
- }
1457
- const pending = opts?.pending === undefined ? !this.state.isLoading : opts.pending;
1458
- const baseLocation = pending
1459
- ? this.latestLocation
1460
- : this.state.resolvedLocation || this.state.location;
1461
- const match = matchPathname(this.basepath, baseLocation.pathname, {
1462
- ...opts,
1463
- to: next.pathname,
1464
- });
1465
- if (!match) {
1466
- return false;
1467
- }
1468
- if (location.params) {
1469
- if (!deepEqual(match, location.params, { partial: true })) {
1470
- return false;
1471
- }
1472
- }
1473
- if (match && (opts?.includeSearch ?? true)) {
1474
- return deepEqual(baseLocation.search, next.search, { partial: true })
1475
- ? match
1476
- : false;
1477
- }
1478
- return match;
1479
- };
1480
- this._handleNotFound = (matches, err, { updateMatch = this.updateMatch, } = {}) => {
1481
- const matchesByRouteId = Object.fromEntries(matches.map((match) => [match.routeId, match]));
1482
- // Start at the route that errored or default to the root route
1483
- let routeCursor = (err.global
1484
- ? this.looseRoutesById[rootRouteId]
1485
- : this.looseRoutesById[err.routeId]) ||
1486
- this.looseRoutesById[rootRouteId];
1487
- // Go up the tree until we find a route with a notFoundComponent or we hit the root
1488
- while (!routeCursor.options.notFoundComponent &&
1489
- !this.options.defaultNotFoundComponent &&
1490
- routeCursor.id !== rootRouteId) {
1491
- routeCursor = routeCursor.parentRoute;
1492
- invariant(routeCursor, 'Found invalid route tree while trying to find not-found handler.');
1493
- }
1494
- const match = matchesByRouteId[routeCursor.id];
1495
- invariant(match, 'Could not find match for route: ' + routeCursor.id);
1496
- // Assign the error to the match
1497
- updateMatch(match.id, (prev) => ({
1498
- ...prev,
1499
- status: 'notFound',
1500
- error: err,
1501
- isFetching: false,
1502
- }));
1503
- if (err.routerCode === 'BEFORE_LOAD' && routeCursor.parentRoute) {
1504
- err.routeId = routeCursor.parentRoute.id;
1505
- this._handleNotFound(matches, err, {
1506
- updateMatch,
1507
- });
1508
- }
1509
- };
1510
- this.hasNotFoundMatch = () => {
1511
- return this.__store.state.matches.some((d) => d.status === 'notFound' || d.globalNotFound);
1512
- };
1513
- this.update({
1514
- defaultPreloadDelay: 50,
1515
- defaultPendingMs: 1000,
1516
- defaultPendingMinMs: 500,
1517
- context: undefined,
1518
- ...options,
1519
- caseSensitive: options.caseSensitive ?? false,
1520
- notFoundMode: options.notFoundMode ?? 'fuzzy',
1521
- stringifySearch: options.stringifySearch ?? defaultStringifySearch,
1522
- parseSearch: options.parseSearch ?? defaultParseSearch,
1523
- });
1524
- if (typeof document !== 'undefined') {
1525
- ;
1526
- window.__TSR_ROUTER__ = this;
1527
- }
7
+ super(options);
1528
8
  }
1529
- get state() {
1530
- return this.__store.state;
1531
- }
1532
- get looseRoutesById() {
1533
- return this.routesById;
1534
- }
1535
- matchRoutesInternal(next, opts) {
1536
- const { foundRoute, matchedRoutes, routeParams } = this.getMatchedRoutes(next, opts?.dest);
1537
- let isGlobalNotFound = false;
1538
- // Check to see if the route needs a 404 entry
1539
- if (
1540
- // If we found a route, and it's not an index route and we have left over path
1541
- foundRoute
1542
- ? foundRoute.path !== '/' && routeParams['**']
1543
- : // Or if we didn't find a route and we have left over path
1544
- trimPathRight(next.pathname)) {
1545
- // If the user has defined an (old) 404 route, use it
1546
- if (this.options.notFoundRoute) {
1547
- matchedRoutes.push(this.options.notFoundRoute);
1548
- }
1549
- else {
1550
- // If there is no routes found during path matching
1551
- isGlobalNotFound = true;
1552
- }
1553
- }
1554
- const globalNotFoundRouteId = (() => {
1555
- if (!isGlobalNotFound) {
1556
- return undefined;
1557
- }
1558
- if (this.options.notFoundMode !== 'root') {
1559
- for (let i = matchedRoutes.length - 1; i >= 0; i--) {
1560
- const route = matchedRoutes[i];
1561
- if (route.children) {
1562
- return route.id;
1563
- }
1564
- }
1565
- }
1566
- return rootRouteId;
1567
- })();
1568
- const parseErrors = matchedRoutes.map((route) => {
1569
- let parsedParamsError;
1570
- const parseParams = route.options.params?.parse ?? route.options.parseParams;
1571
- if (parseParams) {
1572
- try {
1573
- const parsedParams = parseParams(routeParams);
1574
- // Add the parsed params to the accumulated params bag
1575
- Object.assign(routeParams, parsedParams);
1576
- }
1577
- catch (err) {
1578
- parsedParamsError = new PathParamError(err.message, {
1579
- cause: err,
1580
- });
1581
- if (opts?.throwOnError) {
1582
- throw parsedParamsError;
1583
- }
1584
- return parsedParamsError;
1585
- }
1586
- }
1587
- return;
1588
- });
1589
- const matches = [];
1590
- const getParentContext = (parentMatch) => {
1591
- const parentMatchId = parentMatch?.id;
1592
- const parentContext = !parentMatchId
1593
- ? (this.options.context ?? {})
1594
- : (parentMatch.context ?? this.options.context ?? {});
1595
- return parentContext;
1596
- };
1597
- matchedRoutes.forEach((route, index) => {
1598
- // Take each matched route and resolve + validate its search params
1599
- // This has to happen serially because each route's search params
1600
- // can depend on the parent route's search params
1601
- // It must also happen before we create the match so that we can
1602
- // pass the search params to the route's potential key function
1603
- // which is used to uniquely identify the route match in state
1604
- const parentMatch = matches[index - 1];
1605
- const [preMatchSearch, strictMatchSearch, searchError] = (() => {
1606
- // Validate the search params and stabilize them
1607
- const parentSearch = parentMatch?.search ?? next.search;
1608
- const parentStrictSearch = parentMatch?._strictSearch ?? {};
1609
- try {
1610
- const strictSearch = validateSearch(route.options.validateSearch, { ...parentSearch }) ??
1611
- {};
1612
- return [
1613
- {
1614
- ...parentSearch,
1615
- ...strictSearch,
1616
- },
1617
- { ...parentStrictSearch, ...strictSearch },
1618
- undefined,
1619
- ];
1620
- }
1621
- catch (err) {
1622
- let searchParamError = err;
1623
- if (!(err instanceof SearchParamError)) {
1624
- searchParamError = new SearchParamError(err.message, {
1625
- cause: err,
1626
- });
1627
- }
1628
- if (opts?.throwOnError) {
1629
- throw searchParamError;
1630
- }
1631
- return [parentSearch, {}, searchParamError];
1632
- }
1633
- })();
1634
- // This is where we need to call route.options.loaderDeps() to get any additional
1635
- // deps that the route's loader function might need to run. We need to do this
1636
- // before we create the match so that we can pass the deps to the route's
1637
- // potential key function which is used to uniquely identify the route match in state
1638
- const loaderDeps = route.options.loaderDeps?.({
1639
- search: preMatchSearch,
1640
- }) ?? '';
1641
- const loaderDepsHash = loaderDeps ? JSON.stringify(loaderDeps) : '';
1642
- const { usedParams, interpolatedPath } = interpolatePath({
1643
- path: route.fullPath,
1644
- params: routeParams,
1645
- decodeCharMap: this.pathParamsDecodeCharMap,
1646
- });
1647
- const matchId = interpolatePath({
1648
- path: route.id,
1649
- params: routeParams,
1650
- leaveWildcards: true,
1651
- decodeCharMap: this.pathParamsDecodeCharMap,
1652
- }).interpolatedPath + loaderDepsHash;
1653
- // Waste not, want not. If we already have a match for this route,
1654
- // reuse it. This is important for layout routes, which might stick
1655
- // around between navigation actions that only change leaf routes.
1656
- // Existing matches are matches that are already loaded along with
1657
- // pending matches that are still loading
1658
- const existingMatch = this.getMatch(matchId);
1659
- const previousMatch = this.state.matches.find((d) => d.routeId === route.id);
1660
- const cause = previousMatch ? 'stay' : 'enter';
1661
- let match;
1662
- if (existingMatch) {
1663
- match = {
1664
- ...existingMatch,
1665
- cause,
1666
- params: previousMatch
1667
- ? replaceEqualDeep(previousMatch.params, routeParams)
1668
- : routeParams,
1669
- _strictParams: usedParams,
1670
- search: previousMatch
1671
- ? replaceEqualDeep(previousMatch.search, preMatchSearch)
1672
- : replaceEqualDeep(existingMatch.search, preMatchSearch),
1673
- _strictSearch: strictMatchSearch,
1674
- };
1675
- }
1676
- else {
1677
- const status = route.options.loader ||
1678
- route.options.beforeLoad ||
1679
- route.lazyFn ||
1680
- routeNeedsPreload(route)
1681
- ? 'pending'
1682
- : 'success';
1683
- match = {
1684
- id: matchId,
1685
- index,
1686
- routeId: route.id,
1687
- params: previousMatch
1688
- ? replaceEqualDeep(previousMatch.params, routeParams)
1689
- : routeParams,
1690
- _strictParams: usedParams,
1691
- pathname: joinPaths([this.basepath, interpolatedPath]),
1692
- updatedAt: Date.now(),
1693
- search: previousMatch
1694
- ? replaceEqualDeep(previousMatch.search, preMatchSearch)
1695
- : preMatchSearch,
1696
- _strictSearch: strictMatchSearch,
1697
- searchError: undefined,
1698
- status,
1699
- isFetching: false,
1700
- error: undefined,
1701
- paramsError: parseErrors[index],
1702
- __routeContext: {},
1703
- __beforeLoadContext: {},
1704
- context: {},
1705
- abortController: new AbortController(),
1706
- fetchCount: 0,
1707
- cause,
1708
- loaderDeps: previousMatch
1709
- ? replaceEqualDeep(previousMatch.loaderDeps, loaderDeps)
1710
- : loaderDeps,
1711
- invalid: false,
1712
- preload: false,
1713
- links: undefined,
1714
- scripts: undefined,
1715
- headScripts: undefined,
1716
- meta: undefined,
1717
- staticData: route.options.staticData || {},
1718
- loadPromise: createControlledPromise(),
1719
- fullPath: route.fullPath,
1720
- };
1721
- }
1722
- if (!opts?.preload) {
1723
- // If we have a global not found, mark the right match as global not found
1724
- match.globalNotFound = globalNotFoundRouteId === route.id;
1725
- }
1726
- // update the searchError if there is one
1727
- match.searchError = searchError;
1728
- const parentContext = getParentContext(parentMatch);
1729
- match.context = {
1730
- ...parentContext,
1731
- ...match.__routeContext,
1732
- ...match.__beforeLoadContext,
1733
- };
1734
- matches.push(match);
1735
- });
1736
- matches.forEach((match, index) => {
1737
- const route = this.looseRoutesById[match.routeId];
1738
- const existingMatch = this.getMatch(match.id);
1739
- // only execute `context` if we are not just building a location
1740
- if (!existingMatch && opts?._buildLocation !== true) {
1741
- const parentMatch = matches[index - 1];
1742
- const parentContext = getParentContext(parentMatch);
1743
- // Update the match's context
1744
- const contextFnContext = {
1745
- deps: match.loaderDeps,
1746
- params: match.params,
1747
- context: parentContext,
1748
- location: next,
1749
- navigate: (opts) => this.navigate({ ...opts, _fromLocation: next }),
1750
- buildLocation: this.buildLocation,
1751
- cause: match.cause,
1752
- abortController: match.abortController,
1753
- preload: !!match.preload,
1754
- matches,
1755
- };
1756
- // Get the route context
1757
- match.__routeContext = route.options.context?.(contextFnContext) ?? {};
1758
- match.context = {
1759
- ...parentContext,
1760
- ...match.__routeContext,
1761
- ...match.__beforeLoadContext,
1762
- };
1763
- }
1764
- // If it's already a success, update headers and head content
1765
- // These may get updated again if the match is refreshed
1766
- // due to being stale
1767
- if (match.status === 'success') {
1768
- match.headers = route.options.headers?.({
1769
- loaderData: match.loaderData,
1770
- });
1771
- const assetContext = {
1772
- matches,
1773
- match,
1774
- params: match.params,
1775
- loaderData: match.loaderData,
1776
- };
1777
- const headFnContent = route.options.head?.(assetContext);
1778
- match.links = headFnContent?.links;
1779
- match.headScripts = headFnContent?.scripts;
1780
- match.meta = headFnContent?.meta;
1781
- match.scripts = route.options.scripts?.(assetContext);
1782
- }
1783
- });
1784
- return matches;
1785
- }
1786
- }
1787
- // A function that takes an import() argument which is a function and returns a new function that will
1788
- // proxy arguments from the caller to the imported function, retaining all type
1789
- // information along the way
1790
- export function lazyFn(fn, key) {
1791
- return async (...args) => {
1792
- const imported = await fn();
1793
- return imported[key || 'default'](...args);
1794
- };
1795
- }
1796
- export class SearchParamError extends Error {
1797
- }
1798
- export class PathParamError extends Error {
1799
- }
1800
- export function getInitialRouterState(location) {
1801
- return {
1802
- loadedAt: 0,
1803
- isLoading: false,
1804
- isTransitioning: false,
1805
- status: 'idle',
1806
- resolvedLocation: undefined,
1807
- location,
1808
- matches: [],
1809
- pendingMatches: [],
1810
- cachedMatches: [],
1811
- statusCode: 200,
1812
- };
1813
9
  }
1814
10
  //# sourceMappingURL=router.js.map