@tanstack/router-core 1.131.19 → 1.131.21

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