@tanstack/react-router 0.0.1-beta.177 → 0.0.1-beta.179

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.
@@ -871,6 +871,7 @@
871
871
  const componentTypes = ['component', 'errorComponent', 'pendingComponent'];
872
872
  const visibilityChangeEvent = 'visibilitychange';
873
873
  const focusEvent = 'focus';
874
+ const preloadWarning = 'Error preloading route! ☝️';
874
875
  class Router {
875
876
  #unsubHistory;
876
877
  resetNextScroll = false;
@@ -1300,39 +1301,14 @@
1300
1301
  return parentSearchInfo;
1301
1302
  }
1302
1303
  })();
1303
- Object.assign(match, {
1304
- ...searchInfo
1305
- });
1306
- const contextInfo = (() => {
1307
- try {
1308
- const routeContext = route.options.getContext?.({
1309
- parentContext: parentMatch?.routeContext ?? {},
1310
- context: parentMatch?.context ?? this?.options.context ?? {},
1311
- params: match.params,
1312
- search: match.search
1313
- }) || {};
1314
- const context = {
1315
- ...(parentMatch?.context ?? this?.options.context),
1316
- ...routeContext
1317
- };
1318
- return {
1319
- context,
1320
- routeContext
1321
- };
1322
- } catch (err) {
1323
- route.options.onError?.(err);
1324
- throw err;
1325
- }
1326
- })();
1327
- Object.assign(match, {
1328
- ...contextInfo
1329
- });
1304
+ Object.assign(match, searchInfo);
1330
1305
  });
1331
1306
  return matches;
1332
1307
  };
