@tanstack/react-router 1.32.16 → 1.32.17

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.
Files changed (85) hide show
  1. package/dist/cjs/Matches.cjs +7 -36
  2. package/dist/cjs/Matches.cjs.map +1 -1
  3. package/dist/cjs/Matches.d.cts +2 -11
  4. package/dist/cjs/fileRoute.cjs +7 -5
  5. package/dist/cjs/fileRoute.cjs.map +1 -1
  6. package/dist/cjs/index.cjs +8 -4
  7. package/dist/cjs/index.cjs.map +1 -1
  8. package/dist/cjs/index.d.cts +6 -2
  9. package/dist/cjs/link.cjs +2 -2
  10. package/dist/cjs/link.cjs.map +1 -1
  11. package/dist/cjs/root.cjs +5 -0
  12. package/dist/cjs/root.cjs.map +1 -0
  13. package/dist/cjs/root.d.cts +2 -0
  14. package/dist/cjs/route.cjs +19 -18
  15. package/dist/cjs/route.cjs.map +1 -1
  16. package/dist/cjs/route.d.cts +1 -2
  17. package/dist/cjs/router.cjs +56 -52
  18. package/dist/cjs/router.cjs.map +1 -1
  19. package/dist/cjs/useLoaderData.cjs +13 -0
  20. package/dist/cjs/useLoaderData.cjs.map +1 -0
  21. package/dist/cjs/useLoaderData.d.cts +9 -0
  22. package/dist/cjs/useLoaderDeps.cjs +13 -0
  23. package/dist/cjs/useLoaderDeps.cjs.map +1 -0
  24. package/dist/cjs/useLoaderDeps.d.cts +9 -0
  25. package/dist/cjs/useMatch.cjs +41 -0
  26. package/dist/cjs/useMatch.cjs.map +1 -0
  27. package/dist/cjs/useMatch.d.cts +9 -0
  28. package/dist/cjs/useNavigate.cjs +2 -2
  29. package/dist/cjs/useNavigate.cjs.map +1 -1
  30. package/dist/cjs/useParams.cjs +2 -2
  31. package/dist/cjs/useParams.cjs.map +1 -1
  32. package/dist/cjs/useRouteContext.cjs +2 -2
  33. package/dist/cjs/useRouteContext.cjs.map +1 -1
  34. package/dist/cjs/useSearch.cjs +2 -2
  35. package/dist/cjs/useSearch.cjs.map +1 -1
  36. package/dist/esm/Matches.d.ts +2 -11
  37. package/dist/esm/Matches.js +7 -36
  38. package/dist/esm/Matches.js.map +1 -1
  39. package/dist/esm/fileRoute.js +3 -1
  40. package/dist/esm/fileRoute.js.map +1 -1
  41. package/dist/esm/index.d.ts +6 -2
  42. package/dist/esm/index.js +6 -2
  43. package/dist/esm/index.js.map +1 -1
  44. package/dist/esm/link.js +1 -1
  45. package/dist/esm/link.js.map +1 -1
  46. package/dist/esm/root.d.ts +2 -0
  47. package/dist/esm/root.js +5 -0
  48. package/dist/esm/root.js.map +1 -0
  49. package/dist/esm/route.d.ts +1 -2
  50. package/dist/esm/route.js +4 -3
  51. package/dist/esm/route.js.map +1 -1
  52. package/dist/esm/router.js +7 -3
  53. package/dist/esm/router.js.map +1 -1
  54. package/dist/esm/useLoaderData.d.ts +9 -0
  55. package/dist/esm/useLoaderData.js +13 -0
  56. package/dist/esm/useLoaderData.js.map +1 -0
  57. package/dist/esm/useLoaderDeps.d.ts +9 -0
  58. package/dist/esm/useLoaderDeps.js +13 -0
  59. package/dist/esm/useLoaderDeps.js.map +1 -0
  60. package/dist/esm/useMatch.d.ts +9 -0
  61. package/dist/esm/useMatch.js +24 -0
  62. package/dist/esm/useMatch.js.map +1 -0
  63. package/dist/esm/useNavigate.js +1 -1
  64. package/dist/esm/useNavigate.js.map +1 -1
  65. package/dist/esm/useParams.js +1 -1
  66. package/dist/esm/useParams.js.map +1 -1
  67. package/dist/esm/useRouteContext.js +1 -1
  68. package/dist/esm/useRouteContext.js.map +1 -1
  69. package/dist/esm/useSearch.js +1 -1
  70. package/dist/esm/useSearch.js.map +1 -1
  71. package/package.json +1 -1
  72. package/src/Matches.tsx +20 -88
  73. package/src/fileRoute.ts +4 -2
  74. package/src/index.tsx +4 -5
  75. package/src/link.tsx +1 -1
  76. package/src/root.ts +2 -0
  77. package/src/route.ts +5 -3
  78. package/src/router.ts +7 -4
  79. package/src/useLoaderData.tsx +29 -0
  80. package/src/useLoaderDeps.tsx +29 -0
  81. package/src/useMatch.tsx +40 -0
  82. package/src/useNavigate.tsx +1 -1
  83. package/src/useParams.tsx +1 -1
  84. package/src/useRouteContext.ts +1 -1
  85. package/src/useSearch.tsx +1 -1
