@remix-run/router 1.1.0-pre.0 → 1.1.0-pre.1

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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # `@remix-run/router`
2
2
 
3
+ ## 1.1.0-pre.1
4
+
5
+ ### Patch Changes
6
+
7
+ - Fix issue with deeply nested optional segments ([#9727](https://github.com/remix-run/react-router/pull/9727))
8
+
3
9
  ## 1.1.0-pre.0
4
10
 
5
11
  ### Minor Changes
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @remix-run/router v1.1.0-pre.0
2
+ * @remix-run/router v1.1.0-pre.1
3
3
  *
4
4
  * Copyright (c) Remix Software Inc.
5
5
  *
@@ -691,22 +691,26 @@ function explodeOptionalSegments(path) {
691
691
  if (rest.length === 0) {
692
692
  // Intepret empty string as omitting an optional segment
693
693
  // `["one", "", "three"]` corresponds to omitting `:two` from `/one/:two?/three` -> `/one/three`
694
- return isOptional ? ["", required] : [required];
694
+ return isOptional ? [required, ""] : [required];
695
695
  }
696
696
 
697
697
  let restExploded = explodeOptionalSegments(rest.join("/"));
698
- return restExploded.flatMap(subpath => {
699
- // /one + / + :two/three -> /one/:two/three
700
- let requiredExploded = subpath === "" ? required : required + "/" + subpath; // For optional segments, return the exploded path _without_ current segment first (`subpath`)
701
- // and exploded path _with_ current segment later (`subpath`)
702
- // This ensures that exploded paths are emitted in priority order
703
- // `/one/three/:four` will come before `/one/three/:five`
704
-
705
- return isOptional ? [subpath, requiredExploded] : [requiredExploded];
706
- }).map(exploded => {
707
- // for absolute paths, ensure `/` instead of empty segment
708
- return path.startsWith("/") && exploded === "" ? "/" : exploded;
709
- });
698
+ let result = []; // All child paths with the prefix. Do this for all children before the
699
+ // optional version for all children so we get consistent ordering where the
700
+ // parent optional aspect is preferred as required. Otherwise, we can get
701
+ // child sections interspersed where deeper optional segments are higher than
702
+ // parent optional segments, where for example, /:two would explodes _earlier_
703
+ // then /:one. By always including the parent as required _for all children_
704
+ // first, we avoid this issue
705
+
706
+ result.push(...restExploded.map(subpath => subpath === "" ? required : [required, subpath].join("/"))); // Then if this is an optional value, add all child versions without
707
+
708
+ if (isOptional) {
709
+ result.push(...restExploded);
710
+ } // for absolute paths, ensure `/` instead of empty segment
711
+
712
+
713
+ return result.map(exploded => path.startsWith("/") && exploded === "" ? "/" : exploded);
710
714
  }
711
715
 
712
716
  function rankRouteBranches(branches) {