@tanstack/react-router 1.32.15 → 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 +65 -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 +16 -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 +18 -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);
@@ -821,7 +821,16 @@ class Router {
821
821
  }
822
822
  try {
823
823
  const parentContext = (parentMatch == null ? void 0 : parentMatch.context) ?? this.options.context ?? {};
824
- const beforeLoadContext = await ((_b = (_a = route2.options).beforeLoad) == null ? void 0 : _b.call(_a, {
824
+ matches[index] = match = updateMatch(match.id, (prev) => ({
825
+ ...prev,
826
+ routeContext: utils.replaceEqualDeep(
827
+ match.routeContext,
828
+ parentContext
829
+ ),
830
+ context: utils.replaceEqualDeep(match.context, parentContext),
831
+ abortController
832
+ }));
833
+ const beforeLoadContext = await ((_b = (_a = route.options).beforeLoad) == null ? void 0 : _b.call(_a, {
825
834
  search: match.search,
826
835
  abortController,
827
836
  params: match.params,
@@ -861,7 +870,7 @@ class Router {
861
870
  validResolvedMatches.forEach((match, index) => {
862
871
  const createValidateResolvedMatchPromise = async () => {
863
872
  const parentMatchPromise = matchPromises[index - 1];
864
- const route2 = this.looseRoutesById[match.routeId];
873
+ const route = this.looseRoutesById[match.routeId];
865
874
  const loaderContext = {
866
875
  params: match.params,
867
876
  deps: match.loaderDeps,
@@ -872,7 +881,7 @@ class Router {
872
881
  location,
873
882
  navigate: (opts) => this.navigate({ ...opts, from: match.pathname }),
874
883
  cause: preload ? "preload" : match.cause,
875
- route: route2
884
+ route
876
885
  };
877
886
  const fetch = async () => {
878
887
  var _a2, _b2, _c, _d, _e, _f, _g, _h, _i;
@@ -901,13 +910,13 @@ class Router {
901
910
  fetchCount: match.fetchCount + 1
902
911
  })
903
912
  );
904
- lazyPromise = ((_a2 = route2.lazyFn) == null ? void 0 : _a2.call(route2).then((lazyRoute) => {
905
- 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);
906
915
  })) || Promise.resolve();
907
916
  componentsPromise = lazyPromise.then(
908
917
  () => Promise.all(
909
918
  componentTypes.map(async (type) => {
910
- const component = route2.options[type];
919
+ const component = route.options[type];
911
920
  if (component == null ? void 0 : component.preload) {
912
921
  await component.preload();
913
922
  }
@@ -916,7 +925,7 @@ class Router {
916
925
  );
917
926
  await lazyPromise;
918
927
  checkLatest();
919
- 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);
920
929
  matches[index] = match = updateMatch(
921
930
  match.id,
922
931
  (prev) => ({
@@ -930,12 +939,12 @@ class Router {
930
939
  handleRedirectAndNotFound(match, loaderData);
931
940
  await potentialPendingMinPromise();
932
941
  checkLatest();
933
- 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, {
934
943
  matches,
935
944
  params: match.params,
936
945
  loaderData
937
946
  });
938
- 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, {
939
948
  loaderData
940
949
  });
941
950
  matches[index] = match = updateMatch(match.id, (prev) => ({
@@ -955,7 +964,7 @@ class Router {
955
964
  checkLatest();
956
965
  handleRedirectAndNotFound(match, e);
957
966
  try {
958
- (_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);
959
968
  } catch (onErrorError) {
960
969
  error = onErrorError;
961
970
  handleRedirectAndNotFound(match, onErrorError);
@@ -972,8 +981,8 @@ class Router {
972
981
  match.loadPromise.resolve();
973
982
  };
974
983
  const age = Date.now() - match.updatedAt;
975
- const staleAge = preload ? route2.options.preloadStaleTime ?? this.options.defaultPreloadStaleTime ?? 3e4 : route2.options.staleTime ?? this.options.defaultStaleTime ?? 0;
976
- 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;
977
986
  const shouldReload = typeof shouldReloadOption === "function" ? shouldReloadOption(loaderContext) : shouldReloadOption;
978
987
  matches[index] = match = {
979
988
  ...match,
@@ -1015,6 +1024,10 @@ class Router {
1015
1024
  await triggerOnReady();
1016
1025
  } catch (err) {
1017
1026
  if (redirects.isRedirect(err) || notFound.isNotFound(err)) {
1027
+ if (notFound.isNotFound(err) && !preload) {
1028
+ console.log("trigger");
1029
+ await triggerOnReady();
1030
+ }
1018
1031
  throw err;
1019
1032
  }
1020
1033
  }
@@ -1049,11 +1062,11 @@ class Router {
1049
1062
  return {
1050
1063
  ...s,
1051
1064
  cachedMatches: s.cachedMatches.filter((d) => {
1052
- const route2 = this.looseRoutesById[d.routeId];
1053
- if (!route2.options.loader) {
1065
+ const route = this.looseRoutesById[d.routeId];
1066
+ if (!route.options.loader) {
1054
1067
  return false;
1055
1068
  }
1056
- 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;
1057
1070
  return d.status !== "error" && Date.now() - d.updatedAt < gcTime;
1058
1071
  })
1059
1072
  };
@@ -1207,7 +1220,7 @@ class Router {
1207
1220
  }
1208
1221
  invariant(
1209
1222
  _ctx,
1210
- "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!"
1211
1224
  );
1212
1225
  const ctx = this.options.transformer.parse(_ctx);
1213
1226
  this.dehydratedData = ctx.payload;
@@ -1225,15 +1238,15 @@ class Router {
1225
1238
  dehydratedMatch,
1226
1239
  `Could not find a client-side match for dehydrated match with id: ${match.id}!`
1227
1240
  );
1228
- const route2 = this.looseRoutesById[match.routeId];
1241
+ const route = this.looseRoutesById[match.routeId];
1229
1242
  const assets = dehydratedMatch.status === "notFound" || dehydratedMatch.status === "redirected" ? {} : {
1230
- 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, {
1231
1244
  matches: allMatches,
1232
1245
  params: match.params,
1233
1246
  loaderData: dehydratedMatch.loaderData
1234
1247
  }),
1235
- links: (_d = (_c2 = route2.options).links) == null ? void 0 : _d.call(_c2),
1236
- 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)
1237
1250
  };
1238
1251
  return {
1239
1252
  ...match,
@@ -1252,8 +1265,8 @@ class Router {
1252
1265
  const matchesByRouteId = Object.fromEntries(
1253
1266
  matches.map((match2) => [match2.routeId, match2])
1254
1267
  );
1255
- let routeCursor = (err.global ? this.looseRoutesById[route.rootRouteId] : this.looseRoutesById[err.routeId]) || this.looseRoutesById[route.rootRouteId];
1256
- 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) {
1257
1270
  routeCursor = routeCursor.parentRoute;
1258
1271
  invariant(
1259
1272
  routeCursor,