@remix-run/router 1.0.2 → 1.0.3-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/utils.ts CHANGED
@@ -329,7 +329,16 @@ export function matchRoutes<
329
329
 
330
330
  let matches = null;
331
331
  for (let i = 0; matches == null && i < branches.length; ++i) {
332
- matches = matchRouteBranch<string, RouteObjectType>(branches[i], pathname);
332
+ matches = matchRouteBranch<string, RouteObjectType>(
333
+ branches[i],
334
+ // Incoming pathnames are generally encoded from either window.location
335
+ // or from router.navigate, but we want to match against the unencoded
336
+ // paths in the route definitions. Memory router locations won't be
337
+ // encoded here but there also shouldn't be anything to decode so this
338
+ // should be a safe operation. This avoids needing matchRoutes to be
339
+ // history-aware.
340
+ safelyDecodeURI(pathname)
341
+ );
333
342
  }
334
343
 
335
344
  return matches;
@@ -702,6 +711,21 @@ function compilePath(
702
711
  return [matcher, paramNames];
703
712
  }
704
713
 
714
+ function safelyDecodeURI(value: string) {
715
+ try {
716
+ return decodeURI(value);
717
+ } catch (error) {
718
+ warning(
719
+ false,
720
+ `The URL path "${value}" could not be decoded because it is is a ` +
721
+ `malformed URL segment. This is probably due to a bad percent ` +
722
+ `encoding (${error}).`
723
+ );
724
+
725
+ return value;
726
+ }
727
+ }
728
+
705
729
  function safelyDecodeURIComponent(value: string, paramName: string) {
706
730
  try {
707
731
  return decodeURIComponent(value);
@@ -835,6 +859,38 @@ function getInvalidPathError(
835
859
  );
836
860
  }
837
861
 
862
+ /**
863
+ * @private
864
+ *
865
+ * When processing relative navigation we want to ignore ancestor routes that
866
+ * do not contribute to the path, such that index/pathless layout routes don't
867
+ * interfere.
868
+ *
869
+ * For example, when moving a route element into an index route and/or a
870
+ * pathless layout route, relative link behavior contained within should stay
871
+ * the same. Both of the following examples should link back to the root:
872
+ *
873
+ * <Route path="/">
874
+ * <Route path="accounts" element={<Link to=".."}>
875
+ * </Route>
876
+ *
877
+ * <Route path="/">
878
+ * <Route path="accounts">
879
+ * <Route element={<AccountsLayout />}> // <-- Does not contribute
880
+ * <Route index element={<Link to=".."} /> // <-- Does not contribute
881
+ * </Route
882
+ * </Route>
883
+ * </Route>
884
+ */
885
+ export function getPathContributingMatches<
886
+ T extends AgnosticRouteMatch = AgnosticRouteMatch
887
+ >(matches: T[]) {
888
+ return matches.filter(
889
+ (match, index) =>
890
+ index === 0 || (match.route.path && match.route.path.length > 0)
891
+ );
892
+ }
893
+
838
894
  /**
839
895
  * @private
840
896
  */