1333
- loadMatches = async (resolvedMatches, opts) => {
1308
+ loadMatches = async (_resolvedMatches, opts) => {
1309
+ const getFreshMatches = () => _resolvedMatches.map(d => this.getRouteMatch(d.id));
1334
1310
  if (!opts?.preload) {
1335
- resolvedMatches.forEach(match => {
1311
+ getFreshMatches().forEach(match => {
1336
1312
  // Update each match with its latest route data
1337
1313
  this.setRouteMatch(match.id, s => ({
1338
1314
  ...s,
@@ -1352,7 +1328,8 @@
1352
1328
 
1353
1329
  // Check each match middleware to see if the route can be accessed
1354
1330
  try {
1355
- for (const [index, match] of resolvedMatches.entries()) {
1331
+ for (const [index, match] of getFreshMatches().entries()) {
1332
+ const parentMatch = getFreshMatches()[index - 1];
1356
1333
  const route = this.getRoute(match.routeId);
1357
1334
  const handleError = (err, code) => {
1358
1335
  err.routerCode = code;
@@ -1383,10 +1360,21 @@
1383
1360
  }
1384
1361
  let didError = false;
1385
1362
  try {
1386
- await route.options.beforeLoad?.({
1363
+ const routeContext = (await route.options.beforeLoad?.({
1387
1364
  ...match,
1388
- preload: !!opts?.preload
1389
- });
1365
+ preload: !!opts?.preload,
1366
+ parentContext: parentMatch?.routeContext ?? {},
1367
+ context: parentMatch?.context ?? this?.options.context ?? {}
1368
+ })) ?? {};
1369
+ const context = {
1370
+ ...(parentMatch?.context ?? this?.options.context),
1371
+ ...routeContext
1372
+ };
1373
+ this.setRouteMatch(match.id, s => ({
1374
+ ...s,
1375
+ context,
1376
+ routeContext
1377
+ }));
1390
1378
  } catch (err) {
1391
1379
  handleError(err, 'BEFORE_LOAD');
1392
1380
  didError = true;
@@ -1403,7 +1391,7 @@
1403
1391
  }
1404
1392
  throw err;
1405
1393
  }
1406
- const validResolvedMatches = resolvedMatches.slice(0, firstBadMatchIndex);
1394
+ const validResolvedMatches = getFreshMatches().slice(0, firstBadMatchIndex);
1407
1395
  const matchPromises = [];
1408
1396
  validResolvedMatches.forEach((match, index) => {
1409
1397
  matchPromises.push((async () => {
@@ -1612,14 +1600,14 @@
1612
1600
  if (preload) {
1613
1601
  this.preloadRoute(nextOpts).catch(err => {
1614
1602
  console.warn(err);
1615
- console.warn('Error preloading route! ☝️');
1603
+ console.warn(preloadWarning);
1616
1604
  });
1617
1605
  }
1618
1606
  };
1619
1607
  const handleTouchStart = e => {
1620
1608
  this.preloadRoute(nextOpts).catch(err => {
1621
1609
  console.warn(err);
1622
- console.warn('Error preloading route! ☝️');
1610
+ console.warn(preloadWarning);
1623
1611
  });
1624
1612
  };
1625
1613
  const handleEnter = e => {
@@ -1632,7 +1620,7 @@
1632
1620
  target.preloadTimeout = null;
1633
1621
  this.preloadRoute(nextOpts).catch(err => {
1634
1622
  console.warn(err);
1635
- console.warn('Error preloading route! ☝️');
1623
+ console.warn(preloadWarning);
1636
1624
  });
1637
1625
  }, preloadDelay);
1638
1626
  }
@@ -1659,6 +1647,7 @@
1659
1647
  dehydrate = () => {
1660
1648
  return {
1661
1649
  state: {
1650
+ matchIds: this.state.matchIds,
1662
1651
  dehydratedMatches: this.state.matches.map(d => pick(d, ['fetchedAt', 'invalid', 'preloadMaxAge', 'maxAge', 'id', 'loaderData', 'status', 'updatedAt']))
1663
1652
  }
1664
1653
  };
@@ -1673,11 +1662,9 @@
1673
1662
  const ctx = _ctx;
1674
1663
  this.dehydratedData = ctx.payload;
1675
1664
  this.options.hydrate?.(ctx.payload);
1676
- const {
1677
- dehydratedMatches
1678
- } = ctx.router.state;
1665
+ const dehydratedState = ctx.router.state;
1679
1666
  let matches = this.matchRoutes(this.state.location.pathname, this.state.location.search).map(match => {
1680
- const dehydratedMatch = dehydratedMatches.find(d => d.id === match.id);
1667
+ const dehydratedMatch = dehydratedState.dehydratedMatches.find(d => d.id === match.id);
1681
1668
  invariant(dehydratedMatch, `Could not find a client-side match for dehydrated match with id: ${match.id}!`);
1682
1669
  if (dehydratedMatch) {
1683
1670
  return {
@@ -1690,6 +1677,7 @@
1690
1677
  this.__store.setState(s => {
1691
1678
  return {
1692
1679
  ...s,
1680
+ matchIds: dehydratedState.matchIds,
1693
1681
  matches,
1694
1682
  matchesById: this.#mergeMatches(s.matchesById, matches)
1695
1683
  };
@@ -1706,10 +1694,10 @@
1706
1694
  const id = `__TSR_DEHYDRATED__${strKey}`;
1707
1695
  const data = typeof getData === 'function' ? await getData() : getData;
1708
1696
  return `<script id='${id}' suppressHydrationWarning>window["__TSR_DEHYDRATED__${escapeJSON(strKey)}"] = ${JSON.stringify(data)}
1709
- // ;(() => {
1710
- // var el = document.getElementById('${id}')
1711
- // el.parentElement.removeChild(el)
1712
- // })()
1697
+ ;(() => {
1698
+ var el = document.getElementById('${id}')
1699
+ el.parentElement.removeChild(el)
1700
+ })()
1713
1701
  </script>`;
1714
1702
  });
1715
1703
  return () => this.hydrateData(key);
@@ -2200,14 +2188,14 @@
2200
2188
  return useMatch({
2201
2189
  ...opts,
2202
2190
  from: route.id,
2203
- select: d => opts?.select?.(d.context) ?? d.context
2191
+ select: d => opts?.select ? opts.select(d.context) : d.context
2204
2192
  });
2205
2193
  },
2206
2194
  useRouteContext: (opts = {}) => {
2207
2195
  return useMatch({
2208
2196
  ...opts,
2209
2197
  from: route.id,
2210
- select: d => opts?.select?.(d.routeContext) ?? d.routeContext
2198
+ select: d => opts?.select ? opts.select(d.routeContext) : d.routeContext
2211
2199
  });
2212
2200
  },
2213
2201
  useSearch: (opts = {}) => {
@@ -2414,7 +2402,7 @@
2414
2402
  return useRouterState({
2415
2403
  select: state => {
2416
2404
  const matches = state.matches.slice(state.matches.findIndex(d => d.id === matchIds[0]));
2417
- return opts?.select?.(matches) ?? matches;
2405
+ return opts?.select ? opts.select(matches) : matches;
2418
2406
  }
2419
2407
  });
2420
2408
  }
@@ -2432,39 +2420,39 @@
2432
2420
  if (opts?.strict ?? true) {
2433
2421
  invariant(nearestMatchRouteId == matchRouteId, `useMatch("${matchRouteId}") is being called in a component that is meant to render the '${nearestMatchRouteId}' route. Did you mean to 'useMatch("${matchRouteId}", { strict: false })' or 'useRoute("${matchRouteId}")' instead?`);
2434
2422
  }
2435
- const match = useRouterState({
2423
+ const matchSelection = useRouterState({
2436
2424
  select: state => {
2437
2425
  const matches = state.matches;
2438
2426
  const match = opts?.from ? matches.find(d => d.routeId === opts?.from) : matches.find(d => d.id === nearestMatchId);
2439
2427
  invariant(match, `Could not find ${opts?.from ? `an active match from "${opts.from}"` : 'a nearest match!'}`);
2440
- return opts?.select?.(match) ?? match;
2428
+ return opts?.select ? opts.select(match) : match;
2441
2429
  }
2442
2430
  });
2443
- return match;
2431
+ return matchSelection;
2444
2432
  }
2445
2433
  function useLoader(opts) {
2446
2434
  return useMatch({
2447
2435
  ...opts,
2448
- select: match => opts?.select?.(match.loaderData) ?? match.loaderData
2436
+ select: match => opts?.select ? opts?.select(match.loaderData) : match.loaderData
2449
2437
  });
2450
2438
  }
2451
2439
  function useRouterContext(opts) {
2452
2440
  return useMatch({
2453
2441
  ...opts,
2454
- select: match => opts?.select?.(match.context) ?? match.context
2442
+ select: match => opts?.select ? opts.select(match.context) : match.context
2455
2443
  });
2456
2444
  }
2457
2445
  function useRouteContext(opts) {
2458
2446
  return useMatch({
2459
2447
  ...opts,
2460
- select: match => opts?.select?.(match.routeContext) ?? match.routeContext
2448
+ select: match => opts?.select ? opts.select(match.routeContext) : match.routeContext
2461
2449
  });
2462
2450
  }
2463
2451
  function useSearch(opts) {
2464
2452
  return useMatch({
2465
2453
  ...opts,
2466
2454
  select: match => {
2467
- return opts?.select?.(match.search) ?? match.search;
2455
+ return opts?.select ? opts.select(match.search) : match.search;
2468
2456
  }
2469
2457
  });
2470
2458
  }
@@ -2472,7 +2460,7 @@
2472
2460
  return useRouterState({
2473
2461
  select: state => {
2474
2462
  const params = last(state.matches)?.params;
2475
- return opts?.select?.(params) ?? params;
2463
+ return opts?.select ? opts.select(params) : params;
2476
2464
  }
2477
2465
  });
2478
2466
  }