@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.
@@ -1379,12 +1379,10 @@ function createRouteMatch(router, route, opts) {
1379
1379
  isPending: false,
1380
1380
  isFetching: false,
1381
1381
  isInvalid: false,
1382
+ invalidAt: Infinity,
1382
1383
  getIsInvalid: () => {
1383
- var _ref, _routeMatch$options$l;
1384
-
1385
1384
  const now = Date.now();
1386
- const maxAge = (_ref = (_routeMatch$options$l = routeMatch.options.loaderMaxAge) != null ? _routeMatch$options$l : router.options.defaultLoaderMaxAge) != null ? _ref : 0;
1387
- return routeMatch.isInvalid || routeMatch.updatedAt + maxAge < now;
1385
+ return routeMatch.isInvalid || routeMatch.invalidAt < now;
1388
1386
  },
1389
1387
  __: {
1390
1388
  abortController: new AbortController(),
@@ -1475,7 +1473,7 @@ function createRouteMatch(router, route, opts) {
1475
1473
  hasLoaders: () => {
1476
1474
  return !!(route.options.loader || route.options.import || elementTypes.some(d => typeof route.options[d] === 'function'));
1477
1475
  },
1478
- load: async () => {
1476
+ load: async opts => {
1479
1477
  const id = '' + Date.now() + Math.random();
1480
1478
  routeMatch.__.latestId = id; // If the match was in an error state, set it
1481
1479
  // to a loading state again. Otherwise, keep it
@@ -1530,6 +1528,8 @@ function createRouteMatch(router, route, opts) {
1530
1528
 
1531
1529
  routeMatch.__.dataPromise = Promise.resolve().then(async () => {
1532
1530
  try {
1531
+ var _ref, _ref2, _opts$maxAge;
1532
+
1533
1533
  if (routeMatch.options.loader) {
1534
1534
  const data = await routeMatch.options.loader({
1535
1535
  params: routeMatch.params,
@@ -1547,6 +1547,7 @@ function createRouteMatch(router, route, opts) {
1547
1547
  routeMatch.error = undefined;
1548
1548
  routeMatch.status = 'success';
1549
1549
  routeMatch.updatedAt = Date.now();
1550
+ 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);
1550
1551
  } catch (err) {
1551
1552
  if (id !== routeMatch.__.latestId) {
1552
1553
  return routeMatch.__.loaderPromise;
@@ -1669,7 +1670,8 @@ function createRouter(userOptions) {
1669
1670
  const originalOptions = _extends({
1670
1671
  defaultLoaderGcMaxAge: 5 * 60 * 1000,
1671
1672
  defaultLoaderMaxAge: 0,
1672
- defaultLinkPreloadDelay: 50
1673
+ defaultPreloadMaxAge: 2000,
1674
+ defaultPreloadDelay: 50
1673
1675
  }, userOptions, {
1674
1676
  stringifySearch: (_userOptions$stringif = userOptions == null ? void 0 : userOptions.stringifySearch) != null ? _userOptions$stringif : defaultStringifySearch,
1675
1677
  parseSearch: (_userOptions$parseSea = userOptions == null ? void 0 : userOptions.parseSearch) != null ? _userOptions$parseSea : defaultParseSearch
@@ -2066,7 +2068,21 @@ function createRouter(userOptions) {
2066
2068
  delete router.matchCache[matchId];
2067
2069
  });
2068
2070
  },
2069
- loadRoute: async function loadRoute(navigateOpts, loaderOpts) {
2071
+ loadRoute: async function loadRoute(navigateOpts) {
2072
+ if (navigateOpts === void 0) {
2073
+ navigateOpts = router.location;
2074
+ }
2075
+
2076
+ const next = router.buildNext(navigateOpts);
2077
+ const matches = router.matchRoutes(next.pathname, {
2078
+ strictParseParams: true
2079
+ });
2080
+ await router.loadMatches(matches);
2081
+ return matches;
2082
+ },
2083
+ preloadRoute: async function preloadRoute(navigateOpts, loaderOpts) {
2084
+ var _ref4, _ref5, _loaderOpts$maxAge, _ref6, _ref7, _loaderOpts$gcMaxAge;
2085
+
2070
2086
  if (navigateOpts === void 0) {
2071
2087
  navigateOpts = router.location;
2072
2088
  }
@@ -2077,7 +2093,8 @@ function createRouter(userOptions) {
2077
2093
  });
2078
2094
  await router.loadMatches(matches, {
2079
2095
  preload: true,
2080
- maxAge: loaderOpts.maxAge
2096
+ maxAge: (_ref4 = (_ref5 = (_loaderOpts$maxAge = loaderOpts.maxAge) != null ? _loaderOpts$maxAge : router.options.defaultPreloadMaxAge) != null ? _ref5 : router.options.defaultLoaderMaxAge) != null ? _ref4 : 0,
2097
+ gcMaxAge: (_ref6 = (_ref7 = (_loaderOpts$gcMaxAge = loaderOpts.gcMaxAge) != null ? _loaderOpts$gcMaxAge : router.options.defaultPreloadGcMaxAge) != null ? _ref7 : router.options.defaultLoaderGcMaxAge) != null ? _ref6 : 0
2081
2098
  });
2082
2099
  return matches;
2083
2100
  },
@@ -2172,32 +2189,30 @@ function createRouter(userOptions) {
2172
2189
  },
2173
2190
  loadMatches: async (resolvedMatches, loaderOpts) => {
2174
2191
  const now = Date.now();
2192
+ const minMaxAge = loaderOpts != null && loaderOpts.preload ? Math.max(loaderOpts == null ? void 0 : loaderOpts.maxAge, loaderOpts == null ? void 0 : loaderOpts.gcMaxAge) : 0;
2175
2193
  const matchPromises = resolvedMatches.map(async match => {
2176
2194
  // Validate the match (loads search params etc)
2177
- match.__.validate(); // If the match doesn't have a loader, don't attempt to load it
2195
+ match.__.validate(); // If this is a preload, add it to the preload cache
2178
2196
 
2179
2197
 
2180
- if (!match.hasLoaders()) {
2181
- return;
2182
- } // If this is a preload, add it to the preload cache
2183
-
2184
-
2185
- if (loaderOpts != null && loaderOpts.preload && (loaderOpts == null ? void 0 : loaderOpts.maxAge) > 0) {
2198
+ if (loaderOpts != null && loaderOpts.preload && minMaxAge > 0) {
2186
2199
  // If the match is currently active, don't preload it
2187
2200
  if (router.state.matches.find(d => d.matchId === match.matchId)) {
2188
2201
  return;
2189
2202
  }
2190
2203
 
2191
2204
  router.matchCache[match.matchId] = {
2192
- gc: now + loaderOpts.maxAge,
2193
- // TODO: Should this use the route's maxAge?
2205
+ gc: now + loaderOpts.gcMaxAge,
2194
2206
  match
2195
2207
  };
2196
2208
  } // If the match is invalid, errored or idle, trigger it to load
2197
2209
 
2198
2210
 
2199
2211
  if (match.status === 'success' && match.getIsInvalid() || match.status === 'error' || match.status === 'idle') {
2200
- match.load();
2212
+ const maxAge = loaderOpts != null && loaderOpts.preload ? loaderOpts == null ? void 0 : loaderOpts.maxAge : undefined;
2213
+ match.load({
2214
+ maxAge
2215
+ });
2201
2216
  }
2202
2217
 
2203
2218
  if (match.status === 'loading') {
@@ -2259,7 +2274,7 @@ function createRouter(userOptions) {
2259
2274
  const next = router.buildNext(location);
2260
2275
  return router.commitLocation(next, location.replace);
2261
2276
  },
2262
- navigate: async _ref4 => {
2277
+ navigate: async _ref8 => {
2263
2278
  let {
2264
2279
  from,
2265
2280
  to = '.',
@@ -2267,7 +2282,7 @@ function createRouter(userOptions) {
2267
2282
  hash,
2268
2283
  replace,
2269
2284
  params
2270
- } = _ref4;
2285
+ } = _ref8;
2271
2286
  // If this link simply reloads the current route,
2272
2287
  // make sure it has a new key so it will trigger a data refresh
2273
2288
  // If this `to` is a valid external URL, return
@@ -2291,8 +2306,8 @@ function createRouter(userOptions) {
2291
2306
  params
2292
2307
  });
2293
2308
  },
2294
- buildLink: _ref5 => {
2295
- var _preload, _ref6, _ref7, _ref8;
2309
+ buildLink: _ref9 => {
2310
+ var _preload, _ref10;
2296
2311
 
2297
2312
  let {
2298
2313
  from,
@@ -2305,9 +2320,10 @@ function createRouter(userOptions) {
2305
2320
  activeOptions,
2306
2321
  preload,
2307
2322
  preloadMaxAge: userPreloadMaxAge,
2323
+ preloadGcMaxAge: userPreloadGcMaxAge,
2308
2324
  preloadDelay: userPreloadDelay,
2309
2325
  disabled
2310
- } = _ref5;
2326
+ } = _ref9;
2311
2327
 
2312
2328
  // If this link simply reloads the current route,
2313
2329
  // make sure it has a new key so it will trigger a data refresh
@@ -2330,9 +2346,8 @@ function createRouter(userOptions) {
2330
2346
  replace
2331
2347
  };
2332
2348
  const next = router.buildNext(nextOpts);
2333
- preload = (_preload = preload) != null ? _preload : router.options.defaultLinkPreload;
2334
- const preloadMaxAge = (_ref6 = (_ref7 = userPreloadMaxAge != null ? userPreloadMaxAge : router.options.defaultLinkPreloadMaxAge) != null ? _ref7 : router.options.defaultLoaderGcMaxAge) != null ? _ref6 : 0;
2335
- const preloadDelay = (_ref8 = userPreloadDelay != null ? userPreloadDelay : router.options.defaultLinkPreloadDelay) != null ? _ref8 : 0; // Compare path/hash for matches
2349
+ preload = (_preload = preload) != null ? _preload : router.options.defaultPreload;
2350
+ const preloadDelay = (_ref10 = userPreloadDelay != null ? userPreloadDelay : router.options.defaultPreloadDelay) != null ? _ref10 : 0; // Compare path/hash for matches
2336
2351
 
2337
2352
  const pathIsEqual = router.state.location.pathname === next.pathname;
2338
2353
  const currentPathSplit = router.state.location.pathname.split('/');
@@ -2360,9 +2375,10 @@ function createRouter(userOptions) {
2360
2375
 
2361
2376
 
2362
2377
  const handleFocus = e => {
2363
- if (preload && preloadMaxAge > 0) {
2364
- router.loadRoute(nextOpts, {
2365
- maxAge: preloadMaxAge
2378
+ if (preload) {
2379
+ router.preloadRoute(nextOpts, {
2380
+ maxAge: userPreloadMaxAge,
2381
+ gcMaxAge: userPreloadGcMaxAge
2366
2382
  });
2367
2383
  }
2368
2384
  };
@@ -2370,15 +2386,16 @@ function createRouter(userOptions) {
2370
2386
  const handleEnter = e => {
2371
2387
  const target = e.target || {};
2372
2388
 
2373
- if (preload && preloadMaxAge > 0) {
2389
+ if (preload) {
2374
2390
  if (target.preloadTimeout) {
2375
2391
  return;
2376
2392
  }
2377
2393
 
2378
2394
  target.preloadTimeout = setTimeout(() => {
2379
2395
  target.preloadTimeout = null;
2380
- router.loadRoute(nextOpts, {
2381
- maxAge: preloadMaxAge
2396
+ router.preloadRoute(nextOpts, {
2397
+ maxAge: userPreloadMaxAge,
2398
+ gcMaxAge: userPreloadGcMaxAge
2382
2399
  });
2383
2400
  }, preloadDelay);
2384
2401
  }