@tanstack/react-router 0.0.1-beta.245 → 0.0.1-beta.247

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.
@@ -415,9 +415,12 @@ function matchPathname(basepath, currentPathname, matchLocation) {
415
415
  }
416
416
  return pathParams ?? {};
417
417
  }
418
+ function removeBasepath(basepath, pathname) {
419
+ return basepath != '/' ? pathname.substring(basepath.length) : pathname;
420
+ }
418
421
  function matchByPath(basepath, from, matchLocation) {
419
422
  // Remove the base path from the pathname
420
- from = basepath != '/' ? from.substring(basepath.length) : from;
423
+ from = removeBasepath(basepath, from);
421
424
  // Default to to $ (wildcard)
422
425
  const to = `${matchLocation.to ?? '$'}`;
423
426
  // Parse the from and to
@@ -477,7 +480,8 @@ function matchByPath(basepath, from, matchLocation) {
477
480
  }
478
481
  }
479
482
  if (!isLastBaseSegment && isLastRouteSegment) {
480
- return !!matchLocation.fuzzy;
483
+ params['**'] = joinPaths(baseSegments.slice(i + 1).map(d => d.value));
484
+ return !!matchLocation.fuzzy && routeSegment?.value !== '/';
481
485
  }
482
486
  }
483
487
  return true;
