@tanstack/router-core 0.0.1-beta.156 → 0.0.1-beta.158

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.
@@ -731,7 +731,7 @@
731
731
  }
732
732
 
733
733
  const defaultParseSearch = parseSearchWith(JSON.parse);
734
- const defaultStringifySearch = stringifySearchWith(JSON.stringify);
734
+ const defaultStringifySearch = stringifySearchWith(JSON.stringify, JSON.parse);
735
735
  function parseSearchWith(parser) {
736
736
  return searchStr => {
737
737
  if (searchStr.substring(0, 1) === '?') {
@@ -753,7 +753,26 @@
753
753
  return query;
754
754
  };
755
755
  }
756
- function stringifySearchWith(stringify) {
756
+ function stringifySearchWith(stringify, parser) {
757
+ function stringifyValue(val) {
758
+ if (typeof val === 'object' && val !== null) {
759
+ try {
760
+ return stringify(val);
761
+ } catch (err) {
762
+ // silent
763
+ }
764
+ } else if (typeof val === 'string' && typeof parser === 'function') {
765
+ try {
766
+ // Check if it's a valid parseable string.
767
+ // If it is, then stringify it again.
768
+ parser(val);
769
+ return stringify(val);
770
+ } catch (err) {
771
+ // silent
772
+ }
773
+ }
774
+ return val;
775
+ }
757
776
  return search => {
758
777
  search = {
759
778
  ...search
@@ -763,12 +782,10 @@
763
782
  const val = search[key];
764
783
  if (typeof val === 'undefined' || val === undefined) {
765
784
  delete search[key];
766
- } else if (val && typeof val === 'object' && val !== null) {
767
- try {
768
- search[key] = stringify(val);
769
- } catch (err) {
770
- // silent
771
- }
785
+ } else if (Array.isArray(val)) {
786
+ search[key] = val.map(stringifyValue);
787
+ } else {
788
+ search[key] = stringifyValue(val);
772
789
  }
773
790
  });
774
791
  }
@@ -1044,42 +1061,36 @@
1044
1061
  if (routeCursor) matchedRoutes.unshift(routeCursor);
1045
1062
  }
1046
1063
 
1047
- // Alright, by now we should have all of our
1048
- // matching routes and their param pairs, let's
1049
- // Turn them into actual `Match` objects and
1050
- // accumulate the params into a single params bag
1051
- let allParams = {};
1052
-
1053
1064
  // Existing matches are matches that are already loaded along with
1054
1065
  // pending matches that are still loading
1055
1066
 
1056
- const matches = matchedRoutes.map(route => {
1057
- let parsedParams;
1067
+ const parseErrors = matchedRoutes.map(route => {
1058
1068
  let parsedParamsError;
1059
- try {
1060
- parsedParams = route.options.parseParams?.(routeParams) ?? routeParams;
1061
- // (typeof route.options.parseParams === 'object' &&
1062
- // route.options.parseParams.parse
1063
- // ? route.options.parseParams.parse(routeParams)
1064
- // : (route.options.parseParams as any)?.(routeParams!)) ?? routeParams
1065
- } catch (err) {
1066
- parsedParamsError = new PathParamError(err.message, {
1067
- cause: err
1068
- });
1069
- if (opts?.throwOnError) {
1070
- throw parsedParamsError;
1069
+ if (route.options.parseParams) {
1070
+ try {
1071
+ const parsedParams = route.options.parseParams(routeParams);
1072
+ // Add the parsed params to the accumulated params bag
1073
+ Object.assign(routeParams, parsedParams);
1074
+ } catch (err) {
1075
+ parsedParamsError = new PathParamError(err.message, {
1076
+ cause: err
1077
+ });
1078
+ if (opts?.throwOnError) {
1079
+ throw parsedParamsError;
1080
+ }
1081
+ return parsedParamsError;
1071
1082
  }
1072
1083
  }
1073
-
1074
- // Add the parsed params to the accumulated params bag
1075
- Object.assign(allParams, parsedParams);
1076
- const interpolatedPath = interpolatePath(route.path, allParams);
1084
+ return;
1085
+ });
1086
+ const matches = matchedRoutes.map((route, index) => {
1087
+ const interpolatedPath = interpolatePath(route.path, routeParams);
1077
1088
  const key = route.options.key ? route.options.key({
1078
- params: allParams,
1089
+ params: routeParams,
1079
1090
  search: locationSearch
1080
1091
  }) ?? '' : '';
1081
1092
  const stringifiedKey = key ? JSON.stringify(key) : '';
1082
- const matchId = interpolatePath(route.id, allParams, true) + stringifiedKey;
1093
+ const matchId = interpolatePath(route.id, routeParams, true) + stringifiedKey;
1083
1094
 
1084
1095
  // Waste not, want not. If we already have a match for this route,
1085
1096
  // reuse it. This is important for layout routes, which might stick
@@ -1097,7 +1108,7 @@
1097
1108
  id: matchId,
1098
1109
  key: stringifiedKey,
1099
1110
  routeId: route.id,
1100
- params: allParams,
1111
+ params: routeParams,
1101
1112
  pathname: joinPaths([this.basepath, interpolatedPath]),
1102
1113
  updatedAt: Date.now(),
1103
1114
  invalidAt: Infinity,
@@ -1108,7 +1119,7 @@
1108
1119
  isFetching: false,
1109
1120
  invalid: false,
1110
1121
  error: undefined,
1111
- paramsError: parsedParamsError,
1122
+ paramsError: parseErrors[index],
1112
1123
  searchError: undefined,
1113
1124
  loaderData: undefined,
1114
1125
  loadPromise: Promise.resolve(),