react-router 0.0.0-experimental-35fa15e5 → 0.0.0-experimental-8bb3ffdf

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,168 @@
1
1
  # `react-router`
2
2
 
3
+ ## 6.21.0
4
+
5
+ ### Minor Changes
6
+
7
+ - Add a new `future.v7_relativeSplatPath` flag to implement 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/pull/11078) 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 path 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`
143
+
144
+ ## 6.20.1
145
+
146
+ ### Patch Changes
147
+
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))
149
+ - Updated dependencies:
150
+ - `@remix-run/router@1.13.1`
151
+
152
+ ## 6.20.0
153
+
154
+ ### Minor Changes
155
+
156
+ - Export the `PathParam` type from the public API ([#10719](https://github.com/remix-run/react-router/pull/10719))
157
+
158
+ ### Patch Changes
159
+
160
+ - Fix bug with `resolveTo` in splat routes ([#11045](https://github.com/remix-run/react-router/pull/11045))
161
+ - This is a follow up to [#10983](https://github.com/remix-run/react-router/pull/10983) to handle the few other code paths using `getPathContributingMatches`
162
+ - This removes the `UNSAFE_getPathContributingMatches` export from `@remix-run/router` since we no longer need this in the `react-router`/`react-router-dom` layers
163
+ - Updated dependencies:
164
+ - `@remix-run/router@1.13.0`
165
+
3
166
  ## 6.19.0
4
167
 
5
168
  ### Minor Changes
@@ -122,7 +285,7 @@
122
285
 
123
286
  ## 6.12.1
124
287
 
125
- > **Warning**
288
+ > [!WARNING]
126
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.
127
290
 
128
291
  ### Patch Changes
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * React Router v0.0.0-experimental-35fa15e5
2
+ * React Router v0.0.0-experimental-8bb3ffdf
3
3
  *
4
4
  * Copyright (c) Remix Software Inc.
5
5
  *
@@ -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_getResolveToMatches(matches));
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_getResolveToMatches(matches));
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
 
@@ -374,7 +378,7 @@ function useRoutesImpl(routes, locationArg, dataRouterState, future) {
374
378
  });
375
379
  if (process.env.NODE_ENV !== "production") {
376
380
  process.env.NODE_ENV !== "production" ? UNSAFE_warning(parentRoute || matches != null, "No routes matched location \"" + location.pathname + location.search + location.hash + "\" ") : void 0;
377
- process.env.NODE_ENV !== "production" ? UNSAFE_warning(matches == null || matches[matches.length - 1].route.element !== undefined || matches[matches.length - 1].route.Component !== undefined, "Matched leaf route at location \"" + location.pathname + location.search + location.hash + "\" " + "does not have an element or Component. This means it will render an <Outlet /> with a " + "null value by default resulting in an \"empty\" page.") : void 0;
381
+ process.env.NODE_ENV !== "production" ? UNSAFE_warning(matches == null || matches[matches.length - 1].route.element !== undefined || matches[matches.length - 1].route.Component !== undefined || matches[matches.length - 1].route.lazy !== undefined, "Matched leaf route at location \"" + location.pathname + location.search + location.hash + "\" " + "does not have an element or Component. This means it will render an <Outlet /> with a " + "null value by default resulting in an \"empty\" page.") : void 0;
378
382
  }
379
383
  let renderedMatches = _renderMatches(matches && matches.map(match => Object.assign({}, match, {
380
384
  params: Object.assign({}, parentParams, match.params),
@@ -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,
@@ -548,17 +552,24 @@ function _renderMatches(matches, parentMatches, dataRouterState, future) {
548
552
  if (match.route.HydrateFallback || match.route.hydrateFallbackElement) {
549
553
  fallbackIndex = i;
550
554
  }
551
- if (match.route.loader && match.route.id && dataRouterState.loaderData[match.route.id] === undefined && (!dataRouterState.errors || dataRouterState.errors[match.route.id] === undefined)) {
552
- // We found the first route without data/errors which means it's loader
553
- // still needs to run. Flag that we need to render a fallback and
554
- // render up until the appropriate fallback
555
- renderFallback = true;
556
- if (fallbackIndex >= 0) {
557
- renderedMatches = renderedMatches.slice(0, fallbackIndex + 1);
558
- } else {
559
- renderedMatches = [renderedMatches[0]];
555
+ if (match.route.id) {
556
+ let {
557
+ loaderData,
558
+ errors
559
+ } = dataRouterState;
560
+ let needsToRunLoader = match.route.loader && loaderData[match.route.id] === undefined && (!errors || errors[match.route.id] === undefined);
561
+ if (match.route.lazy || needsToRunLoader) {
562
+ // We found the first route that's not ready to render (waiting on
563
+ // lazy, or has a loader that hasn't run yet). Flag that we need to
564
+ // render a fallback and render up until the appropriate fallback
565
+ renderFallback = true;
566
+ if (fallbackIndex >= 0) {
567
+ renderedMatches = renderedMatches.slice(0, fallbackIndex + 1);
568
+ } else {
569
+ renderedMatches = [renderedMatches[0]];
570
+ }
571
+ break;
560
572
  }
561
- break;
562
573
  }
563
574
  }
564
575
  }
@@ -569,7 +580,7 @@ function _renderMatches(matches, parentMatches, dataRouterState, future) {
569
580
  let errorElement = null;
570
581
  let hydrateFallbackElement = null;
571
582
  if (dataRouterState) {
572
- error = errors && match.route.id ? errors[match.route.id] : null;
583
+ error = errors && match.route.id ? errors[match.route.id] : undefined;
573
584
  errorElement = match.route.errorElement || defaultErrorElement;
574
585
  if (renderFallback) {
575
586
  if (fallbackIndex < 0 && index === 0) {
@@ -759,7 +770,7 @@ function useRouteError() {
759
770
 
760
771
  // If this was a render error, we put it in a RouteError context inside
761
772
  // of RenderErrorBoundary
762
- if (error) {
773
+ if (error !== undefined) {
763
774
  return error;
764
775
  }
765
776
 
@@ -936,7 +947,7 @@ function RouterProvider(_ref) {
936
947
  // pick up on any render-driven redirects/navigations (useEffect/<Navigate>)
937
948
  React.useLayoutEffect(() => router.subscribe(setState), [router, setState]);
938
949
  React.useEffect(() => {
939
- process.env.NODE_ENV !== "production" ? UNSAFE_warning(fallbackElement == null || !router.future.v7_partialHydration, "`<RouterProvider fallbackElement>` is deprecated when using `v7_partialHydration`") : void 0;
950
+ process.env.NODE_ENV !== "production" ? UNSAFE_warning(fallbackElement == null || !router.future.v7_partialHydration, "`<RouterProvider fallbackElement>` is deprecated when using " + "`v7_partialHydration`, use a `HydrateFallback` component instead") : void 0;
940
951
  // Only log this once on initial mount
941
952
  // eslint-disable-next-line react-hooks/exhaustive-deps
942
953
  }, []);
@@ -978,8 +989,11 @@ function RouterProvider(_ref) {
978
989
  basename: basename,
979
990
  location: state.location,
980
991
  navigationType: state.historyAction,
981
- navigator: navigator
982
- }, state.initialized ? /*#__PURE__*/React.createElement(DataRoutes, {
992
+ navigator: navigator,
993
+ future: {
994
+ v7_relativeSplatPath: router.future.v7_relativeSplatPath
995
+ }
996
+ }, state.initialized || router.future.v7_partialHydration ? /*#__PURE__*/React.createElement(DataRoutes, {
983
997
  routes: router.routes,
984
998
  future: router.future,
985
999
  state: state
@@ -1031,7 +1045,8 @@ function MemoryRouter(_ref3) {
1031
1045
  children: children,
1032
1046
  location: state.location,
1033
1047
  navigationType: state.action,
1034
- navigator: history
1048
+ navigator: history,
1049
+ future: future
1035
1050
  });
1036
1051
  }
1037
1052
  /**
@@ -1053,7 +1068,11 @@ function Navigate(_ref4) {
1053
1068
  !useInRouterContext() ? process.env.NODE_ENV !== "production" ? UNSAFE_invariant(false, // TODO: This error is probably because they somehow have 2 versions of
1054
1069
  // the router loaded. We can help them understand how to avoid that.
1055
1070
  "<Navigate> may be used only in the context of a <Router> component.") : UNSAFE_invariant(false) : void 0;
1056
- 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;
1071
+ let {
1072
+ future,
1073
+ static: isStatic
1074
+ } = React.useContext(NavigationContext);
1075
+ 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;
1057
1076
  let {
1058
1077
  matches
1059
1078
  } = React.useContext(RouteContext);
@@ -1064,7 +1083,7 @@ function Navigate(_ref4) {
1064
1083
 
1065
1084
  // Resolve the path outside of the effect so that when effects run twice in
1066
1085
  // StrictMode they navigate to the same place
1067
- let path = resolveTo(to, UNSAFE_getResolveToMatches(matches), locationPathname, relative === "path");
1086
+ let path = resolveTo(to, UNSAFE_getResolveToMatches(matches, future.v7_relativeSplatPath), locationPathname, relative === "path");
1068
1087
  let jsonPath = JSON.stringify(path);
1069
1088
  React.useEffect(() => navigate(JSON.parse(jsonPath), {
1070
1089
  replace,
@@ -1105,7 +1124,8 @@ function Router(_ref5) {
1105
1124
  location: locationProp,
1106
1125
  navigationType = Action.Pop,
1107
1126
  navigator,
1108
- static: staticProp = false
1127
+ static: staticProp = false,
1128
+ future
1109
1129
  } = _ref5;
1110
1130
  !!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;
1111
1131
 
@@ -1115,8 +1135,11 @@ function Router(_ref5) {
1115
1135
  let navigationContext = React.useMemo(() => ({
1116
1136
  basename,
1117
1137
  navigator,
1118
- static: staticProp
1119
- }), [basename, navigator, staticProp]);
1138
+ static: staticProp,
1139
+ future: _extends({
1140
+ v7_relativeSplatPath: false
1141
+ }, future)
1142
+ }), [basename, future, navigator, staticProp]);
1120
1143
  if (typeof locationProp === "string") {
1121
1144
  locationProp = parsePath(locationProp);
1122
1145
  }