@@ -517,7 +521,6 @@ class Route {
517
521
  constructor(options) {
518
522
  this.options = options || {};
519
523
  this.isRoot = !options?.getParentRoute;
520
- Route.__onInit(this);
521
524
  this.$$typeof = Symbol.for('react.memo');
522
525
  }
523
526
  init = opts => {
@@ -561,10 +564,6 @@ class Route {
561
564
  Object.assign(this.options, options);
562
565
  return this;
563
566
  };
564
- static __onInit = route => {
565
- // This is a dummy static method that should get
566
- // replaced by a framework specific implementation if necessary
567
- };
568
567
  useMatch = opts => {
569
568
  return useMatch({
570
569
  ...opts,
@@ -613,6 +612,15 @@ function createRouteMask(opts) {
613
612
 
614
613
  //
615
614
 
615
+ class NotFoundRoute extends Route {
616
+ constructor(options) {
617
+ super({
618
+ ...options,
619
+ id: '404'
620
+ });
621
+ }
622
+ }
623
+
616
624
  const matchContext = /*#__PURE__*/React.createContext(undefined);
617
625
  function Matches() {
618
626
  const router = useRouter();
@@ -1310,11 +1318,15 @@ class Router {
1310
1318
  buildRouteTree = () => {
1311
1319
  this.routesById = {};
1312
1320
  this.routesByPath = {};
1321
+ const notFoundRoute = this.options.notFoundRoute;
1322
+ if (notFoundRoute) {
1323
+ notFoundRoute.init({
1324
+ originalIndex: 99999999999
1325
+ });
1326
+ this.routesById[notFoundRoute.id] = notFoundRoute;
1327
+ }
1313
1328
  const recurseRoutes = childRoutes => {
1314
1329
  childRoutes.forEach((childRoute, i) => {
1315
- // if (typeof childRoute === 'function') {
1316
- // childRoute = (childRoute as any)()
1317
- // }
1318
1330
  childRoute.init({
1319
1331
  originalIndex: i
1320
1332
  });
@@ -1334,8 +1346,12 @@ class Router {
1334
1346
  });
1335
1347
  };
1336
1348
  recurseRoutes([this.routeTree]);
1337
- this.flatRoutes = Object.values(this.routesByPath).map((d, i) => {
1338
- const trimmed = trimPath(d.fullPath);
1349
+ const scoredRoutes = [];
1350
+ Object.values(this.routesById).forEach((d, i) => {
1351
+ if (d.isRoot || !d.path) {
1352
+ return;
1353
+ }
1354
+ const trimmed = trimPathLeft(d.fullPath);
1339
1355
  const parsed = parsePathname(trimmed);
1340
1356
  while (parsed.length > 1 && parsed[0]?.value === '/') {
1341
1357
  parsed.shift();
@@ -1349,14 +1365,15 @@ class Router {
1349
1365
  }
1350
1366
  return 1;
1351
1367
  });
1352
- return {
1368
+ scoredRoutes.push({
1353
1369
  child: d,
1354
1370
  trimmed,
1355
1371
  parsed,
1356
1372
  index: i,
1357
1373
  score
1358
- };
1359
- }).sort((a, b) => {
1374
+ });
1375
+ });
1376
+ this.flatRoutes = scoredRoutes.sort((a, b) => {
1360
1377
  let isIndex = a.trimmed === '/' ? 1 : b.trimmed === '/' ? -1 : 0;
1361
1378
  if (isIndex !== 0) return isIndex;
1362
1379
  const length = Math.min(a.score.length, b.score.length);
@@ -1458,7 +1475,7 @@ class Router {
1458
1475
  const matchedParams = matchPathname(this.basepath, trimPathRight(pathname), {
1459
1476
  to: route.fullPath,
1460
1477
  caseSensitive: route.options.caseSensitive ?? this.options.caseSensitive,
1461
- fuzzy: false
1478
+ fuzzy: true
1462
1479
  });
1463
1480
  if (matchedParams) {
1464
1481
  routeParams = matchedParams;
@@ -1468,7 +1485,17 @@ class Router {
1468
1485
  });
1469
1486
  let routeCursor = foundRoute || this.routesById['__root__'];
1470
1487
  let matchedRoutes = [routeCursor];
1471
- // let includingLayouts = true
1488
+
1489
+ // Check to see if the route needs a 404 entry
1490
+ if (
1491
+ // If we found a route, and it's not an index route and we have left over path
1492
+ (foundRoute ? foundRoute.path !== '/' && routeParams['**'] :
1493
+ // Or if we didn't find a route and we have left over path
1494
+ trimPathRight(pathname)) &&
1495
+ // And we have a 404 route configured
1496
+ this.options.notFoundRoute) {
1497
+ matchedRoutes.push(this.options.notFoundRoute);
1498
+ }
1472
1499
  while (routeCursor?.parentRoute) {
1473
1500
  routeCursor = routeCursor.parentRoute;
1474
1501
  if (routeCursor) matchedRoutes.unshift(routeCursor);
@@ -2092,12 +2119,12 @@ class Router {
2092
2119
  let matches = this.matchRoutes(next.pathname, next.search, {
2093
2120
  throwOnError: true
2094
2121
  });
2095
- await this.loadMatches({
2122
+ matches = await this.loadMatches({
2096
2123
  matches,
2097
2124
  preload: true,
2098
2125
  checkLatest: () => undefined
2099
2126
  });
2100
- return [last(matches), matches];
2127
+ return matches;
2101
2128
  };
2102
2129
  buildLink = dest => {
2103
2130
  // If this link simply reloads the current route,
@@ -2499,13 +2526,7 @@ function useBlocker(message, condition = true) {
2499
2526
  } = useRouter();
2500
2527
  React.useEffect(() => {
2501
2528
  if (!condition) return;
2502
- let unblock = history.block((retry, cancel) => {
2503
- if (window.confirm(message)) {
2504
- unblock();
2505
- retry();
2506
- }
2507
- });
2508
- return unblock;
2529
+ return history.block(message);
2509
2530
  });
2510
2531
  }
2511
2532
  function Block({
@@ -2553,5 +2574,5 @@ function Navigate(props) {
2553
2574
  return null;
2554
2575
  }
2555
2576
 
2556
- export { Await, Block, CatchBoundary, CatchBoundaryImpl, ErrorComponent, FileRoute, Link, Match, MatchRoute, Matches, Navigate, Outlet, PathParamError, RootRoute, Route, Router, RouterProvider, ScrollRestoration, SearchParamError, cleanPath, componentTypes, createRouteMask, decode, deepEqual, defaultParseSearch, defaultStringifySearch, defer, encode, escapeJSON, functionalUpdate, getInitialRouterState, getRouteMatch, interpolatePath, isDehydratedDeferred, isPlainObject, isRedirect, isServer, joinPaths, last, lazyFn, lazyRouteComponent, matchByPath, matchContext, matchPathname, parsePathname, parseSearchWith, pick, redirect, replaceEqualDeep, resolvePath, rootRouteId, rootRouteWithContext, routerContext, shallow, stringifySearchWith, trimPath, trimPathLeft, trimPathRight, typedNavigate, useAwaited, useBlocker, useElementScrollRestoration, useLayoutEffect$1 as useLayoutEffect, useLinkProps, useLoaderData, useMatch, useMatchRoute, useMatches, useNavigate, useParams, useRouteContext, useRouter, useRouterState, useScrollRestoration, useSearch, useStableCallback };
2577
+ export { Await, Block, CatchBoundary, CatchBoundaryImpl, ErrorComponent, FileRoute, Link, Match, MatchRoute, Matches, Navigate, NotFoundRoute, Outlet, PathParamError, RootRoute, Route, Router, RouterProvider, ScrollRestoration, SearchParamError, cleanPath, componentTypes, createRouteMask, decode, deepEqual, defaultParseSearch, defaultStringifySearch, defer, encode, escapeJSON, functionalUpdate, getInitialRouterState, getRouteMatch, interpolatePath, isDehydratedDeferred, isPlainObject, isRedirect, isServer, joinPaths, last, lazyFn, lazyRouteComponent, matchByPath, matchContext, matchPathname, parsePathname, parseSearchWith, pick, redirect, removeBasepath, replaceEqualDeep, resolvePath, rootRouteId, rootRouteWithContext, routerContext, shallow, stringifySearchWith, trimPath, trimPathLeft, trimPathRight, typedNavigate, useAwaited, useBlocker, useElementScrollRestoration, useLayoutEffect$1 as useLayoutEffect, useLinkProps, useLoaderData, useMatch, useMatchRoute, useMatches, useNavigate, useParams, useRouteContext, useRouter, useRouterState, useScrollRestoration, useSearch, useStableCallback };
2557
2578
  //# sourceMappingURL=index.js.map