react-router 6.20.1-pre.0 → 6.21.0-pre.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/CHANGELOG.md CHANGED
@@ -1,12 +1,153 @@
1
1
  # `react-router`
2
2
 
3
- ## 6.20.1-pre.0
3
+ ## 6.21.0-pre.0
4
+
5
+ ### Minor Changes
6
+
7
+ - Add a new `future.v7_relativeSplatPath` flag to implenent a breaking bug fix to relative routing when inside a splat route. ([#11087](https://github.com/remix-run/react-router/pull/11087))
8
+
9
+ This fix was originally added in [#10983](https://github.com/remix-run/react-router/issues/10983) and was later reverted in [#11078](https://github.com/remix-run/react-router/issues/110788) because it was determined that a large number of existing applications were relying on the buggy behavior (see [#11052](https://github.com/remix-run/react-router/issues/11052))
10
+
11
+ **The Bug**
12
+ The buggy behavior is that without this flag, the default behavior when resolving relative paths is to _ignore_ any splat (`*`) portion of the current route path.
13
+
14
+ **The Background**
15
+ This decision was originally made thinking that it would make the concept of nested different sections of your apps in `<Routes>` easier if relative routing would _replace_ the current splat:
16
+
17
+ ```jsx
18
+ <BrowserRouter>
19
+ <Routes>
20
+ <Route path="/" element={<Home />} />
21
+ <Route path="dashboard/*" element={<Dashboard />} />
22
+ </Routes>
23
+ </BrowserRouter>
24
+ ```
25
+
26
+ Any paths like `/dashboard`, `/dashboard/team`, `/dashboard/projects` will match the `Dashboard` route. The dashboard component itself can then render nested `<Routes>`:
27
+
28
+ ```jsx
29
+ function Dashboard() {
30
+ return (
31
+ <div>
32
+ <h2>Dashboard</h2>
33
+ <nav>
34
+ <Link to="/">Dashboard Home</Link>
35
+ <Link to="team">Team</Link>
36
+ <Link to="projects">Projects</Link>
37
+ </nav>
38
+
39
+ <Routes>
40
+ <Route path="/" element={<DashboardHome />} />
41
+ <Route path="team" element={<DashboardTeam />} />
42
+ <Route path="projects" element={<DashboardProjects />} />
43
+ </Routes>
44
+ </div>
45
+ );
46
+ }
47
+ ```
48
+
49
+ Now, all links and route paths are relative to the router above them. This makes code splitting and compartmentalizing your app really easy. You could render the `Dashboard` as its own independent app, or embed it into your large app without making any changes to it.
50
+
51
+ **The Problem**
52
+
53
+ The problem is that this concept of ignoring part of a pth breaks a lot of other assumptions in React Router - namely that `"."` always means the current location pathname for that route. When we ignore the splat portion, we start getting invalid paths when using `"."`:
54
+
55
+ ```jsx
56
+ // If we are on URL /dashboard/team, and we want to link to /dashboard/team:
57
+ function DashboardTeam() {
58
+ // ❌ This is broken and results in <a href="/dashboard">
59
+ return <Link to=".">A broken link to the Current URL</Link>;
60
+
61
+ // ✅ This is fixed but super unintuitive since we're already at /dashboard/team!
62
+ return <Link to="./team">A broken link to the Current URL</Link>;
63
+ }
64
+ ```
65
+
66
+ We've also introduced an issue that we can no longer move our `DashboardTeam` component around our route hierarchy easily - since it behaves differently if we're underneath a non-splat route, such as `/dashboard/:widget`. Now, our `"."` links will, properly point to ourself _inclusive of the dynamic param value_ so behavior will break from it's corresponding usage in a `/dashboard/*` route.
67
+
68
+ Even worse, consider a nested splat route configuration:
69
+
70
+ ```jsx
71
+ <BrowserRouter>
72
+ <Routes>
73
+ <Route path="dashboard">
74
+ <Route path="*" element={<Dashboard />} />
75
+ </Route>
76
+ </Routes>
77
+ </BrowserRouter>
78
+ ```
79
+
80
+ Now, a `<Link to=".">` and a `<Link to="..">` inside the `Dashboard` component go to the same place! That is definitely not correct!
81
+
82
+ Another common issue arose in Data Routers (and Remix) where any `<Form>` should post to it's own route `action` if you the user doesn't specify a form action:
83
+
84
+ ```jsx
85
+ let router = createBrowserRouter({
86
+ path: "/dashboard",
87
+ children: [
88
+ {
89
+ path: "*",
90
+ action: dashboardAction,
91
+ Component() {
92
+ // ❌ This form is broken! It throws a 405 error when it submits because
93
+ // it tries to submit to /dashboard (without the splat value) and the parent
94
+ // `/dashboard` route doesn't have an action
95
+ return <Form method="post">...</Form>;
96
+ },
97
+ },
98
+ ],
99
+ });
100
+ ```
101
+
102
+ This is just a compounded issue from the above because the default location for a `Form` to submit to is itself (`"."`) - and if we ignore the splat portion, that now resolves to the parent route.
103
+
104
+ **The Solution**
105
+ If you are leveraging this behavior, it's recommended to enable the future flag, move your splat to it's own route, and leverage `../` for any links to "sibling" pages:
106
+
107
+ ```jsx
108
+ <BrowserRouter>
109
+ <Routes>
110
+ <Route path="dashboard">
111
+ <Route path="*" element={<Dashboard />} />
112
+ </Route>
113
+ </Routes>
114
+ </BrowserRouter>
115
+
116
+ function Dashboard() {
117
+ return (
118
+ <div>
119
+ <h2>Dashboard</h2>
120
+ <nav>
121
+ <Link to="..">Dashboard Home</Link>
122
+ <Link to="../team">Team</Link>
123
+ <Link to="../projects">Projects</Link>
124
+ </nav>
125
+
126
+ <Routes>
127
+ <Route path="/" element={<DashboardHome />} />
128
+ <Route path="team" element={<DashboardTeam />} />
129
+ <Route path="projects" element={<DashboardProjects />} />
130
+ </Router>
131
+ </div>
132
+ );
133
+ }
134
+ ```
135
+
136
+ This way, `.` means "the full current pathname for my route" in all cases (including static, dynamic, and splat routes) and `..` always means "my parents pathname".
137
+
138
+ ### Patch Changes
139
+
140
+ - Properly handle falsy error values in ErrorBoundary's ([#11071](https://github.com/remix-run/react-router/pull/11071))
141
+ - Updated dependencies:
142
+ - `@remix-run/router@1.14.0-pre.0`
143
+
144
+ ## 6.20.1
4
145
 
5
146
  ### Patch Changes
6
147
 
7
148
  - Revert the `useResolvedPath` fix for splat routes due to a large number of applications that were relying on the buggy behavior (see https://github.com/remix-run/react-router/issues/11052#issuecomment-1836589329). We plan to re-introduce this fix behind a future flag in the next minor version. ([#11078](https://github.com/remix-run/react-router/pull/11078))
8
149
  - Updated dependencies:
9
- - `@remix-run/router@1.13.1-pre.0`
150
+ - `@remix-run/router@1.13.1`
10
151
 
11
152
  ## 6.20.0
12
153
 
@@ -144,7 +285,7 @@
144
285
 
145
286
  ## 6.12.1
146
287
 
147
- > **Warning**
288
+ > [!WARNING]
148
289
  > Please use version `6.13.0` or later instead of `6.12.1`. This version suffers from a `webpack`/`terser` minification issue resulting in invalid minified code in your resulting production bundles which can cause issues in your application. See [#10579](https://github.com/remix-run/react-router/issues/10579) for more details.
149
290
 
150
291
  ### Patch Changes
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * React Router v6.20.1-pre.0
2
+ * React Router v6.21.0-pre.0
3
3
  *
4
4
  * Copyright (c) Remix Software Inc.
5
5
  *
@@ -9,7 +9,7 @@
9
9
  * @license MIT
10
10
  */
11
11
  import * as React from 'react';
12
- import { UNSAFE_invariant, joinPaths, matchPath, UNSAFE_getPathContributingMatches, UNSAFE_warning, resolveTo, parsePath, matchRoutes, Action, UNSAFE_convertRouteMatchToUiMatch, stripBasename, IDLE_BLOCKER, isRouteErrorResponse, createMemoryHistory, AbortedDeferredError, createRouter } from '@remix-run/router';
12
+ import { UNSAFE_invariant, joinPaths, matchPath, UNSAFE_getResolveToMatches, UNSAFE_warning, resolveTo, parsePath, matchRoutes, Action, UNSAFE_convertRouteMatchToUiMatch, stripBasename, IDLE_BLOCKER, isRouteErrorResponse, createMemoryHistory, AbortedDeferredError, createRouter } from '@remix-run/router';
13
13
  export { AbortedDeferredError, Action as NavigationType, createPath, defer, generatePath, isRouteErrorResponse, json, matchPath, matchRoutes, parsePath, redirect, redirectDocument, resolvePath } from '@remix-run/router';
14
14
 
15
15
  function _extends() {
@@ -204,6 +204,7 @@ function useNavigateUnstable() {
204
204
  let dataRouterContext = React.useContext(DataRouterContext);
205
205
  let {
206
206
  basename,
207
+ future,
207
208
  navigator
208
209
  } = React.useContext(NavigationContext);
209
210
  let {
@@ -212,7 +213,7 @@ function useNavigateUnstable() {
212
213
  let {
213
214
  pathname: locationPathname
214
215
  } = useLocation();
215
- let routePathnamesJson = JSON.stringify(UNSAFE_getPathContributingMatches(matches).map(match => match.pathnameBase));
216
+ let routePathnamesJson = JSON.stringify(UNSAFE_getResolveToMatches(matches, future.v7_relativeSplatPath));
216
217
  let activeRef = React.useRef(false);
217
218
  useIsomorphicLayoutEffect(() => {
218
219
  activeRef.current = true;
@@ -295,13 +296,16 @@ function useResolvedPath(to, _temp2) {
295
296
  let {
296
297
  relative
297
298
  } = _temp2 === void 0 ? {} : _temp2;
299
+ let {
300
+ future
301
+ } = React.useContext(NavigationContext);
298
302
  let {
299
303
  matches
300
304
  } = React.useContext(RouteContext);
301
305
  let {
302
306
  pathname: locationPathname
303
307
  } = useLocation();
304
- let routePathnamesJson = JSON.stringify(UNSAFE_getPathContributingMatches(matches).map(match => match.pathnameBase));
308
+ let routePathnamesJson = JSON.stringify(UNSAFE_getResolveToMatches(matches, future.v7_relativeSplatPath));
305
309
  return React.useMemo(() => resolveTo(to, JSON.parse(routePathnamesJson), locationPathname, relative === "path"), [to, routePathnamesJson, locationPathname, relative]);
306
310
  }
307
311
 
@@ -318,7 +322,7 @@ function useRoutes(routes, locationArg) {
318
322
  }
319
323
 
320
324
  // Internal implementation with accept optional param for RouterProvider usage
321
- function useRoutesImpl(routes, locationArg, dataRouterState) {
325
+ function useRoutesImpl(routes, locationArg, dataRouterState, future) {
322
326
  !useInRouterContext() ? process.env.NODE_ENV !== "production" ? UNSAFE_invariant(false, // TODO: This error is probably because they somehow have 2 versions of the
323
327
  // router loaded. We can help them understand how to avoid that.
324
328
  "useRoutes() may be used only in the context of a <Router> component.") : UNSAFE_invariant(false) : void 0;
@@ -384,7 +388,7 @@ function useRoutesImpl(routes, locationArg, dataRouterState) {
384
388
  pathnameBase: match.pathnameBase === "/" ? parentPathnameBase : joinPaths([parentPathnameBase,
385
389
  // Re-encode pathnames that were decoded inside matchRoutes
386
390
  navigator.encodeLocation ? navigator.encodeLocation(match.pathnameBase).pathname : match.pathnameBase])
387
- })), parentMatches, dataRouterState);
391
+ })), parentMatches, dataRouterState, future);
388
392
 
389
393
  // When a user passes in a `locationArg`, the associated routes need to
390
394
  // be wrapped in a new `LocationContext.Provider` in order for `useLocation`
@@ -472,7 +476,7 @@ class RenderErrorBoundary extends React.Component {
472
476
  // this because the error provided from the app state may be cleared without
473
477
  // the location changing.
474
478
  return {
475
- error: props.error || state.error,
479
+ error: props.error !== undefined ? props.error : state.error,
476
480
  location: state.location,
477
481
  revalidation: props.revalidation || state.revalidation
478
482
  };
@@ -481,7 +485,7 @@ class RenderErrorBoundary extends React.Component {
481
485
  console.error("React Router caught the following error during render", error, errorInfo);
482
486
  }
483
487
  render() {
484
- return this.state.error ? /*#__PURE__*/React.createElement(RouteContext.Provider, {
488
+ return this.state.error !== undefined ? /*#__PURE__*/React.createElement(RouteContext.Provider, {
485
489
  value: this.props.routeContext
486
490
  }, /*#__PURE__*/React.createElement(RouteErrorContext.Provider, {
487
491
  value: this.state.error,
@@ -506,7 +510,7 @@ function RenderedRoute(_ref) {
506
510
  value: routeContext
507
511
  }, children);
508
512
  }
509
- function _renderMatches(matches, parentMatches, dataRouterState) {
513
+ function _renderMatches(matches, parentMatches, dataRouterState, future) {
510
514
  var _dataRouterState2;
511
515
  if (parentMatches === void 0) {
512
516
  parentMatches = [];
@@ -514,6 +518,9 @@ function _renderMatches(matches, parentMatches, dataRouterState) {
514
518
  if (dataRouterState === void 0) {
515
519
  dataRouterState = null;
516
520
  }
521
+ if (future === void 0) {
522
+ future = null;
523
+ }
517
524
  if (matches == null) {
518
525
  var _dataRouterState;
519
526
  if ((_dataRouterState = dataRouterState) != null && _dataRouterState.errors) {
@@ -533,18 +540,59 @@ function _renderMatches(matches, parentMatches, dataRouterState) {
533
540
  !(errorIndex >= 0) ? process.env.NODE_ENV !== "production" ? UNSAFE_invariant(false, "Could not find a matching route for errors on route IDs: " + Object.keys(errors).join(",")) : UNSAFE_invariant(false) : void 0;
534
541
  renderedMatches = renderedMatches.slice(0, Math.min(renderedMatches.length, errorIndex + 1));
535
542
  }
543
+
544
+ // If we're in a partial hydration mode, detect if we need to render down to
545
+ // a given HydrateFallback while we load the rest of the hydration data
546
+ let renderFallback = false;
547
+ let fallbackIndex = -1;
548
+ if (dataRouterState && future && future.v7_partialHydration) {
549
+ for (let i = 0; i < renderedMatches.length; i++) {
550
+ let match = renderedMatches[i];
551
+ // Track the deepest fallback up until the first route without data
552
+ if (match.route.HydrateFallback || match.route.hydrateFallbackElement) {
553
+ fallbackIndex = i;
554
+ }
555
+ if (match.route.loader && match.route.id && dataRouterState.loaderData[match.route.id] === undefined && (!dataRouterState.errors || dataRouterState.errors[match.route.id] === undefined)) {
556
+ // We found the first route without data/errors which means it's loader
557
+ // still needs to run. Flag that we need to render a fallback and
558
+ // render up until the appropriate fallback
559
+ renderFallback = true;
560
+ if (fallbackIndex >= 0) {
561
+ renderedMatches = renderedMatches.slice(0, fallbackIndex + 1);
562
+ } else {
563
+ renderedMatches = [renderedMatches[0]];
564
+ }
565
+ break;
566
+ }
567
+ }
568
+ }
536
569
  return renderedMatches.reduceRight((outlet, match, index) => {
537
- let error = match.route.id ? errors == null ? void 0 : errors[match.route.id] : null;
538
- // Only data routers handle errors
570
+ // Only data routers handle errors/fallbacks
571
+ let error;
572
+ let shouldRenderHydrateFallback = false;
539
573
  let errorElement = null;
574
+ let hydrateFallbackElement = null;
540
575
  if (dataRouterState) {
576
+ error = errors && match.route.id ? errors[match.route.id] : undefined;
541
577
  errorElement = match.route.errorElement || defaultErrorElement;
578
+ if (renderFallback) {
579
+ if (fallbackIndex < 0 && index === 0) {
580
+ warningOnce("route-fallback", false, "No `HydrateFallback` element provided to render during initial hydration");
581
+ shouldRenderHydrateFallback = true;
582
+ hydrateFallbackElement = null;
583
+ } else if (fallbackIndex === index) {
584
+ shouldRenderHydrateFallback = true;
585
+ hydrateFallbackElement = match.route.hydrateFallbackElement || null;
586
+ }
587
+ }
542
588
  }
543
589
  let matches = parentMatches.concat(renderedMatches.slice(0, index + 1));
544
590
  let getChildren = () => {
545
591
  let children;
546
592
  if (error) {
547
593
  children = errorElement;
594
+ } else if (shouldRenderHydrateFallback) {
595
+ children = hydrateFallbackElement;
548
596
  } else if (match.route.Component) {
549
597
  // Note: This is a de-optimized path since React won't re-use the
550
598
  // ReactElement since it's identity changes with each new
@@ -715,7 +763,7 @@ function useRouteError() {
715
763
 
716
764
  // If this was a render error, we put it in a RouteError context inside
717
765
  // of RenderErrorBoundary
718
- if (error) {
766
+ if (error !== undefined) {
719
767
  return error;
720
768
  }
721
769
 
@@ -891,6 +939,11 @@ function RouterProvider(_ref) {
891
939
  // Need to use a layout effect here so we are subscribed early enough to
892
940
  // pick up on any render-driven redirects/navigations (useEffect/<Navigate>)
893
941
  React.useLayoutEffect(() => router.subscribe(setState), [router, setState]);
942
+ React.useEffect(() => {
943
+ process.env.NODE_ENV !== "production" ? UNSAFE_warning(fallbackElement == null || !router.future.v7_partialHydration, "`<RouterProvider fallbackElement>` is deprecated when using `v7_partialHydration`") : void 0;
944
+ // Only log this once on initial mount
945
+ // eslint-disable-next-line react-hooks/exhaustive-deps
946
+ }, []);
894
947
  let navigator = React.useMemo(() => {
895
948
  return {
896
949
  createHref: router.createHref,
@@ -912,7 +965,10 @@ function RouterProvider(_ref) {
912
965
  router,
913
966
  navigator,
914
967
  static: false,
915
- basename
968
+ basename,
969
+ future: {
970
+ v7_relativeSplatPath: router.future.v7_relativeSplatPath
971
+ }
916
972
  }), [router, navigator, basename]);
917
973
 
918
974
  // The fragment and {null} here are important! We need them to keep React 18's
@@ -932,15 +988,17 @@ function RouterProvider(_ref) {
932
988
  navigator: navigator
933
989
  }, state.initialized ? /*#__PURE__*/React.createElement(DataRoutes, {
934
990
  routes: router.routes,
991
+ future: router.future,
935
992
  state: state
936
993
  }) : fallbackElement))), null);
937
994
  }
938
995
  function DataRoutes(_ref2) {
939
996
  let {
940
997
  routes,
998
+ future,
941
999
  state
942
1000
  } = _ref2;
943
- return useRoutesImpl(routes, undefined, state);
1001
+ return useRoutesImpl(routes, undefined, state, future);
944
1002
  }
945
1003
  /**
946
1004
  * A `<Router>` that stores all entries in memory.
@@ -980,7 +1038,8 @@ function MemoryRouter(_ref3) {
980
1038
  children: children,
981
1039
  location: state.location,
982
1040
  navigationType: state.action,
983
- navigator: history
1041
+ navigator: history,
1042
+ future: future
984
1043
  });
985
1044
  }
986
1045
  /**
@@ -1002,7 +1061,11 @@ function Navigate(_ref4) {
1002
1061
  !useInRouterContext() ? process.env.NODE_ENV !== "production" ? UNSAFE_invariant(false, // TODO: This error is probably because they somehow have 2 versions of
1003
1062
  // the router loaded. We can help them understand how to avoid that.
1004
1063
  "<Navigate> may be used only in the context of a <Router> component.") : UNSAFE_invariant(false) : void 0;
1005
- process.env.NODE_ENV !== "production" ? UNSAFE_warning(!React.useContext(NavigationContext).static, "<Navigate> must not be used on the initial render in a <StaticRouter>. " + "This is a no-op, but you should modify your code so the <Navigate> is " + "only ever rendered in response to some user interaction or state change.") : void 0;
1064
+ let {
1065
+ future,
1066
+ static: isStatic
1067
+ } = React.useContext(NavigationContext);
1068
+ process.env.NODE_ENV !== "production" ? UNSAFE_warning(!isStatic, "<Navigate> must not be used on the initial render in a <StaticRouter>. " + "This is a no-op, but you should modify your code so the <Navigate> is " + "only ever rendered in response to some user interaction or state change.") : void 0;
1006
1069
  let {
1007
1070
  matches
1008
1071
  } = React.useContext(RouteContext);
@@ -1013,7 +1076,7 @@ function Navigate(_ref4) {
1013
1076
 
1014
1077
  // Resolve the path outside of the effect so that when effects run twice in
1015
1078
  // StrictMode they navigate to the same place
1016
- let path = resolveTo(to, UNSAFE_getPathContributingMatches(matches).map(match => match.pathnameBase), locationPathname, relative === "path");
1079
+ let path = resolveTo(to, UNSAFE_getResolveToMatches(matches, future.v7_relativeSplatPath), locationPathname, relative === "path");
1017
1080
  let jsonPath = JSON.stringify(path);
1018
1081
  React.useEffect(() => navigate(JSON.parse(jsonPath), {
1019
1082
  replace,
@@ -1054,7 +1117,8 @@ function Router(_ref5) {
1054
1117
  location: locationProp,
1055
1118
  navigationType = Action.Pop,
1056
1119
  navigator,
1057
- static: staticProp = false
1120
+ static: staticProp = false,
1121
+ future
1058
1122
  } = _ref5;
1059
1123
  !!useInRouterContext() ? process.env.NODE_ENV !== "production" ? UNSAFE_invariant(false, "You cannot render a <Router> inside another <Router>." + " You should never have more than one in your app.") : UNSAFE_invariant(false) : void 0;
1060
1124
 
@@ -1064,8 +1128,11 @@ function Router(_ref5) {
1064
1128
  let navigationContext = React.useMemo(() => ({
1065
1129
  basename,
1066
1130
  navigator,
1067
- static: staticProp
1068
- }), [basename, navigator, staticProp]);
1131
+ static: staticProp,
1132
+ future: _extends({
1133
+ v7_relativeSplatPath: false
1134
+ }, future)
1135
+ }), [basename, future, navigator, staticProp]);
1069
1136
  if (typeof locationProp === "string") {
1070
1137
  locationProp = parsePath(locationProp);
1071
1138
  }
@@ -1317,6 +1384,17 @@ function mapRouteProperties(route) {
1317
1384
  Component: undefined
1318
1385
  });
1319
1386
  }
1387
+ if (route.HydrateFallback) {
1388
+ if (process.env.NODE_ENV !== "production") {
1389
+ if (route.hydrateFallbackElement) {
1390
+ process.env.NODE_ENV !== "production" ? UNSAFE_warning(false, "You should not include both `HydrateFallback` and `hydrateFallbackElement` on your route - " + "`HydrateFallback` will be used.") : void 0;
1391
+ }
1392
+ }
1393
+ Object.assign(updates, {
1394
+ hydrateFallbackElement: /*#__PURE__*/React.createElement(route.HydrateFallback),
1395
+ HydrateFallback: undefined
1396
+ });
1397
+ }
1320
1398
  if (route.ErrorBoundary) {
1321
1399
  if (process.env.NODE_ENV !== "production") {
1322
1400
  if (route.errorElement) {