@@ -4,7 +4,7 @@ const history = require("@tanstack/history");
4
4
  const reactStore = require("@tanstack/react-store");
5
5
  const invariant = require("tiny-invariant");
6
6
  const warning = require("tiny-warning");
7
- const route = require("./route.cjs");
7
+ const root = require("./root.cjs");
8
8
  const searchParams = require("./searchParams.cjs");
9
9
  const utils = require("./utils.cjs");
10
10
  const RouterProvider = require("./RouterProvider.cjs");
@@ -220,13 +220,13 @@ class Router {
220
220
  };
221
221
  this.matchRoutes = (pathname, locationSearch, opts) => {
222
222
  let routeParams = {};
223
- const foundRoute = this.flatRoutes.find((route2) => {
223
+ const foundRoute = this.flatRoutes.find((route) => {
224
224
  const matchedParams = path.matchPathname(
225
225
  this.basepath,
226
226
  path.trimPathRight(pathname),
227
227
  {
228
- to: route2.fullPath,
229
- caseSensitive: route2.options.caseSensitive ?? this.options.caseSensitive,
228
+ to: route.fullPath,
229
+ caseSensitive: route.options.caseSensitive ?? this.options.caseSensitive,
230
230
  fuzzy: true
231
231
  }
232
232
  );
@@ -236,7 +236,7 @@ class Router {
236
236
  }
237
237
  return false;
238
238
  });
239
- let routeCursor = foundRoute || this.routesById[route.rootRouteId];
239
+ let routeCursor = foundRoute || this.routesById[root.rootRouteId];
240
240
  const matchedRoutes = [routeCursor];
241
241
  let isGlobalNotFound = false;
242
242
  if (
@@ -262,19 +262,19 @@ class Router {
262
262
  }
263
263
  if (this.options.notFoundMode !== "root") {
264
264
  for (let i = matchedRoutes.length - 1; i >= 0; i--) {
265
- const route2 = matchedRoutes[i];
266
- if (route2.children) {
267
- return route2.id;
265
+ const route = matchedRoutes[i];
266
+ if (route.children) {
267
+ return route.id;
268
268
  }
269
269
  }
270
270
  }
271
- return route.rootRouteId;
271
+ return root.rootRouteId;
272
272
  })();
273
- const parseErrors = matchedRoutes.map((route2) => {
273
+ const parseErrors = matchedRoutes.map((route) => {
274
274
  let parsedParamsError;
275
- if (route2.options.parseParams) {
275
+ if (route.options.parseParams) {
276
276
  try {
277
- const parsedParams = route2.options.parseParams(routeParams);
277
+ const parsedParams = route.options.parseParams(routeParams);
278
278
  Object.assign(routeParams, parsedParams);
279
279
  } catch (err) {
280
280
  parsedParamsError = new PathParamError(err.message, {
@@ -289,13 +289,13 @@ class Router {
289
289
  return;
290
290
  });
291
291
  const matches = [];
292
- matchedRoutes.forEach((route2, index) => {
292
+ matchedRoutes.forEach((route, index) => {
293
293
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
294
294
  const parentMatch = matches[index - 1];
295
295
  const [preMatchSearch, searchError] = (() => {
296
296
  const parentSearch = (parentMatch == null ? void 0 : parentMatch.search) ?? locationSearch;
297
297
  try {
298
- const validator = typeof route2.options.validateSearch === "object" ? route2.options.validateSearch.parse : route2.options.validateSearch;
298
+ const validator = typeof route.options.validateSearch === "object" ? route.options.validateSearch.parse : route.options.validateSearch;
299
299
  const search = (validator == null ? void 0 : validator(parentSearch)) ?? {};
300
300
  return [
301
301
  {
@@ -314,16 +314,16 @@ class Router {
314
314
  return [parentSearch, searchParamError];
315
315
  }
316
316
  })();
317
- const loaderDeps = ((_b = (_a = route2.options).loaderDeps) == null ? void 0 : _b.call(_a, {
317
+ const loaderDeps = ((_b = (_a = route.options).loaderDeps) == null ? void 0 : _b.call(_a, {
318
318
  search: preMatchSearch
319
319
  })) ?? "";
320
320
  const loaderDepsHash = loaderDeps ? JSON.stringify(loaderDeps) : "";
321
321
  const interpolatedPath = path.interpolatePath({
322
- path: route2.fullPath,
322
+ path: route.fullPath,
323
323
  params: routeParams
324
324
  });
325
325
  const matchId = path.interpolatePath({
326
- path: route2.id,
326
+ path: route.id,
327
327
  params: routeParams,
328
328
  leaveWildcards: true
329
329
  }) + loaderDepsHash;
@@ -337,14 +337,14 @@ class Router {
337
337
  params: routeParams
338
338
  };
339
339
  } else {
340
- const status = route2.options.loader || route2.options.beforeLoad ? "pending" : "success";
340
+ const status = route.options.loader || route.options.beforeLoad ? "pending" : "success";
341
341
  const loadPromise = utils.createControlledPromise();
342
342
  if (status === "success") {
343
343
  loadPromise.resolve();
344
344
  }
345
345
  match = {
346
346
  id: matchId,
347
- routeId: route2.id,
347
+ routeId: route.id,
348
348
  params: routeParams,
349
349
  pathname: path.joinPaths([this.basepath, interpolatedPath]),
350
350
  updatedAt: Date.now(),
@@ -364,23 +364,23 @@ class Router {
364
364
  loaderDeps,
365
365
  invalid: false,
366
366
  preload: false,
367
- links: (_d = (_c = route2.options).links) == null ? void 0 : _d.call(_c),
368
- scripts: (_f = (_e = route2.options).scripts) == null ? void 0 : _f.call(_e),
369
- staticData: route2.options.staticData || {}
367
+ links: (_d = (_c = route.options).links) == null ? void 0 : _d.call(_c),
368
+ scripts: (_f = (_e = route.options).scripts) == null ? void 0 : _f.call(_e),
369
+ staticData: route.options.staticData || {}
370
370
  };
371
371
  }
372
372
  if (match.status === "success") {
373
- match.meta = (_h = (_g = route2.options).meta) == null ? void 0 : _h.call(_g, {
373
+ match.meta = (_h = (_g = route.options).meta) == null ? void 0 : _h.call(_g, {
374
374
  matches,
375
375
  params: match.params,
376
376
  loaderData: match.loaderData
377
377
  });
378
- match.headers = (_j = (_i = route2.options).headers) == null ? void 0 : _j.call(_i, {
378
+ match.headers = (_j = (_i = route.options).headers) == null ? void 0 : _j.call(_i, {
379
379
  loaderData: match.loaderData
380
380
  });
381
381
  }
382
382
  if (!(opts == null ? void 0 : opts.preload)) {
383
- match.globalNotFound = globalNotFoundRouteId === route2.id;
383
+ match.globalNotFound = globalNotFoundRouteId === route.id;
384
384
  }
385
385
  match.search = utils.replaceEqualDeep(match.search, preMatchSearch);
386
386
  match.searchError = searchError;
@@ -743,11 +743,11 @@ class Router {
743
743
  isFetching: false,
744
744
  error: err
745
745
  }));
746
- rendered = true;
747
746
  if (!err.routeId) {
748
747
  err.routeId = match.routeId;
749
748
  }
750
749
  if (redirects.isRedirect(err)) {
750
+ rendered = true;
751
751
  err = this.resolveRedirect(err);
752
752
  throw err;
753
753
  } else if (notFound.isNotFound(err)) {
@@ -764,11 +764,11 @@ class Router {
764
764
  try {
765
765
  for (let [index, match] of matches.entries()) {
766
766
  const parentMatch = matches[index - 1];
767
- const route2 = this.looseRoutesById[match.routeId];
767
+ const route = this.looseRoutesById[match.routeId];
768
768
  const abortController = new AbortController();
769
769
  let loadPromise = match.loadPromise;
770
- const pendingMs = route2.options.pendingMs ?? this.options.defaultPendingMs;
771
- const shouldPending = !!(onReady && !this.isServer && !preload && (route2.options.loader || route2.options.beforeLoad) && typeof pendingMs === "number" && pendingMs !== Infinity && (route2.options.pendingComponent ?? this.options.defaultPendingComponent));
770
+ const pendingMs = route.options.pendingMs ?? this.options.defaultPendingMs;
771
+ const shouldPending = !!(onReady && !this.isServer && !preload && (route.options.loader || route.options.beforeLoad) && typeof pendingMs === "number" && pendingMs !== Infinity && (route.options.pendingComponent ?? this.options.defaultPendingComponent));
772
772
  if (shouldPending) {
773
773
  setTimeout(() => {
774
774
  try {
@@ -800,7 +800,7 @@ class Router {
800
800
  firstBadMatchIndex = firstBadMatchIndex ?? index;
801
801
  handleRedirectAndNotFound(match, err);
802
802
  try {
803
- (_b2 = (_a2 = route2.options).onError) == null ? void 0 : _b2.call(_a2, err);
803
+ (_b2 = (_a2 = route.options).onError) == null ? void 0 : _b2.call(_a2, err);
804
804
  } catch (errorHandlerErr) {
805
805
  err = errorHandlerErr;
806
806
  handleRedirectAndNotFound(match, err);
@@ -830,7 +830,7 @@ class Router {
830
830
  context: utils.replaceEqualDeep(match.context, parentContext),
831
831
  abortController
832
832
  }));
833
- const beforeLoadContext = await ((_b = (_a = route2.options).beforeLoad) == null ? void 0 : _b.call(_a, {
833
+ const beforeLoadContext = await ((_b = (_a = route.options).beforeLoad) == null ? void 0 : _b.call(_a, {
834
834
  search: match.search,
835
835
  abortController,
836
836
  params: match.params,
@@ -870,7 +870,7 @@ class Router {
870
870
  validResolvedMatches.forEach((match, index) => {
871
871
  const createValidateResolvedMatchPromise = async () => {
872
872
  const parentMatchPromise = matchPromises[index - 1];
873
- const route2 = this.looseRoutesById[match.routeId];
873
+ const route = this.looseRoutesById[match.routeId];
874
874
  const loaderContext = {
875
875
  params: match.params,
876
876
  deps: match.loaderDeps,
@@ -881,7 +881,7 @@ class Router {
881
881
  location,
882
882
  navigate: (opts) => this.navigate({ ...opts, from: match.pathname }),
883
883
  cause: preload ? "preload" : match.cause,
884
- route: route2
884
+ route
885
885
  };
886
886
  const fetch = async () => {
887
887
  var _a2, _b2, _c, _d, _e, _f, _g, _h, _i;
@@ -910,13 +910,13 @@ class Router {
910
910
  fetchCount: match.fetchCount + 1
911
911
  })
912
912
  );
913
- lazyPromise = ((_a2 = route2.lazyFn) == null ? void 0 : _a2.call(route2).then((lazyRoute) => {
914
- Object.assign(route2.options, lazyRoute.options);
913
+ lazyPromise = ((_a2 = route.lazyFn) == null ? void 0 : _a2.call(route).then((lazyRoute) => {
914
+ Object.assign(route.options, lazyRoute.options);
915
915
  })) || Promise.resolve();
916
916
  componentsPromise = lazyPromise.then(
917
917
  () => Promise.all(
918
918
  componentTypes.map(async (type) => {
919
- const component = route2.options[type];
919
+ const component = route.options[type];
920
920
  if (component == null ? void 0 : component.preload) {
921
921
  await component.preload();
922
922
  }
@@ -925,7 +925,7 @@ class Router {
925
925
  );
926
926
  await lazyPromise;
927
927
  checkLatest();
928
- loaderPromise = (_c = (_b2 = route2.options).loader) == null ? void 0 : _c.call(_b2, loaderContext);
928
+ loaderPromise = (_c = (_b2 = route.options).loader) == null ? void 0 : _c.call(_b2, loaderContext);
929
929
  matches[index] = match = updateMatch(
930
930
  match.id,
931
931
  (prev) => ({
@@ -939,12 +939,12 @@ class Router {
939
939
  handleRedirectAndNotFound(match, loaderData);
940
940
  await potentialPendingMinPromise();
941
941
  checkLatest();
942
- const meta = (_e = (_d = route2.options).meta) == null ? void 0 : _e.call(_d, {
942
+ const meta = (_e = (_d = route.options).meta) == null ? void 0 : _e.call(_d, {
943
943
  matches,
944
944
  params: match.params,
945
945
  loaderData
946
946
  });
947
- const headers = (_g = (_f = route2.options).headers) == null ? void 0 : _g.call(_f, {
947
+ const headers = (_g = (_f = route.options).headers) == null ? void 0 : _g.call(_f, {
948
948
  loaderData
949
949
  });
950
950
  matches[index] = match = updateMatch(match.id, (prev) => ({
@@ -964,7 +964,7 @@ class Router {
964
964
  checkLatest();
965
965
  handleRedirectAndNotFound(match, e);
966
966
  try {
967
- (_i = (_h = route2.options).onError) == null ? void 0 : _i.call(_h, e);
967
+ (_i = (_h = route.options).onError) == null ? void 0 : _i.call(_h, e);
968
968
  } catch (onErrorError) {
969
969
  error = onErrorError;
970
970
  handleRedirectAndNotFound(match, onErrorError);
@@ -981,8 +981,8 @@ class Router {
981
981
  match.loadPromise.resolve();
982
982
  };
983
983
  const age = Date.now() - match.updatedAt;
984
- const staleAge = preload ? route2.options.preloadStaleTime ?? this.options.defaultPreloadStaleTime ?? 3e4 : route2.options.staleTime ?? this.options.defaultStaleTime ?? 0;
985
- const shouldReloadOption = route2.options.shouldReload;
984
+ const staleAge = preload ? route.options.preloadStaleTime ?? this.options.defaultPreloadStaleTime ?? 3e4 : route.options.staleTime ?? this.options.defaultStaleTime ?? 0;
985
+ const shouldReloadOption = route.options.shouldReload;
986
986
  const shouldReload = typeof shouldReloadOption === "function" ? shouldReloadOption(loaderContext) : shouldReloadOption;
987
987
  matches[index] = match = {
988
988
  ...match,
@@ -1024,6 +1024,10 @@ class Router {
1024
1024
  await triggerOnReady();
1025
1025
  } catch (err) {
1026
1026
  if (redirects.isRedirect(err) || notFound.isNotFound(err)) {
1027
+ if (notFound.isNotFound(err) && !preload) {
1028
+ console.log("trigger");
1029
+ await triggerOnReady();
1030
+ }
1027
1031
  throw err;
1028
1032
  }
1029
1033
  }
@@ -1058,11 +1062,11 @@ class Router {
1058
1062
  return {
1059
1063
  ...s,
1060
1064
  cachedMatches: s.cachedMatches.filter((d) => {
1061
- const route2 = this.looseRoutesById[d.routeId];
1062
- if (!route2.options.loader) {
1065
+ const route = this.looseRoutesById[d.routeId];
1066
+ if (!route.options.loader) {
1063
1067
  return false;
1064
1068
  }
1065
- const gcTime = (d.preload ? route2.options.preloadGcTime ?? this.options.defaultPreloadGcTime : route2.options.gcTime ?? this.options.defaultGcTime) ?? 5 * 60 * 1e3;
1069
+ const gcTime = (d.preload ? route.options.preloadGcTime ?? this.options.defaultPreloadGcTime : route.options.gcTime ?? this.options.defaultGcTime) ?? 5 * 60 * 1e3;
1066
1070
  return d.status !== "error" && Date.now() - d.updatedAt < gcTime;
1067
1071
  })
1068
1072
  };
@@ -1216,7 +1220,7 @@ class Router {
1216
1220
  }
1217
1221
  invariant(
1218
1222
  _ctx,
1219
- "Expected to find a __TSR_DEHYDRATED__ property on window... but we did not. Did you forget to render <DehydrateRouter /> in your app?"
1223
+ "Expected to find a __TSR_DEHYDRATED__ property on window... but we did not. Please file an issue!"
1220
1224
  );
1221
1225
  const ctx = this.options.transformer.parse(_ctx);
1222
1226
  this.dehydratedData = ctx.payload;
@@ -1234,15 +1238,15 @@ class Router {
1234
1238
  dehydratedMatch,
1235
1239
  `Could not find a client-side match for dehydrated match with id: ${match.id}!`
1236
1240
  );
1237
- const route2 = this.looseRoutesById[match.routeId];
1241
+ const route = this.looseRoutesById[match.routeId];
1238
1242
  const assets = dehydratedMatch.status === "notFound" || dehydratedMatch.status === "redirected" ? {} : {
1239
- meta: (_b2 = (_a2 = route2.options).meta) == null ? void 0 : _b2.call(_a2, {
1243
+ meta: (_b2 = (_a2 = route.options).meta) == null ? void 0 : _b2.call(_a2, {
1240
1244
  matches: allMatches,
1241
1245
  params: match.params,
1242
1246
  loaderData: dehydratedMatch.loaderData
1243
1247
  }),
1244
- links: (_d = (_c2 = route2.options).links) == null ? void 0 : _d.call(_c2),
1245
- scripts: (_f = (_e = route2.options).scripts) == null ? void 0 : _f.call(_e)
1248
+ links: (_d = (_c2 = route.options).links) == null ? void 0 : _d.call(_c2),
1249
+ scripts: (_f = (_e = route.options).scripts) == null ? void 0 : _f.call(_e)
1246
1250
  };
1247
1251
  return {
1248
1252
  ...match,
@@ -1261,8 +1265,8 @@ class Router {
1261
1265
  const matchesByRouteId = Object.fromEntries(
1262
1266
  matches.map((match2) => [match2.routeId, match2])
1263
1267
  );
1264
- let routeCursor = (err.global ? this.looseRoutesById[route.rootRouteId] : this.looseRoutesById[err.routeId]) || this.looseRoutesById[route.rootRouteId];
1265
- while (!routeCursor.options.notFoundComponent && !this.options.defaultNotFoundComponent && routeCursor.id !== route.rootRouteId) {
1268
+ let routeCursor = (err.global ? this.looseRoutesById[root.rootRouteId] : this.looseRoutesById[err.routeId]) || this.looseRoutesById[root.rootRouteId];
1269
+ while (!routeCursor.options.notFoundComponent && !this.options.defaultNotFoundComponent && routeCursor.id !== root.rootRouteId) {
1266
1270
  routeCursor = routeCursor.parentRoute;
1267
1271
  invariant(
1268
1272
  routeCursor,