@tanstack/react-router 0.0.1-alpha.7 → 0.0.1-alpha.9

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.
@@ -1410,12 +1410,10 @@ function createRouteMatch(router, route, opts) {
1410
1410
  isPending: false,
1411
1411
  isFetching: false,
1412
1412
  isInvalid: false,
1413
+ invalidAt: Infinity,
1413
1414
  getIsInvalid: () => {
1414
- var _ref, _routeMatch$options$l;
1415
-
1416
1415
  const now = Date.now();
1417
- const maxAge = (_ref = (_routeMatch$options$l = routeMatch.options.loaderMaxAge) != null ? _routeMatch$options$l : router.options.defaultLoaderMaxAge) != null ? _ref : 0;
1418
- return routeMatch.isInvalid || routeMatch.updatedAt + maxAge < now;
1416
+ return routeMatch.isInvalid || routeMatch.invalidAt < now;
1419
1417
  },
1420
1418
  __: {
1421
1419
  abortController: new AbortController(),
@@ -1506,7 +1504,7 @@ function createRouteMatch(router, route, opts) {
1506
1504
  hasLoaders: () => {
1507
1505
  return !!(route.options.loader || route.options.import || elementTypes.some(d => typeof route.options[d] === 'function'));
1508
1506
  },
1509
- load: async () => {
1507
+ load: async opts => {
1510
1508
  const id = '' + Date.now() + Math.random();
1511
1509
  routeMatch.__.latestId = id; // If the match was in an error state, set it
1512
1510
  // to a loading state again. Otherwise, keep it
@@ -1561,6 +1559,8 @@ function createRouteMatch(router, route, opts) {
1561
1559
 
1562
1560
  routeMatch.__.dataPromise = Promise.resolve().then(async () => {
1563
1561
  try {
1562
+ var _ref, _ref2, _opts$maxAge;
1563
+
1564
1564
  if (routeMatch.options.loader) {
1565
1565
  const data = await routeMatch.options.loader({
1566
1566
  params: routeMatch.params,
@@ -1578,6 +1578,7 @@ function createRouteMatch(router, route, opts) {
1578
1578
  routeMatch.error = undefined;
1579
1579
  routeMatch.status = 'success';
1580
1580
  routeMatch.updatedAt = Date.now();
1581
+ routeMatch.invalidAt = routeMatch.updatedAt + ((_ref = (_ref2 = (_opts$maxAge = opts == null ? void 0 : opts.maxAge) != null ? _opts$maxAge : routeMatch.options.loaderMaxAge) != null ? _ref2 : router.options.defaultLoaderMaxAge) != null ? _ref : 0);
1581
1582
  } catch (err) {
1582
1583
  if (id !== routeMatch.__.latestId) {
1583
1584
  return routeMatch.__.loaderPromise;
@@ -1700,7 +1701,8 @@ function createRouter(userOptions) {
1700
1701
  const originalOptions = _extends({
1701
1702
  defaultLoaderGcMaxAge: 5 * 60 * 1000,
1702
1703
  defaultLoaderMaxAge: 0,
1703
- defaultLinkPreloadDelay: 50
1704
+ defaultPreloadMaxAge: 2000,
1705
+ defaultPreloadDelay: 50
1704
1706
  }, userOptions, {
1705
1707
  stringifySearch: (_userOptions$stringif = userOptions == null ? void 0 : userOptions.stringifySearch) != null ? _userOptions$stringif : defaultStringifySearch,
1706
1708
  parseSearch: (_userOptions$parseSea = userOptions == null ? void 0 : userOptions.parseSearch) != null ? _userOptions$parseSea : defaultParseSearch
@@ -2097,7 +2099,21 @@ function createRouter(userOptions) {
2097
2099
  delete router.matchCache[matchId];
2098
2100
  });
2099
2101
  },
2100
- loadRoute: async function loadRoute(navigateOpts, loaderOpts) {
2102
+ loadRoute: async function loadRoute(navigateOpts) {
2103
+ if (navigateOpts === void 0) {
2104
+ navigateOpts = router.location;
2105
+ }
2106
+
2107
+ const next = router.buildNext(navigateOpts);
2108
+ const matches = router.matchRoutes(next.pathname, {
2109
+ strictParseParams: true
2110
+ });
2111
+ await router.loadMatches(matches);
2112
+ return matches;
2113
+ },
2114
+ preloadRoute: async function preloadRoute(navigateOpts, loaderOpts) {
2115
+ var _ref4, _ref5, _loaderOpts$maxAge, _ref6, _ref7, _loaderOpts$gcMaxAge;
2116
+
2101
2117
  if (navigateOpts === void 0) {
2102
2118
  navigateOpts = router.location;
2103
2119
  }
@@ -2108,7 +2124,8 @@ function createRouter(userOptions) {
2108
2124
  });
2109
2125
  await router.loadMatches(matches, {
2110
2126
  preload: true,
2111
- maxAge: loaderOpts.maxAge
2127
+ maxAge: (_ref4 = (_ref5 = (_loaderOpts$maxAge = loaderOpts.maxAge) != null ? _loaderOpts$maxAge : router.options.defaultPreloadMaxAge) != null ? _ref5 : router.options.defaultLoaderMaxAge) != null ? _ref4 : 0,
2128
+ gcMaxAge: (_ref6 = (_ref7 = (_loaderOpts$gcMaxAge = loaderOpts.gcMaxAge) != null ? _loaderOpts$gcMaxAge : router.options.defaultPreloadGcMaxAge) != null ? _ref7 : router.options.defaultLoaderGcMaxAge) != null ? _ref6 : 0
2112
2129
  });
2113
2130
  return matches;
2114
2131
  },
@@ -2203,32 +2220,30 @@ function createRouter(userOptions) {
2203
2220
  },
2204
2221
  loadMatches: async (resolvedMatches, loaderOpts) => {
2205
2222
  const now = Date.now();
2223
+ const minMaxAge = loaderOpts != null && loaderOpts.preload ? Math.max(loaderOpts == null ? void 0 : loaderOpts.maxAge, loaderOpts == null ? void 0 : loaderOpts.gcMaxAge) : 0;
2206
2224
  const matchPromises = resolvedMatches.map(async match => {
2207
2225
  // Validate the match (loads search params etc)
2208
- match.__.validate(); // If the match doesn't have a loader, don't attempt to load it
2226
+ match.__.validate(); // If this is a preload, add it to the preload cache
2209
2227
 
2210
2228
 
2211
- if (!match.hasLoaders()) {
2212
- return;
2213
- } // If this is a preload, add it to the preload cache
2214
-
2215
-
2216
- if (loaderOpts != null && loaderOpts.preload && (loaderOpts == null ? void 0 : loaderOpts.maxAge) > 0) {
2229
+ if (loaderOpts != null && loaderOpts.preload && minMaxAge > 0) {
2217
2230
  // If the match is currently active, don't preload it
2218
2231
  if (router.state.matches.find(d => d.matchId === match.matchId)) {
2219
2232
  return;
2220
2233
  }
2221
2234
 
2222
2235
  router.matchCache[match.matchId] = {
2223
- gc: now + loaderOpts.maxAge,
2224
- // TODO: Should this use the route's maxAge?
2236
+ gc: now + loaderOpts.gcMaxAge,
2225
2237
  match
2226
2238
  };
2227
2239
  } // If the match is invalid, errored or idle, trigger it to load
2228
2240
 
2229
2241
 
2230
2242
  if (match.status === 'success' && match.getIsInvalid() || match.status === 'error' || match.status === 'idle') {
2231
- match.load();
2243
+ const maxAge = loaderOpts != null && loaderOpts.preload ? loaderOpts == null ? void 0 : loaderOpts.maxAge : undefined;
2244
+ match.load({
2245
+ maxAge
2246
+ });
2232
2247
  }
2233
2248
 
2234
2249
  if (match.status === 'loading') {
@@ -2290,7 +2305,7 @@ function createRouter(userOptions) {
2290
2305
  const next = router.buildNext(location);
2291
2306
  return router.commitLocation(next, location.replace);
2292
2307
  },
2293
- navigate: async _ref4 => {
2308
+ navigate: async _ref8 => {
2294
2309
  let {
2295
2310
  from,
2296
2311
  to = '.',
@@ -2298,7 +2313,7 @@ function createRouter(userOptions) {
2298
2313
  hash,
2299
2314
  replace,
2300
2315
  params
2301
- } = _ref4;
2316
+ } = _ref8;
2302
2317
  // If this link simply reloads the current route,
2303
2318
  // make sure it has a new key so it will trigger a data refresh
2304
2319
  // If this `to` is a valid external URL, return
@@ -2322,8 +2337,8 @@ function createRouter(userOptions) {
2322
2337
  params
2323
2338
  });
2324
2339
  },
2325
- buildLink: _ref5 => {
2326
- var _preload, _ref6, _ref7, _ref8;
2340
+ buildLink: _ref9 => {
2341
+ var _preload, _ref10;
2327
2342
 
2328
2343
  let {
2329
2344
  from,
@@ -2336,9 +2351,10 @@ function createRouter(userOptions) {
2336
2351
  activeOptions,
2337
2352
  preload,
2338
2353
  preloadMaxAge: userPreloadMaxAge,
2354
+ preloadGcMaxAge: userPreloadGcMaxAge,
2339
2355
  preloadDelay: userPreloadDelay,
2340
2356
  disabled
2341
- } = _ref5;
2357
+ } = _ref9;
2342
2358
 
2343
2359
  // If this link simply reloads the current route,
2344
2360
  // make sure it has a new key so it will trigger a data refresh
@@ -2361,9 +2377,8 @@ function createRouter(userOptions) {
2361
2377
  replace
2362
2378
  };
2363
2379
  const next = router.buildNext(nextOpts);
2364
- preload = (_preload = preload) != null ? _preload : router.options.defaultLinkPreload;
2365
- const preloadMaxAge = (_ref6 = (_ref7 = userPreloadMaxAge != null ? userPreloadMaxAge : router.options.defaultLinkPreloadMaxAge) != null ? _ref7 : router.options.defaultLoaderGcMaxAge) != null ? _ref6 : 0;
2366
- const preloadDelay = (_ref8 = userPreloadDelay != null ? userPreloadDelay : router.options.defaultLinkPreloadDelay) != null ? _ref8 : 0; // Compare path/hash for matches
2380
+ preload = (_preload = preload) != null ? _preload : router.options.defaultPreload;
2381
+ const preloadDelay = (_ref10 = userPreloadDelay != null ? userPreloadDelay : router.options.defaultPreloadDelay) != null ? _ref10 : 0; // Compare path/hash for matches
2367
2382
 
2368
2383
  const pathIsEqual = router.state.location.pathname === next.pathname;
2369
2384
  const currentPathSplit = router.state.location.pathname.split('/');
@@ -2391,9 +2406,10 @@ function createRouter(userOptions) {
2391
2406
 
2392
2407
 
2393
2408
  const handleFocus = e => {
2394
- if (preload && preloadMaxAge > 0) {
2395
- router.loadRoute(nextOpts, {
2396
- maxAge: preloadMaxAge
2409
+ if (preload) {
2410
+ router.preloadRoute(nextOpts, {
2411
+ maxAge: userPreloadMaxAge,
2412
+ gcMaxAge: userPreloadGcMaxAge
2397
2413
  });
2398
2414
  }
2399
2415
  };
@@ -2401,15 +2417,16 @@ function createRouter(userOptions) {
2401
2417
  const handleEnter = e => {
2402
2418
  const target = e.target || {};
2403
2419
 
2404
- if (preload && preloadMaxAge > 0) {
2420
+ if (preload) {
2405
2421
  if (target.preloadTimeout) {
2406
2422
  return;
2407
2423
  }
2408
2424
 
2409
2425
  target.preloadTimeout = setTimeout(() => {
2410
2426
  target.preloadTimeout = null;
2411
- router.loadRoute(nextOpts, {
2412
- maxAge: preloadMaxAge
2427
+ router.preloadRoute(nextOpts, {
2428
+ maxAge: userPreloadMaxAge,
2429
+ gcMaxAge: userPreloadGcMaxAge
2413
2430
  });
2414
2431
  }, preloadDelay);
2415
2432
  }