react-router 7.15.1 → 7.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/CHANGELOG.md +27 -2
- package/dist/development/{browser-3AnU12UI.d.mts → browser-CGcs-0pD.d.mts} +2 -2
- package/dist/development/{browser-BOdXz9dK.d.ts → browser-D3uq9sI1.d.ts} +2 -2
- package/dist/development/{chunk-4N6VE7H7.mjs → chunk-6CSD65Y2.mjs} +12 -11
- package/dist/development/{chunk-RJYABSBD.mjs → chunk-ASILSGTR.mjs} +6 -6
- package/dist/development/{chunk-D6LUOGOQ.js → chunk-KFNXW4AL.js} +10 -10
- package/dist/{production/chunk-Y6IFXO7V.js → development/chunk-PBLBZ3QU.js} +7 -7
- package/dist/development/{chunk-66UKHEGQ.js → chunk-PULC7NLK.js} +100 -99
- package/dist/{production/context-ByvtofY2.d.mts → development/context-CmHpk1Ws.d.mts} +3 -3
- package/dist/development/{data-BqZ2x964.d.ts → data-D4xhSy90.d.ts} +1 -1
- package/dist/{production/data-BVUf681J.d.mts → development/data-U8FS-wNn.d.mts} +1 -1
- package/dist/development/dom-export.d.mts +4 -4
- package/dist/development/dom-export.d.ts +4 -4
- package/dist/development/dom-export.js +30 -30
- package/dist/development/dom-export.mjs +5 -5
- package/dist/{production/index-react-server-client-BS5F89FR.d.ts → development/index-react-server-client-CwU9bE5R.d.ts} +4 -4
- package/dist/{production/index-react-server-client-DY04-103.d.mts → development/index-react-server-client-DPrDrCew.d.mts} +3 -3
- package/dist/development/index-react-server-client.d.mts +3 -3
- package/dist/development/index-react-server-client.d.ts +3 -3
- package/dist/development/index-react-server-client.js +4 -4
- package/dist/development/index-react-server-client.mjs +2 -2
- package/dist/development/index-react-server.d.mts +1 -1
- package/dist/development/index-react-server.d.ts +1 -1
- package/dist/development/index-react-server.js +1 -1
- package/dist/development/index-react-server.mjs +1 -1
- package/dist/development/index.d.mts +18 -12
- package/dist/development/index.d.ts +18 -12
- package/dist/development/index.js +89 -89
- package/dist/development/index.mjs +3 -3
- package/dist/{production/instrumentation-cRWWLfsU.d.ts → development/instrumentation-1q4YhLGP.d.ts} +2 -2
- package/dist/development/lib/types/internal.d.mts +2 -2
- package/dist/development/lib/types/internal.d.ts +2 -2
- package/dist/development/lib/types/internal.js +1 -1
- package/dist/development/lib/types/internal.mjs +1 -1
- package/dist/development/{register-Bsscfj79.d.ts → register-CNAx3TXj.d.ts} +1 -1
- package/dist/development/{register-Df8okEea.d.mts → register-CqK96Zfk.d.mts} +1 -1
- package/dist/production/{browser-3AnU12UI.d.mts → browser-CGcs-0pD.d.mts} +2 -2
- package/dist/production/{browser-BOdXz9dK.d.ts → browser-D3uq9sI1.d.ts} +2 -2
- package/dist/production/{chunk-6S4627ZB.mjs → chunk-5TQZEVD5.mjs} +6 -6
- package/dist/{development/chunk-4YRVXM2U.js → production/chunk-CTIXC7EV.js} +7 -7
- package/dist/production/{chunk-PNZCCTKT.js → chunk-EN242BO4.js} +100 -99
- package/dist/production/{chunk-JAKZPQZC.mjs → chunk-OSYEOCBT.mjs} +12 -11
- package/dist/production/{chunk-HUBUW7R3.js → chunk-RTRY3JFT.js} +10 -10
- package/dist/{development/context-ByvtofY2.d.mts → production/context-CmHpk1Ws.d.mts} +3 -3
- package/dist/production/{data-BqZ2x964.d.ts → data-D4xhSy90.d.ts} +1 -1
- package/dist/{development/data-BVUf681J.d.mts → production/data-U8FS-wNn.d.mts} +1 -1
- package/dist/production/dom-export.d.mts +4 -4
- package/dist/production/dom-export.d.ts +4 -4
- package/dist/production/dom-export.js +30 -30
- package/dist/production/dom-export.mjs +5 -5
- package/dist/{development/index-react-server-client-BS5F89FR.d.ts → production/index-react-server-client-CwU9bE5R.d.ts} +4 -4
- package/dist/{development/index-react-server-client-DY04-103.d.mts → production/index-react-server-client-DPrDrCew.d.mts} +3 -3
- package/dist/production/index-react-server-client.d.mts +3 -3
- package/dist/production/index-react-server-client.d.ts +3 -3
- package/dist/production/index-react-server-client.js +4 -4
- package/dist/production/index-react-server-client.mjs +2 -2
- package/dist/production/index-react-server.d.mts +1 -1
- package/dist/production/index-react-server.d.ts +1 -1
- package/dist/production/index-react-server.js +1 -1
- package/dist/production/index-react-server.mjs +1 -1
- package/dist/production/index.d.mts +18 -12
- package/dist/production/index.d.ts +18 -12
- package/dist/production/index.js +89 -89
- package/dist/production/index.mjs +3 -3
- package/dist/{development/instrumentation-cRWWLfsU.d.ts → production/instrumentation-1q4YhLGP.d.ts} +2 -2
- package/dist/production/lib/types/internal.d.mts +2 -2
- package/dist/production/lib/types/internal.d.ts +2 -2
- package/dist/production/lib/types/internal.js +1 -1
- package/dist/production/lib/types/internal.mjs +1 -1
- package/dist/production/{register-Bsscfj79.d.ts → register-CNAx3TXj.d.ts} +1 -1
- package/dist/production/{register-Df8okEea.d.mts → register-CqK96Zfk.d.mts} +1 -1
- package/docs/explanation/backend-for-frontend.md +50 -0
- package/docs/explanation/code-splitting.md +61 -0
- package/docs/explanation/concurrency.md +135 -0
- package/docs/explanation/form-vs-fetcher.md +292 -0
- package/docs/explanation/hot-module-replacement.md +137 -0
- package/docs/explanation/hydration.md +14 -0
- package/docs/explanation/index-query-param.md +86 -0
- package/docs/explanation/index.md +4 -0
- package/docs/explanation/lazy-route-discovery.md +78 -0
- package/docs/explanation/location.md +6 -0
- package/docs/explanation/progressive-enhancement.md +150 -0
- package/docs/explanation/race-conditions.md +88 -0
- package/docs/explanation/react-transitions.md +160 -0
- package/docs/explanation/route-matching.md +7 -0
- package/docs/explanation/server-client-execution.md +4 -0
- package/docs/explanation/sessions-and-cookies.md +465 -0
- package/docs/explanation/special-files.md +16 -0
- package/docs/explanation/state-management.md +524 -0
- package/docs/explanation/styling.md +87 -0
- package/docs/explanation/type-safety.md +82 -0
- package/docs/how-to/accessibility.md +44 -0
- package/docs/how-to/client-data.md +199 -0
- package/docs/how-to/data-strategy.md +317 -0
- package/docs/how-to/error-boundary.md +231 -0
- package/docs/how-to/error-reporting.md +142 -0
- package/docs/how-to/fetchers.md +307 -0
- package/docs/how-to/file-route-conventions.md +410 -0
- package/docs/how-to/file-uploads.md +217 -0
- package/docs/how-to/form-validation.md +120 -0
- package/docs/how-to/headers.md +164 -0
- package/docs/how-to/index.md +4 -0
- package/docs/how-to/instrumentation.md +556 -0
- package/docs/how-to/meta.md +40 -0
- package/docs/how-to/middleware.md +763 -0
- package/docs/how-to/navigation-blocking.md +233 -0
- package/docs/how-to/optimize-revalidation.md +12 -0
- package/docs/how-to/pre-rendering.md +225 -0
- package/docs/how-to/presets.md +103 -0
- package/docs/how-to/react-server-components.md +899 -0
- package/docs/how-to/resource-routes.md +126 -0
- package/docs/how-to/route-module-type-safety.md +100 -0
- package/docs/how-to/search-params.md +4 -0
- package/docs/how-to/security.md +30 -0
- package/docs/how-to/server-bundles.md +66 -0
- package/docs/how-to/spa.md +120 -0
- package/docs/how-to/status.md +63 -0
- package/docs/how-to/suspense.md +132 -0
- package/docs/how-to/using-handle.md +117 -0
- package/docs/how-to/view-transitions.md +237 -0
- package/docs/how-to/webhook.md +50 -0
- package/docs/index.md +39 -0
- package/docs/start/data/actions.md +138 -0
- package/docs/start/data/custom.md +198 -0
- package/docs/start/data/data-loading.md +44 -0
- package/docs/start/data/index.md +4 -0
- package/docs/start/data/installation.md +52 -0
- package/docs/start/data/navigating.md +12 -0
- package/docs/start/data/pending-ui.md +12 -0
- package/docs/start/data/route-object.md +268 -0
- package/docs/start/data/routing.md +281 -0
- package/docs/start/data/testing.md +8 -0
- package/docs/start/declarative/index.md +4 -0
- package/docs/start/declarative/installation.md +43 -0
- package/docs/start/declarative/navigating.md +133 -0
- package/docs/start/declarative/routing.md +237 -0
- package/docs/start/declarative/url-values.md +65 -0
- package/docs/start/framework/actions.md +174 -0
- package/docs/start/framework/data-loading.md +201 -0
- package/docs/start/framework/deploying.md +96 -0
- package/docs/start/framework/index.md +4 -0
- package/docs/start/framework/installation.md +41 -0
- package/docs/start/framework/navigating.md +182 -0
- package/docs/start/framework/pending-ui.md +142 -0
- package/docs/start/framework/rendering.md +59 -0
- package/docs/start/framework/route-module.md +527 -0
- package/docs/start/framework/routing.md +362 -0
- package/docs/start/framework/testing.md +133 -0
- package/docs/start/index.md +4 -0
- package/docs/start/modes.md +201 -0
- package/docs/upgrading/component-routes.md +363 -0
- package/docs/upgrading/future.md +280 -0
- package/docs/upgrading/index.md +4 -0
- package/docs/upgrading/remix.md +403 -0
- package/docs/upgrading/router-provider.md +442 -0
- package/docs/upgrading/v6.md +382 -0
- package/package.json +2 -1
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Hot Module Replacement
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
# Hot Module Replacement
|
|
6
|
+
|
|
7
|
+
[MODES: framework]
|
|
8
|
+
|
|
9
|
+
<br/>
|
|
10
|
+
<br/>
|
|
11
|
+
|
|
12
|
+
Hot Module Replacement is a technique for updating modules in your app without needing to reload the page.
|
|
13
|
+
It's a great developer experience, and React Router supports it when using Vite.
|
|
14
|
+
|
|
15
|
+
HMR does its best to preserve browser state across updates.
|
|
16
|
+
For example, let's say you have form within a modal and you fill out all the fields.
|
|
17
|
+
As soon as you save any changes to the code, traditional live reload would hard refresh the page causing all of those fields to be reset.
|
|
18
|
+
Every time you make a change, you'd have to open up the modal _again_ and fill out the form _again_.
|
|
19
|
+
|
|
20
|
+
But with HMR, all of that state is preserved _across updates_.
|
|
21
|
+
|
|
22
|
+
## React Fast Refresh
|
|
23
|
+
|
|
24
|
+
React already has mechanisms for updating the DOM via its [virtual DOM][virtual-dom] in response to user interactions like clicking a button.
|
|
25
|
+
Wouldn't it be great if React could handle updating the DOM in response to code changes too?
|
|
26
|
+
|
|
27
|
+
That's exactly what [React Fast Refresh][react-refresh] is all about!
|
|
28
|
+
Of course, React is all about components, not general JavaScript code, so React Fast Refresh only handles hot updates for exported React components.
|
|
29
|
+
|
|
30
|
+
But React Fast Refresh does have some limitations that you should be aware of.
|
|
31
|
+
|
|
32
|
+
### Class Component State
|
|
33
|
+
|
|
34
|
+
React Fast Refresh does not preserve state for class components.
|
|
35
|
+
This includes higher-order components that internally return classes:
|
|
36
|
+
|
|
37
|
+
```tsx
|
|
38
|
+
export class ComponentA extends Component {} // ❌
|
|
39
|
+
|
|
40
|
+
export const ComponentB = HOC(ComponentC); // ❌ Won't work if HOC returns a class component
|
|
41
|
+
|
|
42
|
+
export function ComponentD() {} // ✅
|
|
43
|
+
export const ComponentE = () => {}; // ✅
|
|
44
|
+
export default function ComponentF() {} // ✅
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Named Function Components
|
|
48
|
+
|
|
49
|
+
Function components must be named, not anonymous, for React Fast Refresh to track changes:
|
|
50
|
+
|
|
51
|
+
```tsx
|
|
52
|
+
export default () => {}; // ❌
|
|
53
|
+
export default function () {} // ❌
|
|
54
|
+
|
|
55
|
+
const ComponentA = () => {};
|
|
56
|
+
export default ComponentA; // ✅
|
|
57
|
+
|
|
58
|
+
export default function ComponentB() {} // ✅
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Supported Exports
|
|
62
|
+
|
|
63
|
+
React Fast Refresh can only handle component exports. While React Router manages [route exports like `action`, ` headers`, `links`, `loader`, and `meta`][route-module] for you, any user-defined exports will cause full reloads:
|
|
64
|
+
|
|
65
|
+
```tsx
|
|
66
|
+
// These exports are handled by the React Router Vite plugin
|
|
67
|
+
// to be HMR-compatible
|
|
68
|
+
export const meta = { title: "Home" }; // ✅
|
|
69
|
+
export const links = [
|
|
70
|
+
{ rel: "stylesheet", href: "style.css" },
|
|
71
|
+
]; // ✅
|
|
72
|
+
|
|
73
|
+
// These exports are removed by the React Router Vite plugin
|
|
74
|
+
// so they never affect HMR
|
|
75
|
+
export const headers = { "Cache-Control": "max-age=3600" }; // ✅
|
|
76
|
+
export const loader = async () => {}; // ✅
|
|
77
|
+
export const action = async () => {}; // ✅
|
|
78
|
+
|
|
79
|
+
// This is not a route module export, nor a component export,
|
|
80
|
+
// so it will cause a full reload for this route
|
|
81
|
+
export const myValue = "some value"; // ❌
|
|
82
|
+
|
|
83
|
+
export default function Route() {} // ✅
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
👆 Routes probably shouldn't be exporting random values like that anyway.
|
|
87
|
+
If you want to reuse values across routes, stick them in their own non-route module:
|
|
88
|
+
|
|
89
|
+
```ts filename=my-custom-value.ts
|
|
90
|
+
export const myValue = "some value";
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### Changing Hooks
|
|
94
|
+
|
|
95
|
+
React Fast Refresh cannot track changes for a component when hooks are being added or removed from it, causing full reloads just for the next render. After the hooks have been updated, changes should result in hot updates again. For example, if you add a `useState` to your component, you may lose that component's local state for the next render.
|
|
96
|
+
|
|
97
|
+
Additionally, if you are destructuring a hook's return value, React Fast Refresh will not be able to preserve state for the component if the destructured key is removed or renamed.
|
|
98
|
+
For example:
|
|
99
|
+
|
|
100
|
+
```tsx
|
|
101
|
+
export default function Component({ loaderData }) {
|
|
102
|
+
const { pet } = useMyCustomHook();
|
|
103
|
+
return (
|
|
104
|
+
<div>
|
|
105
|
+
<input />
|
|
106
|
+
<p>My dog's name is {pet.name}!</p>
|
|
107
|
+
</div>
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
If you change the key `pet` to `dog`:
|
|
113
|
+
|
|
114
|
+
```diff
|
|
115
|
+
export default function Component() {
|
|
116
|
+
- const { pet } = useMyCustomHook();
|
|
117
|
+
+ const { dog } = useMyCustomHook();
|
|
118
|
+
return (
|
|
119
|
+
<div>
|
|
120
|
+
<input />
|
|
121
|
+
- <p>My dog's name is {pet.name}!</p>
|
|
122
|
+
+ <p>My dog's name is {dog.name}!</p>
|
|
123
|
+
</div>
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
then React Fast Refresh will not be able to preserve state `<input />` ❌.
|
|
129
|
+
|
|
130
|
+
### Component Keys
|
|
131
|
+
|
|
132
|
+
In some cases, React cannot distinguish between existing components being changed and new components being added. [React needs `key`s][react-keys] to disambiguate these cases and track changes when sibling elements are modified.
|
|
133
|
+
|
|
134
|
+
[virtual-dom]: https://reactjs.org/docs/faq-internals.html#what-is-the-virtual-dom
|
|
135
|
+
[react-refresh]: https://github.com/facebook/react/tree/main/packages/react-refresh
|
|
136
|
+
[react-keys]: https://react.dev/learn/rendering-lists#why-does-react-need-keys
|
|
137
|
+
[route-module]: ../start/framework/route-module
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Hydration
|
|
3
|
+
hidden: true
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
There are a few nuances worth noting around the behavior of `HydrateFallback`:
|
|
7
|
+
|
|
8
|
+
- It is only relevant on initial document request and hydration, and will not be rendered on any subsequent client-side navigations
|
|
9
|
+
- It is only relevant when you are also setting [`clientLoader.hydrate=true`][hydrate-true] on a given route
|
|
10
|
+
- It is also relevant if you do have a `clientLoader` without a server `loader`, as this implies `clientLoader.hydrate=true` since there is otherwise no loader data at all to return from `useLoaderData`
|
|
11
|
+
- Even if you do not specify a `HydrateFallback` in this case, React Router will not render your route component and will bubble up to any ancestor `HydrateFallback` component
|
|
12
|
+
- This is to ensure that `useLoaderData` remains "happy-path"
|
|
13
|
+
- Without a server `loader`, `useLoaderData` would return `undefined` in any rendered route components
|
|
14
|
+
- You cannot render an `<Outlet/>` in a `HydrateFallback` because children routes can't be guaranteed to operate correctly since their ancestor loader data may not yet be available if they are running `clientLoader` functions on hydration (i.e., use cases such as `useRouteLoaderData()` or `useMatches()`)
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Index Query Param
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
# Index Query Param
|
|
6
|
+
|
|
7
|
+
[MODES: framework, data]
|
|
8
|
+
|
|
9
|
+
## Overview
|
|
10
|
+
|
|
11
|
+
You may find a wild `?index` appearing in the URL of your app when submitting forms.
|
|
12
|
+
|
|
13
|
+
Because of nested routes, multiple routes in your route hierarchy can match the URL. Unlike navigations where all matching route [`loader`][loader]s are called to build up the UI, when a [`form`][form_element] is submitted, _only one action is called_.
|
|
14
|
+
|
|
15
|
+
Because index routes share the same URL as their parent, the `?index` param lets you disambiguate between the two.
|
|
16
|
+
|
|
17
|
+
## Understanding Index Routes
|
|
18
|
+
|
|
19
|
+
For example, consider the following route structure:
|
|
20
|
+
|
|
21
|
+
```ts filename=app/routes.ts
|
|
22
|
+
import {
|
|
23
|
+
type RouteConfig,
|
|
24
|
+
route,
|
|
25
|
+
index,
|
|
26
|
+
} from "@react-router/dev/routes";
|
|
27
|
+
|
|
28
|
+
export default [
|
|
29
|
+
route("projects", "./pages/projects.tsx", [
|
|
30
|
+
index("./pages/projects/index.tsx"),
|
|
31
|
+
route(":id", "./pages/projects/project.tsx"),
|
|
32
|
+
]),
|
|
33
|
+
] satisfies RouteConfig;
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
This creates two routes that match `/projects`:
|
|
37
|
+
|
|
38
|
+
- The parent route (`./pages/projects.tsx`)
|
|
39
|
+
- The index route (`./pages/projects/index.tsx`)
|
|
40
|
+
|
|
41
|
+
## Form Submission Targeting
|
|
42
|
+
|
|
43
|
+
For example, consider the following forms:
|
|
44
|
+
|
|
45
|
+
```tsx
|
|
46
|
+
<Form method="post" action="/projects" />
|
|
47
|
+
<Form method="post" action="/projects?index" />
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
The `?index` param will submit to the index route; the action without the index param will submit to the parent route.
|
|
51
|
+
|
|
52
|
+
When a [`<Form>`][form_component] is rendered in an index route without an [`action`][action], the `?index` param will automatically be appended so that the form posts to the index route. The following form, when submitted, will post to `/projects?index` because it is rendered in the context of the `projects` index route:
|
|
53
|
+
|
|
54
|
+
```tsx filename=app/pages/projects/index.tsx
|
|
55
|
+
function ProjectsIndex() {
|
|
56
|
+
return <Form method="post" />;
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
If you moved the code to the project layout (`./pages/projects.tsx` in this example), it would instead post to `/projects`.
|
|
61
|
+
|
|
62
|
+
This applies to `<Form>` and all of its cousins:
|
|
63
|
+
|
|
64
|
+
```tsx
|
|
65
|
+
function Component() {
|
|
66
|
+
const submit = useSubmit();
|
|
67
|
+
submit({}, { action: "/projects" });
|
|
68
|
+
submit({}, { action: "/projects?index" });
|
|
69
|
+
}
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
```tsx
|
|
73
|
+
function Component() {
|
|
74
|
+
const fetcher = useFetcher();
|
|
75
|
+
fetcher.submit({}, { action: "/projects" });
|
|
76
|
+
fetcher.submit({}, { action: "/projects?index" });
|
|
77
|
+
<fetcher.Form action="/projects" />;
|
|
78
|
+
<fetcher.Form action="/projects?index" />;
|
|
79
|
+
<fetcher.Form />; // defaults to the route in context
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
[loader]: ../start/data/route-object#loader
|
|
84
|
+
[form_element]: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form
|
|
85
|
+
[form_component]: ../api/components/Form
|
|
86
|
+
[action]: ../start/data/route-object#action
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Lazy Route Discovery
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
# Lazy Route Discovery
|
|
6
|
+
|
|
7
|
+
[MODES: framework]
|
|
8
|
+
|
|
9
|
+
<br/>
|
|
10
|
+
<br/>
|
|
11
|
+
|
|
12
|
+
Lazy Route Discovery is a performance optimization that loads route information progressively as users navigate through your application, rather than loading the complete route manifest upfront.
|
|
13
|
+
|
|
14
|
+
With Lazy Route Discovery enabled (the default), React Router sends only the routes needed for the initial server-side render in the manifest. As users navigate to new parts of your application, additional route information is fetched dynamically and added to the client-side manifest.
|
|
15
|
+
|
|
16
|
+
The route manifest contains metadata about your routes (JavaScript/CSS imports, whether routes have `loaders`/`actions`, etc.) but not the actual route module implementations. This allows React Router to understand your application's structure without downloading unnecessary route information.
|
|
17
|
+
|
|
18
|
+
## Route Discovery Process
|
|
19
|
+
|
|
20
|
+
When a user navigates to a new route that isn't in the current manifest:
|
|
21
|
+
|
|
22
|
+
1. **Route Discovery Request** - React Router makes a request to the internal `/__manifest` endpoint
|
|
23
|
+
2. **Manifest Patch** - The server responds with the required route information
|
|
24
|
+
3. **Route Loading** - React Router loads the necessary route modules and data
|
|
25
|
+
4. **Navigation** - The user navigates to the new route
|
|
26
|
+
|
|
27
|
+
## Eager Discovery Optimization
|
|
28
|
+
|
|
29
|
+
To prevent navigation waterfalls, React Router implements eager route discovery. All [`<Link>`](../api/components/Link) and [`<NavLink>`](../api/components/NavLink) components rendered on the current page are automatically discovered via a batched request to the server.
|
|
30
|
+
|
|
31
|
+
This discovery request typically completes before users click any links, making subsequent navigation feel synchronous even with lazy route discovery enabled.
|
|
32
|
+
|
|
33
|
+
```tsx
|
|
34
|
+
// Links are automatically discovered by default
|
|
35
|
+
<Link to="/dashboard">Dashboard</Link>
|
|
36
|
+
|
|
37
|
+
// Opt out of discovery for specific links
|
|
38
|
+
<Link to="/admin" discover="none">Admin</Link>
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Performance Benefits
|
|
42
|
+
|
|
43
|
+
Lazy Route Discovery provides several performance improvements:
|
|
44
|
+
|
|
45
|
+
- **Faster Initial Load** - Smaller initial bundle size by excluding unused route metadata
|
|
46
|
+
- **Reduced Memory Usage** - Route information is loaded only when needed
|
|
47
|
+
- **Scalability** - Applications with hundreds of routes see more significant benefits
|
|
48
|
+
|
|
49
|
+
## Configuration
|
|
50
|
+
|
|
51
|
+
You can configure route discovery behavior in your `react-router.config.ts`:
|
|
52
|
+
|
|
53
|
+
```tsx filename=react-router.config.ts
|
|
54
|
+
export default {
|
|
55
|
+
// Default: lazy discovery with /__manifest endpoint
|
|
56
|
+
routeDiscovery: {
|
|
57
|
+
mode: "lazy",
|
|
58
|
+
manifestPath: "/__manifest",
|
|
59
|
+
},
|
|
60
|
+
|
|
61
|
+
// Custom manifest path (useful for multiple apps on same domain)
|
|
62
|
+
routeDiscovery: {
|
|
63
|
+
mode: "lazy",
|
|
64
|
+
manifestPath: "/my-app-manifest",
|
|
65
|
+
},
|
|
66
|
+
|
|
67
|
+
// Disable lazy discovery (include all routes initially)
|
|
68
|
+
routeDiscovery: { mode: "initial" },
|
|
69
|
+
} satisfies Config;
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Deployment Considerations
|
|
73
|
+
|
|
74
|
+
When using lazy route discovery, ensure your deployment setup handles manifest requests properly:
|
|
75
|
+
|
|
76
|
+
- **Route Handling** - Ensure `/__manifest` requests reach your React Router handler
|
|
77
|
+
- **CDN Caching** - If using CDN/edge caching, include `version` and `paths` query parameters in your cache key for the manifest endpoint
|
|
78
|
+
- **Multiple Applications** - Use a custom `manifestPath` if running multiple React Router applications on the same domain
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Progressive Enhancement
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
# Progressive Enhancement
|
|
6
|
+
|
|
7
|
+
[MODES: framework]
|
|
8
|
+
|
|
9
|
+
<br/>
|
|
10
|
+
<br/>
|
|
11
|
+
|
|
12
|
+
> Progressive enhancement is a strategy in web design that puts emphasis on web content first, allowing everyone to access the basic content and functionality of a web page, whilst users with additional browser features or faster Internet access receive the enhanced version instead.
|
|
13
|
+
>
|
|
14
|
+
> <cite>- [Wikipedia][wikipedia]</cite>
|
|
15
|
+
|
|
16
|
+
When using React Router with Server-Side Rendering (the default in framework mode), you can automatically leverage the benefits of progressive enhancement.
|
|
17
|
+
|
|
18
|
+
## Why Progressive Enhancement Matters
|
|
19
|
+
|
|
20
|
+
Coined in 2003 by Steven Champeon & Nick Finck, the phrase emerged during a time of varied CSS and JavaScript support across different browsers, with many users actually browsing the web with JavaScript disabled.
|
|
21
|
+
|
|
22
|
+
Today, we are fortunate to develop for a much more consistent web and where the majority of users have JavaScript enabled.
|
|
23
|
+
|
|
24
|
+
However, we still believe in the core principles of progressive enhancement in React Router. It leads to fast and resilient apps with simple development workflows.
|
|
25
|
+
|
|
26
|
+
**Performance**: While it's easy to think that only 5% of your users have slow connections, the reality is that 100% of your users have slow connections 5% of the time.
|
|
27
|
+
|
|
28
|
+
**Resilience**: Everybody has JavaScript disabled until it's loaded.
|
|
29
|
+
|
|
30
|
+
**Simplicity**: Building your apps in a progressively enhanced way with React Router is actually simpler than building a traditional SPA.
|
|
31
|
+
|
|
32
|
+
## Performance
|
|
33
|
+
|
|
34
|
+
Server rendering allows your app to do more things in parallel than a typical [Single Page App (SPA)][spa], making the initial loading experience and subsequent navigations faster.
|
|
35
|
+
|
|
36
|
+
Typical SPAs send a blank document and only start doing work when JavaScript has loaded:
|
|
37
|
+
|
|
38
|
+
```
|
|
39
|
+
HTML |---|
|
|
40
|
+
JavaScript |---------|
|
|
41
|
+
Data |---------------|
|
|
42
|
+
page rendered 👆
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
A React Router app can start doing work the moment the request hits the server and stream the response so that the browser can start downloading JavaScript, other assets, and data in parallel:
|
|
46
|
+
|
|
47
|
+
```
|
|
48
|
+
👇 first byte
|
|
49
|
+
HTML |---|-----------|
|
|
50
|
+
JavaScript |---------|
|
|
51
|
+
Data |---------------|
|
|
52
|
+
page rendered 👆
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Resilience and Accessibility
|
|
56
|
+
|
|
57
|
+
While your users probably don't browse the web with JavaScript disabled, everybody uses the websites without JavaScript before it finishes loading. React Router embraces progressive enhancement by building on top of HTML, allowing you to build your app in a way that works without JavaScript, and then layer on JavaScript to enhance the experience.
|
|
58
|
+
|
|
59
|
+
The simplest case is a `<Link to="/account">`. These render an `<a href="/account">` tag that works without JavaScript. When JavaScript loads, React Router will intercept clicks and handle the navigation with client side routing. This gives you more control over the UX instead of just spinning favicons in the browser tab--but it works either way.
|
|
60
|
+
|
|
61
|
+
Now consider a simple add to cart button:
|
|
62
|
+
|
|
63
|
+
```tsx
|
|
64
|
+
export function AddToCart({ id }) {
|
|
65
|
+
return (
|
|
66
|
+
<Form method="post" action="/add-to-cart">
|
|
67
|
+
<input type="hidden" name="id" value={id} />
|
|
68
|
+
<button type="submit">Add To Cart</button>
|
|
69
|
+
</Form>
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Whether JavaScript has loaded or not doesn't matter, this button will add the product to the cart.
|
|
75
|
+
|
|
76
|
+
When JavaScript loads, React Router will intercept the form submission and handle it client side. This allows you to add your own pending UI, or other client side behavior.
|
|
77
|
+
|
|
78
|
+
## Simplicity
|
|
79
|
+
|
|
80
|
+
When you start to rely on basic features of the web like HTML and URLs, you will find that you reach for client side state and state management much less.
|
|
81
|
+
|
|
82
|
+
Consider the button from before, with no fundamental change to the code, we can pepper in some client side behavior:
|
|
83
|
+
|
|
84
|
+
```tsx lines=[1,4,7,10-12,14]
|
|
85
|
+
import { useFetcher } from "react-router";
|
|
86
|
+
|
|
87
|
+
export function AddToCart({ id }) {
|
|
88
|
+
const fetcher = useFetcher();
|
|
89
|
+
|
|
90
|
+
return (
|
|
91
|
+
<fetcher.Form method="post" action="/add-to-cart">
|
|
92
|
+
<input name="id" value={id} />
|
|
93
|
+
<button type="submit">
|
|
94
|
+
{fetcher.state === "submitting"
|
|
95
|
+
? "Adding..."
|
|
96
|
+
: "Add To Cart"}
|
|
97
|
+
</button>
|
|
98
|
+
</fetcher.Form>
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
This feature continues to work the very same as it did before when JavaScript is loading, but once JavaScript loads:
|
|
104
|
+
|
|
105
|
+
- `useFetcher` no longer causes a navigation like `<Form>` does, so the user can stay on the same page and keep shopping
|
|
106
|
+
- The app code determines the pending UI instead of spinning favicons in the browser
|
|
107
|
+
|
|
108
|
+
It's not about building it two different ways–once for JavaScript and once without–it's about building it in iterations. Start with the simplest version of the feature and ship it; then iterate to an enhanced user experience.
|
|
109
|
+
|
|
110
|
+
Not only will the user get a progressively enhanced experience, but the app developer gets to "progressively enhance" the UI without changing the fundamental design of the feature.
|
|
111
|
+
|
|
112
|
+
Another example where progressive enhancement leads to simplicity is with the URL. When you start with a URL, you don't need to worry about client side state management. You can just use the URL as the source of truth for the UI.
|
|
113
|
+
|
|
114
|
+
```tsx
|
|
115
|
+
export function SearchBox() {
|
|
116
|
+
return (
|
|
117
|
+
<Form method="get" action="/search">
|
|
118
|
+
<input type="search" name="query" />
|
|
119
|
+
<SearchIcon />
|
|
120
|
+
</Form>
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
This component doesn't need any state management. It just renders a form that submits to `/search`. When JavaScript loads, React Router will intercept the form submission and handle it client side. Here's the next iteration:
|
|
126
|
+
|
|
127
|
+
```tsx lines=[1,4-6,11]
|
|
128
|
+
import { useNavigation } from "react-router";
|
|
129
|
+
|
|
130
|
+
export function SearchBox() {
|
|
131
|
+
const navigation = useNavigation();
|
|
132
|
+
const isSearching =
|
|
133
|
+
navigation.location.pathname === "/search";
|
|
134
|
+
|
|
135
|
+
return (
|
|
136
|
+
<Form method="get" action="/search">
|
|
137
|
+
<input type="search" name="query" />
|
|
138
|
+
{isSearching ? <Spinner /> : <SearchIcon />}
|
|
139
|
+
</Form>
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
No fundamental change in architecture, simply a progressive enhancement for both the user and the code.
|
|
145
|
+
|
|
146
|
+
See also: [State Management][state_management]
|
|
147
|
+
|
|
148
|
+
[wikipedia]: https://en.wikipedia.org/wiki/Progressive_enhancement
|
|
149
|
+
[spa]: ../how-to/spa
|
|
150
|
+
[state_management]: ./state-management
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Race Conditions
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
# Race Conditions
|
|
6
|
+
|
|
7
|
+
[MODES: framework, data]
|
|
8
|
+
|
|
9
|
+
<br/>
|
|
10
|
+
<br/>
|
|
11
|
+
|
|
12
|
+
While impossible to eliminate every possible race condition in your application, React Router automatically handles the most common race conditions found in web user interfaces.
|
|
13
|
+
|
|
14
|
+
## Browser Behavior
|
|
15
|
+
|
|
16
|
+
React Router's handling of network concurrency is heavily inspired by the behavior of web browsers when processing documents.
|
|
17
|
+
|
|
18
|
+
Consider clicking a link to a new document, and then clicking a different link before the new page has finished loading. The browser will:
|
|
19
|
+
|
|
20
|
+
1. cancel the first request
|
|
21
|
+
2. immediately process the new navigation
|
|
22
|
+
|
|
23
|
+
The same behavior applies to form submissions. When a pending form submission is interrupted by a new one, the first is canceled and the new submission is immediately processed.
|
|
24
|
+
|
|
25
|
+
## React Router Behavior
|
|
26
|
+
|
|
27
|
+
Like the browser, interrupted navigations with links and form submissions will cancel in flight data requests and immediately process the new event.
|
|
28
|
+
|
|
29
|
+
Fetchers are a bit more nuanced since they are not singleton events like navigation. Fetchers can't interrupt other fetcher instances, but they can interrupt themselves and the behavior is the same as everything else: cancel the interrupted request and immediately process the new one.
|
|
30
|
+
|
|
31
|
+
Fetchers do, however, interact with each other when it comes to revalidation. After a fetcher's action request returns to the browser, a revalidation for all page data is sent. This means multiple revalidation requests can be in-flight at the same time. React Router will commit all "fresh" revalidation responses and cancel any stale requests. A stale request is any request that started _earlier_ than one that has returned.
|
|
32
|
+
|
|
33
|
+
This management of the network prevents the most common UI bugs caused by network race conditions.
|
|
34
|
+
|
|
35
|
+
Since networks are unpredictable, and your server still processes these cancelled requests, your backend may still experience race conditions and have potential data integrity issues. These risks are the same risks as using default browser behavior with plain HTML `<forms>`, which we consider to be low, and outside the scope of React Router.
|
|
36
|
+
|
|
37
|
+
## Practical Benefits
|
|
38
|
+
|
|
39
|
+
Consider building a type-ahead combobox. As the user types, you send a request to the server. As they type each new character you send a new request. It's important to not show the user results for a value that's not in the text field anymore.
|
|
40
|
+
|
|
41
|
+
When using a fetcher, this is automatically managed for you. Consider this pseudo-code:
|
|
42
|
+
|
|
43
|
+
```tsx
|
|
44
|
+
// route("/city-search", "./search-cities.ts")
|
|
45
|
+
export async function loader({ request }) {
|
|
46
|
+
const { searchParams } = new URL(request.url);
|
|
47
|
+
return searchCities(searchParams.get("q"));
|
|
48
|
+
}
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
```tsx
|
|
52
|
+
export function CitySearchCombobox() {
|
|
53
|
+
const fetcher = useFetcher();
|
|
54
|
+
|
|
55
|
+
return (
|
|
56
|
+
<fetcher.Form action="/city-search">
|
|
57
|
+
<Combobox aria-label="Cities">
|
|
58
|
+
<ComboboxInput
|
|
59
|
+
name="q"
|
|
60
|
+
onChange={(event) =>
|
|
61
|
+
// submit the form onChange to get the list of cities
|
|
62
|
+
fetcher.submit(event.target.form)
|
|
63
|
+
}
|
|
64
|
+
/>
|
|
65
|
+
|
|
66
|
+
{fetcher.data ? (
|
|
67
|
+
<ComboboxPopover className="shadow-popup">
|
|
68
|
+
{fetcher.data.length > 0 ? (
|
|
69
|
+
<ComboboxList>
|
|
70
|
+
{fetcher.data.map((city) => (
|
|
71
|
+
<ComboboxOption
|
|
72
|
+
key={city.id}
|
|
73
|
+
value={city.name}
|
|
74
|
+
/>
|
|
75
|
+
))}
|
|
76
|
+
</ComboboxList>
|
|
77
|
+
) : (
|
|
78
|
+
<span>No results found</span>
|
|
79
|
+
)}
|
|
80
|
+
</ComboboxPopover>
|
|
81
|
+
) : null}
|
|
82
|
+
</Combobox>
|
|
83
|
+
</fetcher.Form>
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
Calls to `fetcher.submit` will cancel pending requests on that fetcher automatically. This ensures you never show the user results for a request for a different input value.
|