@tanstack/router-core 0.0.1-beta.192 → 0.0.1-beta.193

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.
@@ -1086,12 +1086,19 @@ class Router {
1086
1086
  });
1087
1087
  const matches = matchedRoutes.map((route, index) => {
1088
1088
  const interpolatedPath = interpolatePath(route.path, routeParams);
1089
- const key = route.options.key ? route.options.key({
1090
- params: routeParams,
1089
+ const loaderContext = route.options.loaderContext ? route.options.loaderContext({
1091
1090
  search: locationSearch
1092
- }) ?? '' : '';
1093
- const stringifiedKey = key ? JSON.stringify(key) : '';
1094
- const matchId = interpolatePath(route.id, routeParams, true) + stringifiedKey;
1091
+ }) : undefined;
1092
+ const matchId = JSON.stringify([interpolatePath(route.id, routeParams, true), loaderContext].filter(d => d !== undefined), (key, value) => {
1093
+ if (typeof value === 'function') {
1094
+ console.info(route);
1095
+ invariant(false, `Cannot return functions and other non-serializable values from routeOptions.loaderContext! Please use routeOptions.beforeLoad to do this. Route info is logged above 👆`);
1096
+ }
1097
+ if (typeof value === 'object' && value !== null) {
1098
+ return Object.fromEntries(Object.keys(value).sort().map(key => [key, value[key]]));
1099
+ }
1100
+ return value;
1101
+ });
1095
1102
 
1096
1103
  // Waste not, want not. If we already have a match for this route,
1097
1104
  // reuse it. This is important for layout routes, which might stick
@@ -1107,7 +1114,7 @@ class Router {
1107
1114
  const hasLoaders = !!(route.options.loader || componentTypes.some(d => route.options[d]?.preload));
1108
1115
  const routeMatch = {
1109
1116
  id: matchId,
1110
- key: stringifiedKey,
1117
+ loaderContext,
1111
1118
  routeId: route.id,
1112
1119
  params: routeParams,
1113
1120
  pathname: joinPaths([this.basepath, interpolatedPath]),
@@ -1124,7 +1131,6 @@ class Router {
1124
1131
  searchError: undefined,
1125
1132
  loaderData: undefined,
1126
1133
  loadPromise: Promise.resolve(),
1127
- routeContext: undefined,
1128
1134
  context: undefined,
1129
1135
  abortController: new AbortController(),
1130
1136
  fetchedAt: 0
@@ -1181,7 +1187,6 @@ class Router {
1181
1187
  ...s,
1182
1188
  routeSearch: match.routeSearch,
1183
1189
  search: match.search,
1184
- routeContext: match.routeContext,
1185
1190
  context: match.context,
1186
1191
  error: match.error,
1187
1192
  paramsError: match.paramsError,
@@ -1226,21 +1231,25 @@ class Router {
1226
1231
  handleError(match.searchError, 'VALIDATE_SEARCH');
1227
1232
  }
1228
1233
  let didError = false;
1234
+ const parentContext = parentMatch?.context ?? this?.options.context ?? {};
1229
1235
  try {
1230
- const routeContext = (await route.options.beforeLoad?.({
1231
- ...match,
1236
+ const beforeLoadContext = (await route.options.beforeLoad?.({
1237
+ abortController: match.abortController,
1238
+ params: match.params,
1232
1239
  preload: !!opts?.preload,
1233
- parentContext: parentMatch?.routeContext ?? {},
1234
- context: parentMatch?.context ?? this?.options.context ?? {}
1240
+ context: {
1241
+ ...parentContext,
1242
+ ...match.loaderContext
1243
+ }
1235
1244
  })) ?? {};
1236
1245
  const context = {
1237
- ...(parentMatch?.context ?? this?.options.context),
1238
- ...routeContext
1246
+ ...parentContext,
1247
+ ...match.loaderContext,
1248
+ ...beforeLoadContext
1239
1249
  };
1240
1250
  this.setRouteMatch(match.id, s => ({
1241
1251
  ...s,
1242
- context,
1243
- routeContext
1252
+ context
1244
1253
  }));
1245
1254
  } catch (err) {
1246
1255
  handleError(err, 'BEFORE_LOAD');
@@ -1294,9 +1303,11 @@ class Router {
1294
1303
  }
1295
1304
  }));
1296
1305
  const loaderPromise = route.options.loader?.({
1297
- ...match,
1306
+ params: match.params,
1298
1307
  preload: !!opts?.preload,
1299
- parentMatchPromise
1308
+ parentMatchPromise,
1309
+ abortController: match.abortController,
1310
+ context: match.context
1300
1311
  });
1301
1312
  const [_, loader] = await Promise.all([componentsPromise, loaderPromise]);
1302
1313
  if (latestPromise = checkLatest()) return await latestPromise;