@tanstack/react-router 0.0.1-beta.191 → 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.
@@ -1246,12 +1246,19 @@
1246
1246
  });
1247
1247
  const matches = matchedRoutes.map((route, index) => {
1248
1248
  const interpolatedPath = interpolatePath(route.path, routeParams);
1249
- const key = route.options.key ? route.options.key({
1250
- params: routeParams,
1249
+ const loaderContext = route.options.loaderContext ? route.options.loaderContext({
1251
1250
  search: locationSearch
1252
- }) ?? '' : '';
1253
- const stringifiedKey = key ? JSON.stringify(key) : '';
1254
- const matchId = interpolatePath(route.id, routeParams, true) + stringifiedKey;
1251
+ }) : undefined;
1252
+ const matchId = JSON.stringify([interpolatePath(route.id, routeParams, true), loaderContext].filter(d => d !== undefined), (key, value) => {
1253
+ if (typeof value === 'function') {
1254
+ console.info(route);
1255
+ 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 👆`);
1256
+ }
1257
+ if (typeof value === 'object' && value !== null) {
1258
+ return Object.fromEntries(Object.keys(value).sort().map(key => [key, value[key]]));
1259
+ }
1260
+ return value;
1261
+ });
1255
1262
 
1256
1263
  // Waste not, want not. If we already have a match for this route,
1257
1264
  // reuse it. This is important for layout routes, which might stick
@@ -1267,7 +1274,7 @@
1267
1274
  const hasLoaders = !!(route.options.loader || componentTypes.some(d => route.options[d]?.preload));
1268
1275
  const routeMatch = {
1269
1276
  id: matchId,
1270
- key: stringifiedKey,
1277
+ loaderContext,
1271
1278
  routeId: route.id,
1272
1279
  params: routeParams,
1273
1280
  pathname: joinPaths([this.basepath, interpolatedPath]),
@@ -1284,7 +1291,6 @@
1284
1291
  searchError: undefined,
1285
1292
  loaderData: undefined,
1286
1293
  loadPromise: Promise.resolve(),
1287
- routeContext: undefined,
1288
1294
  context: undefined,
1289
1295
  abortController: new AbortController(),
1290
1296
  fetchedAt: 0
@@ -1341,7 +1347,6 @@
1341
1347
  ...s,
1342
1348
  routeSearch: match.routeSearch,
1343
1349
  search: match.search,
1344
- routeContext: match.routeContext,
1345
1350
  context: match.context,
1346
1351
  error: match.error,
1347
1352
  paramsError: match.paramsError,
@@ -1386,21 +1391,25 @@
1386
1391
  handleError(match.searchError, 'VALIDATE_SEARCH');
1387
1392
  }
1388
1393
  let didError = false;
1394
+ const parentContext = parentMatch?.context ?? this?.options.context ?? {};
1389
1395
  try {
1390
- const routeContext = (await route.options.beforeLoad?.({
1391
- ...match,
1396
+ const beforeLoadContext = (await route.options.beforeLoad?.({
1397
+ abortController: match.abortController,
1398
+ params: match.params,
1392
1399
  preload: !!opts?.preload,
1393
- parentContext: parentMatch?.routeContext ?? {},
1394
- context: parentMatch?.context ?? this?.options.context ?? {}
1400
+ context: {
1401
+ ...parentContext,
1402
+ ...match.loaderContext
1403
+ }
1395
1404
  })) ?? {};
1396
1405
  const context = {
1397
- ...(parentMatch?.context ?? this?.options.context),
1398
- ...routeContext
1406
+ ...parentContext,
1407
+ ...match.loaderContext,
1408
+ ...beforeLoadContext
1399
1409
  };
1400
1410
  this.setRouteMatch(match.id, s => ({
1401
1411
  ...s,
1402
- context,
1403
- routeContext
1412
+ context
1404
1413
  }));
1405
1414
  } catch (err) {
1406
1415
  handleError(err, 'BEFORE_LOAD');
@@ -1454,9 +1463,11 @@
1454
1463
  }
1455
1464
  }));
1456
1465
  const loaderPromise = route.options.loader?.({
1457
- ...match,
1466
+ params: match.params,
1458
1467
  preload: !!opts?.preload,
1459
- parentMatchPromise
1468
+ parentMatchPromise,
1469
+ abortController: match.abortController,
1470
+ context: match.context
1460
1471
  });
1461
1472
  const [_, loader] = await Promise.all([componentsPromise, loaderPromise]);
1462
1473
  if (latestPromise = checkLatest()) return await latestPromise;
@@ -2558,7 +2569,7 @@
2558
2569
  function useRouteContext(opts) {
2559
2570
  return useMatch({
2560
2571
  ...opts,
2561
- select: match => opts?.select ? opts.select(match.routeContext) : match.routeContext
2572
+ select: match => opts?.select ? opts.select(match.context) : match.context
2562
2573
  });
2563
2574
  }
2564
2575
  function useSearch(opts) {