@remix-run/router 1.16.1 → 1.17.0

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.
package/utils.ts CHANGED
@@ -255,6 +255,16 @@ export interface DataStrategyFunction {
255
255
  (args: DataStrategyFunctionArgs): Promise<HandlerResult[]>;
256
256
  }
257
257
 
258
+ export interface AgnosticPatchRoutesOnMissFunction<
259
+ M extends AgnosticRouteMatch = AgnosticRouteMatch
260
+ > {
261
+ (opts: {
262
+ path: string;
263
+ matches: M[];
264
+ patch: (routeId: string | null, children: AgnosticRouteObject[]) => void;
265
+ }): void | Promise<void>;
266
+ }
267
+
258
268
  /**
259
269
  * Function provided by the framework-aware layers to set any framework-specific
260
270
  * properties from framework-agnostic properties
@@ -444,11 +454,11 @@ function isIndexRoute(
444
454
  export function convertRoutesToDataRoutes(
445
455
  routes: AgnosticRouteObject[],
446
456
  mapRouteProperties: MapRoutePropertiesFunction,
447
- parentPath: number[] = [],
457
+ parentPath: string[] = [],
448
458
  manifest: RouteManifest = {}
449
459
  ): AgnosticDataRouteObject[] {
450
460
  return routes.map((route, index) => {
451
- let treePath = [...parentPath, index];
461
+ let treePath = [...parentPath, String(index)];
452
462
  let id = typeof route.id === "string" ? route.id : treePath.join("-");
453
463
  invariant(
454
464
  route.index !== true || !route.children,
@@ -502,6 +512,17 @@ export function matchRoutes<
502
512
  routes: RouteObjectType[],
503
513
  locationArg: Partial<Location> | string,
504
514
  basename = "/"
515
+ ): AgnosticRouteMatch<string, RouteObjectType>[] | null {
516
+ return matchRoutesImpl(routes, locationArg, basename, false);
517
+ }
518
+
519
+ export function matchRoutesImpl<
520
+ RouteObjectType extends AgnosticRouteObject = AgnosticRouteObject
521
+ >(
522
+ routes: RouteObjectType[],
523
+ locationArg: Partial<Location> | string,
524
+ basename: string,
525
+ allowPartial: boolean
505
526
  ): AgnosticRouteMatch<string, RouteObjectType>[] | null {
506
527
  let location =
507
528
  typeof locationArg === "string" ? parsePath(locationArg) : locationArg;
@@ -524,7 +545,11 @@ export function matchRoutes<
524
545
  // should be a safe operation. This avoids needing matchRoutes to be
525
546
  // history-aware.
526
547
  let decoded = decodePath(pathname);
527
- matches = matchRouteBranch<string, RouteObjectType>(branches[i], decoded);
548
+ matches = matchRouteBranch<string, RouteObjectType>(
549
+ branches[i],
550
+ decoded,
551
+ allowPartial
552
+ );
528
553
  }
529
554
 
530
555
  return matches;
@@ -615,7 +640,6 @@ function flattenRoutes<
615
640
  `Index routes must not have child routes. Please remove ` +
616
641
  `all child routes from route path "${path}".`
617
642
  );
618
-
619
643
  flattenRoutes(route.children, branches, routesMeta, path);
620
644
  }
621
645
 
@@ -768,7 +792,8 @@ function matchRouteBranch<
768
792
  RouteObjectType extends AgnosticRouteObject = AgnosticRouteObject
769
793
  >(
770
794
  branch: RouteBranch<RouteObjectType>,
771
- pathname: string
795
+ pathname: string,
796
+ allowPartial = false
772
797
  ): AgnosticRouteMatch<ParamKey, RouteObjectType>[] | null {
773
798
  let { routesMeta } = branch;
774
799
 
@@ -787,11 +812,29 @@ function matchRouteBranch<
787
812
  remainingPathname
788
813
  );
789
814
 
790
- if (!match) return null;
815
+ let route = meta.route;
791
816
 
792
- Object.assign(matchedParams, match.params);
817
+ if (
818
+ !match &&
819
+ end &&
820
+ allowPartial &&
821
+ !routesMeta[routesMeta.length - 1].route.index
822
+ ) {
823
+ match = matchPath(
824
+ {
825
+ path: meta.relativePath,
826
+ caseSensitive: meta.caseSensitive,
827
+ end: false,
828
+ },
829
+ remainingPathname
830
+ );
831
+ }
793
832
 
794
- let route = meta.route;
833
+ if (!match) {
834
+ return null;
835
+ }
836
+
837
+ Object.assign(matchedParams, match.params);
795
838
 
796
839
  matches.push({
797
840
  // TODO: Can this as be avoided?