@tanstack/router-core 1.131.19 → 1.131.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,667 @@
1
+ import { batch } from "@tanstack/store";
2
+ import invariant from "tiny-invariant";
3
+ import { isPromise, createControlledPromise } from "./utils.js";
4
+ import { isNotFound } from "./not-found.js";
5
+ import { rootRouteId } from "./root.js";
6
+ import { isRedirect } from "./redirect.js";
7
+ const triggerOnReady = (inner) => {
8
+ var _a;
9
+ if (!inner.rendered) {
10
+ inner.rendered = true;
11
+ return (_a = inner.onReady) == null ? void 0 : _a.call(inner);
12
+ }
13
+ };
14
+ const resolvePreload = (inner, matchId) => {
15
+ return !!(inner.preload && !inner.router.state.matches.some((d) => d.id === matchId));
16
+ };
17
+ const _handleNotFound = (inner, err) => {
18
+ var _a;
19
+ const routeCursor = inner.router.routesById[err.routeId ?? ""] ?? inner.router.routeTree;
20
+ if (!routeCursor.options.notFoundComponent && ((_a = inner.router.options) == null ? void 0 : _a.defaultNotFoundComponent)) {
21
+ routeCursor.options.notFoundComponent = inner.router.options.defaultNotFoundComponent;
22
+ }
23
+ invariant(
24
+ routeCursor.options.notFoundComponent,
25
+ "No notFoundComponent found. Please set a notFoundComponent on your route or provide a defaultNotFoundComponent to the router."
26
+ );
27
+ const matchForRoute = inner.matches.find((m) => m.routeId === routeCursor.id);
28
+ invariant(matchForRoute, "Could not find match for route: " + routeCursor.id);
29
+ inner.updateMatch(matchForRoute.id, (prev) => ({
30
+ ...prev,
31
+ status: "notFound",
32
+ error: err,
33
+ isFetching: false
34
+ }));
35
+ if (err.routerCode === "BEFORE_LOAD" && routeCursor.parentRoute) {
36
+ err.routeId = routeCursor.parentRoute.id;
37
+ _handleNotFound(inner, err);
38
+ }
39
+ };
40
+ const handleRedirectAndNotFound = (inner, match, err) => {
41
+ var _a, _b, _c;
42
+ if (!isRedirect(err) && !isNotFound(err)) return;
43
+ if (isRedirect(err) && err.redirectHandled && !err.options.reloadDocument) {
44
+ throw err;
45
+ }
46
+ if (match) {
47
+ (_a = match._nonReactive.beforeLoadPromise) == null ? void 0 : _a.resolve();
48
+ (_b = match._nonReactive.loaderPromise) == null ? void 0 : _b.resolve();
49
+ match._nonReactive.beforeLoadPromise = void 0;
50
+ match._nonReactive.loaderPromise = void 0;
51
+ const status = isRedirect(err) ? "redirected" : "notFound";
52
+ inner.updateMatch(match.id, (prev) => ({
53
+ ...prev,
54
+ status,
55
+ isFetching: false,
56
+ error: err
57
+ }));
58
+ if (isNotFound(err) && !err.routeId) {
59
+ err.routeId = match.routeId;
60
+ }
61
+ (_c = match._nonReactive.loadPromise) == null ? void 0 : _c.resolve();
62
+ }
63
+ if (isRedirect(err)) {
64
+ inner.rendered = true;
65
+ err.options._fromLocation = inner.location;
66
+ err.redirectHandled = true;
67
+ err = inner.router.resolveRedirect(err);
68
+ throw err;
69
+ } else {
70
+ _handleNotFound(inner, err);
71
+ throw err;
72
+ }
73
+ };
74
+ const shouldSkipLoader = (inner, matchId) => {
75
+ const match = inner.router.getMatch(matchId);
76
+ if (!inner.router.isServer && match._nonReactive.dehydrated) {
77
+ return true;
78
+ }
79
+ if (inner.router.isServer) {
80
+ if (match.ssr === false) {
81
+ return true;
82
+ }
83
+ }
84
+ return false;
85
+ };
86
+ const handleSerialError = (inner, index, err, routerCode) => {
87
+ var _a, _b;
88
+ const { id: matchId, routeId } = inner.matches[index];
89
+ const route = inner.router.looseRoutesById[routeId];
90
+ if (err instanceof Promise) {
91
+ throw err;
92
+ }
93
+ err.routerCode = routerCode;
94
+ inner.firstBadMatchIndex ?? (inner.firstBadMatchIndex = index);
95
+ handleRedirectAndNotFound(inner, inner.router.getMatch(matchId), err);
96
+ try {
97
+ (_b = (_a = route.options).onError) == null ? void 0 : _b.call(_a, err);
98
+ } catch (errorHandlerErr) {
99
+ err = errorHandlerErr;
100
+ handleRedirectAndNotFound(inner, inner.router.getMatch(matchId), err);
101
+ }
102
+ inner.updateMatch(matchId, (prev) => {
103
+ var _a2, _b2;
104
+ (_a2 = prev._nonReactive.beforeLoadPromise) == null ? void 0 : _a2.resolve();
105
+ prev._nonReactive.beforeLoadPromise = void 0;
106
+ (_b2 = prev._nonReactive.loadPromise) == null ? void 0 : _b2.resolve();
107
+ return {
108
+ ...prev,
109
+ error: err,
110
+ status: "error",
111
+ isFetching: false,
112
+ updatedAt: Date.now(),
113
+ abortController: new AbortController()
114
+ };
115
+ });
116
+ };
117
+ const isBeforeLoadSsr = (inner, matchId, index, route) => {
118
+ var _a;
119
+ const existingMatch = inner.router.getMatch(matchId);
120
+ const parentMatchId = (_a = inner.matches[index - 1]) == null ? void 0 : _a.id;
121
+ const parentMatch = parentMatchId ? inner.router.getMatch(parentMatchId) : void 0;
122
+ if (inner.router.isShell()) {
123
+ existingMatch.ssr = matchId === rootRouteId;
124
+ return;
125
+ }
126
+ if ((parentMatch == null ? void 0 : parentMatch.ssr) === false) {
127
+ existingMatch.ssr = false;
128
+ return;
129
+ }
130
+ const parentOverride = (tempSsr2) => {
131
+ if (tempSsr2 === true && (parentMatch == null ? void 0 : parentMatch.ssr) === "data-only") {
132
+ return "data-only";
133
+ }
134
+ return tempSsr2;
135
+ };
136
+ const defaultSsr = inner.router.options.defaultSsr ?? true;
137
+ if (route.options.ssr === void 0) {
138
+ existingMatch.ssr = parentOverride(defaultSsr);
139
+ return;
140
+ }
141
+ if (typeof route.options.ssr !== "function") {
142
+ existingMatch.ssr = parentOverride(route.options.ssr);
143
+ return;
144
+ }
145
+ const { search, params } = existingMatch;
146
+ const ssrFnContext = {
147
+ search: makeMaybe(search, existingMatch.searchError),
148
+ params: makeMaybe(params, existingMatch.paramsError),
149
+ location: inner.location,
150
+ matches: inner.matches.map((match) => ({
151
+ index: match.index,
152
+ pathname: match.pathname,
153
+ fullPath: match.fullPath,
154
+ staticData: match.staticData,
155
+ id: match.id,
156
+ routeId: match.routeId,
157
+ search: makeMaybe(match.search, match.searchError),
158
+ params: makeMaybe(match.params, match.paramsError),
159
+ ssr: match.ssr
160
+ }))
161
+ };
162
+ const tempSsr = route.options.ssr(ssrFnContext);
163
+ if (isPromise(tempSsr)) {
164
+ return tempSsr.then((ssr) => {
165
+ existingMatch.ssr = parentOverride(ssr ?? defaultSsr);
166
+ });
167
+ }
168
+ existingMatch.ssr = parentOverride(tempSsr ?? defaultSsr);
169
+ return;
170
+ };
171
+ const setupPendingTimeout = (inner, matchId, route, match) => {
172
+ var _a;
173
+ if (match._nonReactive.pendingTimeout !== void 0) return;
174
+ const pendingMs = route.options.pendingMs ?? inner.router.options.defaultPendingMs;
175
+ const shouldPending = !!(inner.onReady && !inner.router.isServer && !resolvePreload(inner, matchId) && (route.options.loader || route.options.beforeLoad || routeNeedsPreload(route)) && typeof pendingMs === "number" && pendingMs !== Infinity && (route.options.pendingComponent ?? ((_a = inner.router.options) == null ? void 0 : _a.defaultPendingComponent)));
176
+ if (shouldPending) {
177
+ const pendingTimeout = setTimeout(() => {
178
+ triggerOnReady(inner);
179
+ }, pendingMs);
180
+ match._nonReactive.pendingTimeout = pendingTimeout;
181
+ }
182
+ };
183
+ const shouldExecuteBeforeLoad = (inner, matchId, route) => {
184
+ const existingMatch = inner.router.getMatch(matchId);
185
+ if (!existingMatch._nonReactive.beforeLoadPromise && !existingMatch._nonReactive.loaderPromise)
186
+ return true;
187
+ setupPendingTimeout(inner, matchId, route, existingMatch);
188
+ const then = () => {
189
+ let result = true;
190
+ const match = inner.router.getMatch(matchId);
191
+ if (match.status === "error") {
192
+ result = true;
193
+ } else if (match.preload && (match.status === "redirected" || match.status === "notFound")) {
194
+ handleRedirectAndNotFound(inner, match, match.error);
195
+ }
196
+ return result;
197
+ };
198
+ return existingMatch._nonReactive.beforeLoadPromise ? existingMatch._nonReactive.beforeLoadPromise.then(then) : then();
199
+ };
200
+ const executeBeforeLoad = (inner, matchId, index, route) => {
201
+ var _a;
202
+ const match = inner.router.getMatch(matchId);
203
+ const prevLoadPromise = match._nonReactive.loadPromise;
204
+ match._nonReactive.loadPromise = createControlledPromise(() => {
205
+ prevLoadPromise == null ? void 0 : prevLoadPromise.resolve();
206
+ });
207
+ const { paramsError, searchError } = match;
208
+ if (paramsError) {
209
+ handleSerialError(inner, index, paramsError, "PARSE_PARAMS");
210
+ }
211
+ if (searchError) {
212
+ handleSerialError(inner, index, searchError, "VALIDATE_SEARCH");
213
+ }
214
+ setupPendingTimeout(inner, matchId, route, match);
215
+ const abortController = new AbortController();
216
+ const parentMatchId = (_a = inner.matches[index - 1]) == null ? void 0 : _a.id;
217
+ const parentMatch = parentMatchId ? inner.router.getMatch(parentMatchId) : void 0;
218
+ const parentMatchContext = (parentMatch == null ? void 0 : parentMatch.context) ?? inner.router.options.context ?? void 0;
219
+ const context = { ...parentMatchContext, ...match.__routeContext };
220
+ let isPending = false;
221
+ const pending = () => {
222
+ if (isPending) return;
223
+ isPending = true;
224
+ inner.updateMatch(matchId, (prev) => ({
225
+ ...prev,
226
+ isFetching: "beforeLoad",
227
+ fetchCount: prev.fetchCount + 1,
228
+ abortController,
229
+ context
230
+ }));
231
+ };
232
+ const resolve = () => {
233
+ var _a2;
234
+ (_a2 = match._nonReactive.beforeLoadPromise) == null ? void 0 : _a2.resolve();
235
+ match._nonReactive.beforeLoadPromise = void 0;
236
+ inner.updateMatch(matchId, (prev) => ({
237
+ ...prev,
238
+ isFetching: false
239
+ }));
240
+ };
241
+ if (!route.options.beforeLoad) {
242
+ batch(() => {
243
+ pending();
244
+ resolve();
245
+ });
246
+ return;
247
+ }
248
+ match._nonReactive.beforeLoadPromise = createControlledPromise();
249
+ const { search, params, cause } = match;
250
+ const preload = resolvePreload(inner, matchId);
251
+ const beforeLoadFnContext = {
252
+ search,
253
+ abortController,
254
+ params,
255
+ preload,
256
+ context,
257
+ location: inner.location,
258
+ navigate: (opts) => inner.router.navigate({
259
+ ...opts,
260
+ _fromLocation: inner.location
261
+ }),
262
+ buildLocation: inner.router.buildLocation,
263
+ cause: preload ? "preload" : cause,
264
+ matches: inner.matches
265
+ };
266
+ const updateContext = (beforeLoadContext2) => {
267
+ if (beforeLoadContext2 === void 0) {
268
+ batch(() => {
269
+ pending();
270
+ resolve();
271
+ });
272
+ return;
273
+ }
274
+ if (isRedirect(beforeLoadContext2) || isNotFound(beforeLoadContext2)) {
275
+ pending();
276
+ handleSerialError(inner, index, beforeLoadContext2, "BEFORE_LOAD");
277
+ }
278
+ batch(() => {
279
+ pending();
280
+ inner.updateMatch(matchId, (prev) => ({
281
+ ...prev,
282
+ __beforeLoadContext: beforeLoadContext2,
283
+ context: {
284
+ ...prev.context,
285
+ ...beforeLoadContext2
286
+ }
287
+ }));
288
+ resolve();
289
+ });
290
+ };
291
+ let beforeLoadContext;
292
+ try {
293
+ beforeLoadContext = route.options.beforeLoad(beforeLoadFnContext);
294
+ if (isPromise(beforeLoadContext)) {
295
+ pending();
296
+ return beforeLoadContext.catch((err) => {
297
+ handleSerialError(inner, index, err, "BEFORE_LOAD");
298
+ }).then(updateContext);
299
+ }
300
+ } catch (err) {
301
+ pending();
302
+ handleSerialError(inner, index, err, "BEFORE_LOAD");
303
+ }
304
+ updateContext(beforeLoadContext);
305
+ return;
306
+ };
307
+ const handleBeforeLoad = (inner, index) => {
308
+ const { id: matchId, routeId } = inner.matches[index];
309
+ const route = inner.router.looseRoutesById[routeId];
310
+ const serverSsr = () => {
311
+ if (inner.router.isServer) {
312
+ const maybePromise = isBeforeLoadSsr(inner, matchId, index, route);
313
+ if (isPromise(maybePromise)) return maybePromise.then(queueExecution);
314
+ }
315
+ return queueExecution();
316
+ };
317
+ const queueExecution = () => {
318
+ if (shouldSkipLoader(inner, matchId)) return;
319
+ const shouldExecuteBeforeLoadResult = shouldExecuteBeforeLoad(
320
+ inner,
321
+ matchId,
322
+ route
323
+ );
324
+ return isPromise(shouldExecuteBeforeLoadResult) ? shouldExecuteBeforeLoadResult.then(execute) : execute(shouldExecuteBeforeLoadResult);
325
+ };
326
+ const execute = (shouldExec) => {
327
+ if (shouldExec) {
328
+ return executeBeforeLoad(inner, matchId, index, route);
329
+ }
330
+ return;
331
+ };
332
+ return serverSsr();
333
+ };
334
+ const executeHead = (inner, matchId, route) => {
335
+ var _a, _b, _c, _d, _e, _f;
336
+ const match = inner.router.getMatch(matchId);
337
+ if (!match) {
338
+ return;
339
+ }
340
+ if (!route.options.head && !route.options.scripts && !route.options.headers) {
341
+ return;
342
+ }
343
+ const assetContext = {
344
+ matches: inner.matches,
345
+ match,
346
+ params: match.params,
347
+ loaderData: match.loaderData
348
+ };
349
+ return Promise.all([
350
+ (_b = (_a = route.options).head) == null ? void 0 : _b.call(_a, assetContext),
351
+ (_d = (_c = route.options).scripts) == null ? void 0 : _d.call(_c, assetContext),
352
+ (_f = (_e = route.options).headers) == null ? void 0 : _f.call(_e, assetContext)
353
+ ]).then(([headFnContent, scripts, headers]) => {
354
+ const meta = headFnContent == null ? void 0 : headFnContent.meta;
355
+ const links = headFnContent == null ? void 0 : headFnContent.links;
356
+ const headScripts = headFnContent == null ? void 0 : headFnContent.scripts;
357
+ const styles = headFnContent == null ? void 0 : headFnContent.styles;
358
+ return {
359
+ meta,
360
+ links,
361
+ headScripts,
362
+ headers,
363
+ scripts,
364
+ styles
365
+ };
366
+ });
367
+ };
368
+ const getLoaderContext = (inner, matchId, index, route) => {
369
+ const parentMatchPromise = inner.matchPromises[index - 1];
370
+ const { params, loaderDeps, abortController, context, cause } = inner.router.getMatch(matchId);
371
+ const preload = resolvePreload(inner, matchId);
372
+ return {
373
+ params,
374
+ deps: loaderDeps,
375
+ preload: !!preload,
376
+ parentMatchPromise,
377
+ abortController,
378
+ context,
379
+ location: inner.location,
380
+ navigate: (opts) => inner.router.navigate({
381
+ ...opts,
382
+ _fromLocation: inner.location
383
+ }),
384
+ cause: preload ? "preload" : cause,
385
+ route
386
+ };
387
+ };
388
+ const runLoader = async (inner, matchId, index, route) => {
389
+ var _a, _b, _c, _d;
390
+ try {
391
+ const match = inner.router.getMatch(matchId);
392
+ try {
393
+ if (!inner.router.isServer || match.ssr === true) {
394
+ loadRouteChunk(route);
395
+ }
396
+ const loaderResult = (_b = (_a = route.options).loader) == null ? void 0 : _b.call(
397
+ _a,
398
+ getLoaderContext(inner, matchId, index, route)
399
+ );
400
+ const loaderResultIsPromise = route.options.loader && isPromise(loaderResult);
401
+ const willLoadSomething = !!(loaderResultIsPromise || route._lazyPromise || route._componentsPromise || route.options.head || route.options.scripts || route.options.headers || match._nonReactive.minPendingPromise);
402
+ if (willLoadSomething) {
403
+ inner.updateMatch(matchId, (prev) => ({
404
+ ...prev,
405
+ isFetching: "loader"
406
+ }));
407
+ }
408
+ if (route.options.loader) {
409
+ const loaderData = loaderResultIsPromise ? await loaderResult : loaderResult;
410
+ handleRedirectAndNotFound(
411
+ inner,
412
+ inner.router.getMatch(matchId),
413
+ loaderData
414
+ );
415
+ if (loaderData !== void 0) {
416
+ inner.updateMatch(matchId, (prev) => ({
417
+ ...prev,
418
+ loaderData
419
+ }));
420
+ }
421
+ }
422
+ if (route._lazyPromise) await route._lazyPromise;
423
+ const headResult = executeHead(inner, matchId, route);
424
+ const head = headResult ? await headResult : void 0;
425
+ const pendingPromise = match._nonReactive.minPendingPromise;
426
+ if (pendingPromise) await pendingPromise;
427
+ if (route._componentsPromise) await route._componentsPromise;
428
+ inner.updateMatch(matchId, (prev) => ({
429
+ ...prev,
430
+ error: void 0,
431
+ status: "success",
432
+ isFetching: false,
433
+ updatedAt: Date.now(),
434
+ ...head
435
+ }));
436
+ } catch (e) {
437
+ let error = e;
438
+ const pendingPromise = match._nonReactive.minPendingPromise;
439
+ if (pendingPromise) await pendingPromise;
440
+ handleRedirectAndNotFound(inner, inner.router.getMatch(matchId), e);
441
+ try {
442
+ (_d = (_c = route.options).onError) == null ? void 0 : _d.call(_c, e);
443
+ } catch (onErrorError) {
444
+ error = onErrorError;
445
+ handleRedirectAndNotFound(
446
+ inner,
447
+ inner.router.getMatch(matchId),
448
+ onErrorError
449
+ );
450
+ }
451
+ const headResult = executeHead(inner, matchId, route);
452
+ const head = headResult ? await headResult : void 0;
453
+ inner.updateMatch(matchId, (prev) => ({
454
+ ...prev,
455
+ error,
456
+ status: "error",
457
+ isFetching: false,
458
+ ...head
459
+ }));
460
+ }
461
+ } catch (err) {
462
+ const match = inner.router.getMatch(matchId);
463
+ if (match) {
464
+ const headResult = executeHead(inner, matchId, route);
465
+ if (headResult) {
466
+ const head = await headResult;
467
+ inner.updateMatch(matchId, (prev) => ({
468
+ ...prev,
469
+ ...head
470
+ }));
471
+ }
472
+ match._nonReactive.loaderPromise = void 0;
473
+ }
474
+ handleRedirectAndNotFound(inner, match, err);
475
+ }
476
+ };
477
+ const loadRouteMatch = async (inner, index) => {
478
+ var _a, _b;
479
+ const { id: matchId, routeId } = inner.matches[index];
480
+ let loaderShouldRunAsync = false;
481
+ let loaderIsRunningAsync = false;
482
+ const route = inner.router.looseRoutesById[routeId];
483
+ if (shouldSkipLoader(inner, matchId)) {
484
+ if (inner.router.isServer) {
485
+ const headResult = executeHead(inner, matchId, route);
486
+ if (headResult) {
487
+ const head = await headResult;
488
+ inner.updateMatch(matchId, (prev) => ({
489
+ ...prev,
490
+ ...head
491
+ }));
492
+ }
493
+ return inner.router.getMatch(matchId);
494
+ }
495
+ } else {
496
+ const prevMatch = inner.router.getMatch(matchId);
497
+ if (prevMatch._nonReactive.loaderPromise) {
498
+ if (prevMatch.status === "success" && !inner.sync && !prevMatch.preload) {
499
+ return prevMatch;
500
+ }
501
+ await prevMatch._nonReactive.loaderPromise;
502
+ const match2 = inner.router.getMatch(matchId);
503
+ if (match2.error) {
504
+ handleRedirectAndNotFound(inner, match2, match2.error);
505
+ }
506
+ } else {
507
+ const age = Date.now() - prevMatch.updatedAt;
508
+ const preload = resolvePreload(inner, matchId);
509
+ const staleAge = preload ? route.options.preloadStaleTime ?? inner.router.options.defaultPreloadStaleTime ?? 3e4 : route.options.staleTime ?? inner.router.options.defaultStaleTime ?? 0;
510
+ const shouldReloadOption = route.options.shouldReload;
511
+ const shouldReload = typeof shouldReloadOption === "function" ? shouldReloadOption(getLoaderContext(inner, matchId, index, route)) : shouldReloadOption;
512
+ const nextPreload = !!preload && !inner.router.state.matches.some((d) => d.id === matchId);
513
+ const match2 = inner.router.getMatch(matchId);
514
+ match2._nonReactive.loaderPromise = createControlledPromise();
515
+ if (nextPreload !== match2.preload) {
516
+ inner.updateMatch(matchId, (prev) => ({
517
+ ...prev,
518
+ preload: nextPreload
519
+ }));
520
+ }
521
+ const { status, invalid } = match2;
522
+ loaderShouldRunAsync = status === "success" && (invalid || (shouldReload ?? age > staleAge));
523
+ if (preload && route.options.preload === false) ;
524
+ else if (loaderShouldRunAsync && !inner.sync) {
525
+ loaderIsRunningAsync = true;
526
+ (async () => {
527
+ var _a2, _b2;
528
+ try {
529
+ await runLoader(inner, matchId, index, route);
530
+ const match3 = inner.router.getMatch(matchId);
531
+ (_a2 = match3._nonReactive.loaderPromise) == null ? void 0 : _a2.resolve();
532
+ (_b2 = match3._nonReactive.loadPromise) == null ? void 0 : _b2.resolve();
533
+ match3._nonReactive.loaderPromise = void 0;
534
+ } catch (err) {
535
+ if (isRedirect(err)) {
536
+ await inner.router.navigate(err.options);
537
+ }
538
+ }
539
+ })();
540
+ } else if (status !== "success" || loaderShouldRunAsync && inner.sync) {
541
+ await runLoader(inner, matchId, index, route);
542
+ } else {
543
+ const headResult = executeHead(inner, matchId, route);
544
+ if (headResult) {
545
+ const head = await headResult;
546
+ inner.updateMatch(matchId, (prev) => ({
547
+ ...prev,
548
+ ...head
549
+ }));
550
+ }
551
+ }
552
+ }
553
+ }
554
+ const match = inner.router.getMatch(matchId);
555
+ if (!loaderIsRunningAsync) {
556
+ (_a = match._nonReactive.loaderPromise) == null ? void 0 : _a.resolve();
557
+ (_b = match._nonReactive.loadPromise) == null ? void 0 : _b.resolve();
558
+ }
559
+ clearTimeout(match._nonReactive.pendingTimeout);
560
+ match._nonReactive.pendingTimeout = void 0;
561
+ if (!loaderIsRunningAsync) match._nonReactive.loaderPromise = void 0;
562
+ match._nonReactive.dehydrated = void 0;
563
+ const nextIsFetching = loaderIsRunningAsync ? match.isFetching : false;
564
+ if (nextIsFetching !== match.isFetching || match.invalid !== false) {
565
+ inner.updateMatch(matchId, (prev) => ({
566
+ ...prev,
567
+ isFetching: nextIsFetching,
568
+ invalid: false
569
+ }));
570
+ return inner.router.getMatch(matchId);
571
+ } else {
572
+ return match;
573
+ }
574
+ };
575
+ async function loadMatches(arg) {
576
+ const inner = Object.assign(arg, {
577
+ matchPromises: []
578
+ });
579
+ if (!inner.router.isServer && inner.router.state.matches.some((d) => d._forcePending)) {
580
+ triggerOnReady(inner);
581
+ }
582
+ try {
583
+ for (let i = 0; i < inner.matches.length; i++) {
584
+ const beforeLoad = handleBeforeLoad(inner, i);
585
+ if (isPromise(beforeLoad)) await beforeLoad;
586
+ }
587
+ const max = inner.firstBadMatchIndex ?? inner.matches.length;
588
+ for (let i = 0; i < max; i++) {
589
+ inner.matchPromises.push(loadRouteMatch(inner, i));
590
+ }
591
+ await Promise.all(inner.matchPromises);
592
+ const readyPromise = triggerOnReady(inner);
593
+ if (isPromise(readyPromise)) await readyPromise;
594
+ } catch (err) {
595
+ if (isNotFound(err) && !inner.preload) {
596
+ const readyPromise = triggerOnReady(inner);
597
+ if (isPromise(readyPromise)) await readyPromise;
598
+ throw err;
599
+ }
600
+ if (isRedirect(err)) {
601
+ throw err;
602
+ }
603
+ }
604
+ return inner.matches;
605
+ }
606
+ async function loadRouteChunk(route) {
607
+ if (!route._lazyLoaded && route._lazyPromise === void 0) {
608
+ if (route.lazyFn) {
609
+ route._lazyPromise = route.lazyFn().then((lazyRoute) => {
610
+ const { id: _id, ...options } = lazyRoute.options;
611
+ Object.assign(route.options, options);
612
+ route._lazyLoaded = true;
613
+ route._lazyPromise = void 0;
614
+ });
615
+ } else {
616
+ route._lazyLoaded = true;
617
+ }
618
+ }
619
+ if (!route._componentsLoaded && route._componentsPromise === void 0) {
620
+ const loadComponents = () => {
621
+ var _a;
622
+ const preloads = [];
623
+ for (const type of componentTypes) {
624
+ const preload = (_a = route.options[type]) == null ? void 0 : _a.preload;
625
+ if (preload) preloads.push(preload());
626
+ }
627
+ if (preloads.length)
628
+ return Promise.all(preloads).then(() => {
629
+ route._componentsLoaded = true;
630
+ route._componentsPromise = void 0;
631
+ });
632
+ route._componentsLoaded = true;
633
+ route._componentsPromise = void 0;
634
+ return;
635
+ };
636
+ route._componentsPromise = route._lazyPromise ? route._lazyPromise.then(loadComponents) : loadComponents();
637
+ }
638
+ return route._componentsPromise;
639
+ }
640
+ function makeMaybe(value, error) {
641
+ if (error) {
642
+ return { status: "error", error };
643
+ }
644
+ return { status: "success", value };
645
+ }
646
+ function routeNeedsPreload(route) {
647
+ var _a;
648
+ for (const componentType of componentTypes) {
649
+ if ((_a = route.options[componentType]) == null ? void 0 : _a.preload) {
650
+ return true;
651
+ }
652
+ }
653
+ return false;
654
+ }
655
+ const componentTypes = [
656
+ "component",
657
+ "errorComponent",
658
+ "pendingComponent",
659
+ "notFoundComponent"
660
+ ];
661
+ export {
662
+ componentTypes,
663
+ loadMatches,
664
+ loadRouteChunk,
665
+ routeNeedsPreload
666
+ };
667
+ //# sourceMappingURL=load-matches.js.map