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,363 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Framework Adoption from Component Routes
|
|
3
|
+
order: 4
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Framework Adoption from Component Routes
|
|
7
|
+
|
|
8
|
+
If you are using `<RouterProvider>` please see [Framework Adoption from RouterProvider][upgrade-router-provider] instead.
|
|
9
|
+
|
|
10
|
+
If you are using `<Routes>` this is the right place.
|
|
11
|
+
|
|
12
|
+
The React Router Vite plugin adds framework features to React Router. This guide will help you adopt the plugin in your app. If you run into any issues, please reach out for help on [Twitter](https://x.com/remix_run) or [Discord](https://rmx.as/discord).
|
|
13
|
+
|
|
14
|
+
## Features
|
|
15
|
+
|
|
16
|
+
The Vite plugin adds:
|
|
17
|
+
|
|
18
|
+
- Route loaders, actions, and automatic data revalidation
|
|
19
|
+
- Type-safe Routes Modules
|
|
20
|
+
- Automatic route code-splitting
|
|
21
|
+
- Automatic scroll restoration across navigations
|
|
22
|
+
- Optional Static pre-rendering
|
|
23
|
+
- Optional Server rendering
|
|
24
|
+
|
|
25
|
+
The initial setup requires the most work. However, once complete, you can adopt new features incrementally, one route at a time.
|
|
26
|
+
|
|
27
|
+
## Prerequisites
|
|
28
|
+
|
|
29
|
+
To use the Vite plugin, your project requires:
|
|
30
|
+
|
|
31
|
+
- Node.js 20+ (if using Node as your runtime)
|
|
32
|
+
- Vite 5+
|
|
33
|
+
|
|
34
|
+
## 1. Install the Vite plugin
|
|
35
|
+
|
|
36
|
+
**👉 Install the React Router Vite plugin**
|
|
37
|
+
|
|
38
|
+
```shellscript nonumber
|
|
39
|
+
npm install -D @react-router/dev
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
**👉 Install a runtime adapter**
|
|
43
|
+
|
|
44
|
+
We will assume you are using Node as your runtime.
|
|
45
|
+
|
|
46
|
+
```shellscript nonumber
|
|
47
|
+
npm install @react-router/node
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
**👉 Swap out the React plugin for React Router.**
|
|
51
|
+
|
|
52
|
+
```diff filename=vite.config.ts
|
|
53
|
+
-import react from '@vitejs/plugin-react'
|
|
54
|
+
+import { reactRouter } from "@react-router/dev/vite";
|
|
55
|
+
import { defineConfig } from "vite";
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
export default defineConfig({
|
|
59
|
+
plugins: [
|
|
60
|
+
- react()
|
|
61
|
+
+ reactRouter()
|
|
62
|
+
],
|
|
63
|
+
});
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## 2. Add the React Router config
|
|
67
|
+
|
|
68
|
+
**👉 Create a `react-router.config.ts` file**
|
|
69
|
+
|
|
70
|
+
Add the following to the root of your project. In this config you can tell React Router about your project, like where to find the app directory and to not use SSR (server-side rendering) for now.
|
|
71
|
+
|
|
72
|
+
```shellscript nonumber
|
|
73
|
+
touch react-router.config.ts
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
```ts filename=react-router.config.ts
|
|
77
|
+
import type { Config } from "@react-router/dev/config";
|
|
78
|
+
|
|
79
|
+
export default {
|
|
80
|
+
appDirectory: "src",
|
|
81
|
+
ssr: false,
|
|
82
|
+
} satisfies Config;
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## 3. Add the Root entry point
|
|
86
|
+
|
|
87
|
+
In a typical Vite app, the `index.html` file is the entry point for bundling. The React Router Vite plugin moves the entry point to a `root.tsx` file so you can use React to render the shell of your app instead of static HTML, and eventually upgrade to Server Rendering if you want.
|
|
88
|
+
|
|
89
|
+
**👉 Move your existing `index.html` to `root.tsx`**
|
|
90
|
+
|
|
91
|
+
For example, if your current `index.html` looks like this:
|
|
92
|
+
|
|
93
|
+
```html filename=index.html
|
|
94
|
+
<!DOCTYPE html>
|
|
95
|
+
<html lang="en">
|
|
96
|
+
<head>
|
|
97
|
+
<meta charset="UTF-8" />
|
|
98
|
+
<meta
|
|
99
|
+
name="viewport"
|
|
100
|
+
content="width=device-width, initial-scale=1.0"
|
|
101
|
+
/>
|
|
102
|
+
<title>My App</title>
|
|
103
|
+
</head>
|
|
104
|
+
<body>
|
|
105
|
+
<div id="root"></div>
|
|
106
|
+
<script type="module" src="/src/main.tsx"></script>
|
|
107
|
+
</body>
|
|
108
|
+
</html>
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
You would move that markup into `src/root.tsx` and delete `index.html`:
|
|
112
|
+
|
|
113
|
+
```shellscript nonumber
|
|
114
|
+
touch src/root.tsx
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
```tsx filename=src/root.tsx
|
|
118
|
+
import {
|
|
119
|
+
Links,
|
|
120
|
+
Meta,
|
|
121
|
+
Outlet,
|
|
122
|
+
Scripts,
|
|
123
|
+
ScrollRestoration,
|
|
124
|
+
} from "react-router";
|
|
125
|
+
|
|
126
|
+
export function Layout({
|
|
127
|
+
children,
|
|
128
|
+
}: {
|
|
129
|
+
children: React.ReactNode;
|
|
130
|
+
}) {
|
|
131
|
+
return (
|
|
132
|
+
<html lang="en">
|
|
133
|
+
<head>
|
|
134
|
+
<meta charSet="UTF-8" />
|
|
135
|
+
<meta
|
|
136
|
+
name="viewport"
|
|
137
|
+
content="width=device-width, initial-scale=1.0"
|
|
138
|
+
/>
|
|
139
|
+
<title>My App</title>
|
|
140
|
+
<Meta />
|
|
141
|
+
<Links />
|
|
142
|
+
</head>
|
|
143
|
+
<body>
|
|
144
|
+
{children}
|
|
145
|
+
<ScrollRestoration />
|
|
146
|
+
<Scripts />
|
|
147
|
+
</body>
|
|
148
|
+
</html>
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export default function Root() {
|
|
153
|
+
return <Outlet />;
|
|
154
|
+
}
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
## 4. Add client entry module
|
|
158
|
+
|
|
159
|
+
In the typical Vite app the `index.html` file points to `src/main.tsx` as the client entry point. React Router uses a file named `src/entry.client.tsx` instead.
|
|
160
|
+
|
|
161
|
+
**👉 Make `src/entry.client.tsx` your entry point**
|
|
162
|
+
|
|
163
|
+
If your current `src/main.tsx` looks like this:
|
|
164
|
+
|
|
165
|
+
```tsx filename=src/main.tsx
|
|
166
|
+
import React from "react";
|
|
167
|
+
import ReactDOM from "react-dom/client";
|
|
168
|
+
import { BrowserRouter } from "react-router";
|
|
169
|
+
import "./index.css";
|
|
170
|
+
import App from "./App";
|
|
171
|
+
|
|
172
|
+
ReactDOM.createRoot(
|
|
173
|
+
document.getElementById("root")!,
|
|
174
|
+
).render(
|
|
175
|
+
<React.StrictMode>
|
|
176
|
+
<BrowserRouter>
|
|
177
|
+
<App />
|
|
178
|
+
</BrowserRouter>
|
|
179
|
+
</React.StrictMode>,
|
|
180
|
+
);
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
You would rename it to `entry.client.tsx` and change it to this:
|
|
184
|
+
|
|
185
|
+
```tsx filename=src/entry.client.tsx
|
|
186
|
+
import React from "react";
|
|
187
|
+
import ReactDOM from "react-dom/client";
|
|
188
|
+
import { HydratedRouter } from "react-router/dom";
|
|
189
|
+
import "./index.css";
|
|
190
|
+
|
|
191
|
+
ReactDOM.hydrateRoot(
|
|
192
|
+
document,
|
|
193
|
+
<React.StrictMode>
|
|
194
|
+
<HydratedRouter />
|
|
195
|
+
</React.StrictMode>,
|
|
196
|
+
);
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
- Use `hydrateRoot` instead of `createRoot`
|
|
200
|
+
- Render a `<HydratedRouter>` instead of your `<App/>` component
|
|
201
|
+
- Note: we stopped rendering the `<App/>` component. We'll bring it back in a later step, but first we want to get the app to boot with the new entry point.
|
|
202
|
+
|
|
203
|
+
## 5. Shuffle stuff around
|
|
204
|
+
|
|
205
|
+
Between `root.tsx` and `entry.client.tsx`, you may want to shuffle some stuff around between them.
|
|
206
|
+
|
|
207
|
+
In general:
|
|
208
|
+
|
|
209
|
+
- `root.tsx` contains any rendering things like context providers, layouts, styles, etc.
|
|
210
|
+
- `entry.client.tsx` should be as minimal as possible
|
|
211
|
+
- Remember to _not_ try to render your existing `<App/>` component yet, we'll do that in a later step
|
|
212
|
+
|
|
213
|
+
Note that your `root.tsx` file will be statically generated and served as the entry point of your app, so just that module will need to be compatible with server rendering. This is where most of your trouble will come.
|
|
214
|
+
|
|
215
|
+
## 6. Set up your routes
|
|
216
|
+
|
|
217
|
+
The React Router Vite plugin uses a `routes.ts` file to configure your routes. For now we'll add a simple catchall route to get things going.
|
|
218
|
+
|
|
219
|
+
**👉 Set up a `catchall.tsx` route**
|
|
220
|
+
|
|
221
|
+
```shellscript nonumber
|
|
222
|
+
touch src/routes.ts src/catchall.tsx
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
```ts filename=src/routes.ts
|
|
226
|
+
import {
|
|
227
|
+
type RouteConfig,
|
|
228
|
+
route,
|
|
229
|
+
} from "@react-router/dev/routes";
|
|
230
|
+
|
|
231
|
+
export default [
|
|
232
|
+
// * matches all URLs, the ? makes it optional so it will match / as well
|
|
233
|
+
route("*?", "catchall.tsx"),
|
|
234
|
+
] satisfies RouteConfig;
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
**👉 Render a placeholder route**
|
|
238
|
+
|
|
239
|
+
Eventually we'll replace this with our original `App` component, but for now we'll just render something simple to make sure we can boot the app.
|
|
240
|
+
|
|
241
|
+
```tsx filename=src/catchall.tsx
|
|
242
|
+
export default function Component() {
|
|
243
|
+
return <div>Hello, world!</div>;
|
|
244
|
+
}
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
[View our guide on configuring routes][configuring-routes] to learn more about the `routes.ts` file.
|
|
248
|
+
|
|
249
|
+
## 7. Boot the app
|
|
250
|
+
|
|
251
|
+
At this point you should be able to boot the app and see the root layout.
|
|
252
|
+
|
|
253
|
+
**👉 Add `dev` script and run the app**
|
|
254
|
+
|
|
255
|
+
```json filename=package.json
|
|
256
|
+
"scripts": {
|
|
257
|
+
"dev": "react-router dev"
|
|
258
|
+
}
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
Now make sure you can boot your app at this point before moving on:
|
|
262
|
+
|
|
263
|
+
```shellscript
|
|
264
|
+
npm run dev
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
You will probably want to add `.react-router/` to your `.gitignore` file to avoid tracking unnecessary files in your repository.
|
|
268
|
+
|
|
269
|
+
```txt
|
|
270
|
+
.react-router/
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
You can check out [Type Safety][type-safety] to learn how to fully set up and use autogenerated type safety for params, loader data, and more.
|
|
274
|
+
|
|
275
|
+
## 8. Render your app
|
|
276
|
+
|
|
277
|
+
To get back to rendering your app, we'll update the "catchall" route we set up earlier that matches all URLs so that your existing `<Routes>` get a chance to render.
|
|
278
|
+
|
|
279
|
+
**👉 Update the catchall route to render your app**
|
|
280
|
+
|
|
281
|
+
```tsx filename=src/catchall.tsx
|
|
282
|
+
import App from "./App";
|
|
283
|
+
|
|
284
|
+
export default function Component() {
|
|
285
|
+
return <App />;
|
|
286
|
+
}
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
Your app should be back on the screen and working as usual!
|
|
290
|
+
|
|
291
|
+
## 9. Migrate a route to a Route Module
|
|
292
|
+
|
|
293
|
+
You can now incrementally migrate your routes to route modules.
|
|
294
|
+
|
|
295
|
+
Given an existing route like this:
|
|
296
|
+
|
|
297
|
+
```tsx filename=src/App.tsx
|
|
298
|
+
// ...
|
|
299
|
+
import About from "./containers/About";
|
|
300
|
+
|
|
301
|
+
export default function App() {
|
|
302
|
+
return (
|
|
303
|
+
<Routes>
|
|
304
|
+
<Route path="/about" element={<About />} />
|
|
305
|
+
</Routes>
|
|
306
|
+
);
|
|
307
|
+
}
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
**👉 Add the route definition to `routes.ts`**
|
|
311
|
+
|
|
312
|
+
```tsx filename=src/routes.ts
|
|
313
|
+
import {
|
|
314
|
+
type RouteConfig,
|
|
315
|
+
route,
|
|
316
|
+
} from "@react-router/dev/routes";
|
|
317
|
+
|
|
318
|
+
export default [
|
|
319
|
+
route("/about", "./pages/about.tsx"),
|
|
320
|
+
route("*?", "catchall.tsx"),
|
|
321
|
+
] satisfies RouteConfig;
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
**👉 Add the route module**
|
|
325
|
+
|
|
326
|
+
Edit the route module to use the [Route Module API][route-modules]:
|
|
327
|
+
|
|
328
|
+
```tsx filename=src/pages/about.tsx
|
|
329
|
+
export async function clientLoader() {
|
|
330
|
+
// you can now fetch data here
|
|
331
|
+
return {
|
|
332
|
+
title: "About page",
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
export default function Component({ loaderData }) {
|
|
337
|
+
return <h1>{loaderData.title}</h1>;
|
|
338
|
+
}
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
See [Type Safety][type-safety] to set up autogenerated type safety for params, loader data, and more.
|
|
342
|
+
|
|
343
|
+
The first few routes you migrate are the hardest because you often have to access various abstractions a bit differently than before (like in a loader instead of from a hook or context). But once the trickiest bits get dealt with, you get into an incremental groove.
|
|
344
|
+
|
|
345
|
+
## Enable SSR and/or Pre-rendering
|
|
346
|
+
|
|
347
|
+
If you want to enable server rendering and static pre-rendering, you can do so with the `ssr` and `prerender` options in the bundler plugin. For SSR you'll need to also deploy the server build to a server.
|
|
348
|
+
|
|
349
|
+
```ts filename=react-router.config.ts
|
|
350
|
+
import type { Config } from "@react-router/dev/config";
|
|
351
|
+
|
|
352
|
+
export default {
|
|
353
|
+
ssr: true,
|
|
354
|
+
async prerender() {
|
|
355
|
+
return ["/", "/about", "/contact"];
|
|
356
|
+
},
|
|
357
|
+
} satisfies Config;
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
[upgrade-router-provider]: ./router-provider
|
|
361
|
+
[configuring-routes]: ../start/framework/routing
|
|
362
|
+
[route-modules]: ../start/framework/route-module
|
|
363
|
+
[type-safety]: ../how-to/route-module-type-safety
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Future Flags
|
|
3
|
+
order: 1
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Future Flags and Deprecations
|
|
7
|
+
|
|
8
|
+
This guide walks you through the process of adopting future flags in your React Router app. By following this strategy, you will be able to upgrade to the next major version of React Router with minimal changes. To read more about future flags see [API Development Strategy][api-development-strategy].
|
|
9
|
+
|
|
10
|
+
We highly recommend you make a commit after each step and ship it instead of doing everything all at once. Most flags can be adopted in any order, with exceptions noted below.
|
|
11
|
+
|
|
12
|
+
## Update to latest v7.x
|
|
13
|
+
|
|
14
|
+
First update to the latest minor version of v7.x to have the latest future flags. You may see a number of deprecation warnings as you upgrade, which we'll cover below.
|
|
15
|
+
|
|
16
|
+
👉 Update to latest v7
|
|
17
|
+
|
|
18
|
+
```sh
|
|
19
|
+
npm install react-router@7 @react-router/{dev,node,etc.}@7
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## `future.v8_middleware`
|
|
23
|
+
|
|
24
|
+
[MODES: framework, data]
|
|
25
|
+
|
|
26
|
+
<br/>
|
|
27
|
+
<br/>
|
|
28
|
+
|
|
29
|
+
**Background**
|
|
30
|
+
|
|
31
|
+
Middleware allows you to run code before and after the [`Response`][Response] generation for the matched path. This enables common patterns like authentication, logging, error handling, and data preprocessing in a reusable way. Please see the [docs](../how-to/middleware) for more information.
|
|
32
|
+
|
|
33
|
+
👉 **Enable the Flag**
|
|
34
|
+
|
|
35
|
+
In Framework mode:
|
|
36
|
+
|
|
37
|
+
```ts filename=react-router.config.ts
|
|
38
|
+
import type { Config } from "@react-router/dev/config";
|
|
39
|
+
|
|
40
|
+
export default {
|
|
41
|
+
future: {
|
|
42
|
+
v8_middleware: true,
|
|
43
|
+
},
|
|
44
|
+
} satisfies Config;
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
In Data mode:
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
import { createBrowserRouter } from "react-router/dom";
|
|
51
|
+
|
|
52
|
+
const router = createBrowserRouter(routes, {
|
|
53
|
+
future: {
|
|
54
|
+
v8_middleware: true,
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
**Update your Code**
|
|
60
|
+
|
|
61
|
+
If you're using the `context` parameter in `loader` and `action` functions, you may need to update your code:
|
|
62
|
+
|
|
63
|
+
- In Framework mode, if you're using `react-router-serve`, you should not need to make any updates. Otherwise, this only applies if you have a custom server with a `getLoadContext` function. Please see the docs on the middleware [`getLoadContext` changes](../how-to/middleware#changes-to-getloadcontextapploadcontext) and the instructions to [migrate to the new API](../how-to/middleware#migration-from-apploadcontext).
|
|
64
|
+
- In Data mode, add the `Future` module augmentation described in the [middleware docs](../how-to/middleware#1-typescript-augment-future-for-loaderaction-context) so `context` is typed correctly.
|
|
65
|
+
|
|
66
|
+
## `future.v8_splitRouteModules`
|
|
67
|
+
|
|
68
|
+
[MODES: framework]
|
|
69
|
+
|
|
70
|
+
<br/>
|
|
71
|
+
<br/>
|
|
72
|
+
|
|
73
|
+
**Background**
|
|
74
|
+
|
|
75
|
+
This feature enables splitting client-side route exports (`clientLoader`, `clientAction`, `clientMiddleware`, `HydrateFallback`) into separate chunks that can be loaded independently from the route component. This allows these exports to be fetched and executed while the component code is still downloading, improving performance for client-side data loading.
|
|
76
|
+
|
|
77
|
+
This can be set to `true` for opt-in behavior, or `"enforce"` to require all routes to be splittable (which will cause build failures for routes that cannot be split due to shared code).
|
|
78
|
+
|
|
79
|
+
👉 **Enable the Flag**
|
|
80
|
+
|
|
81
|
+
```ts filename=react-router.config.ts
|
|
82
|
+
import type { Config } from "@react-router/dev/config";
|
|
83
|
+
|
|
84
|
+
export default {
|
|
85
|
+
future: {
|
|
86
|
+
v8_splitRouteModules: true,
|
|
87
|
+
},
|
|
88
|
+
} satisfies Config;
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
**Update your Code**
|
|
92
|
+
|
|
93
|
+
No code changes are required. This is an optimization feature that works automatically once enabled.
|
|
94
|
+
|
|
95
|
+
## `future.v8_viteEnvironmentApi`
|
|
96
|
+
|
|
97
|
+
[MODES: framework]
|
|
98
|
+
|
|
99
|
+
<br/>
|
|
100
|
+
<br/>
|
|
101
|
+
|
|
102
|
+
**Background**
|
|
103
|
+
|
|
104
|
+
This enables support for the experimental Vite Environment API, which provides a more flexible and powerful way to configure Vite environments. This is only available when using Vite 6+.
|
|
105
|
+
|
|
106
|
+
👉 **Enable the Flag**
|
|
107
|
+
|
|
108
|
+
```ts filename=react-router.config.ts
|
|
109
|
+
import type { Config } from "@react-router/dev/config";
|
|
110
|
+
|
|
111
|
+
export default {
|
|
112
|
+
future: {
|
|
113
|
+
v8_viteEnvironmentApi: true,
|
|
114
|
+
},
|
|
115
|
+
} satisfies Config;
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
**Update your Code**
|
|
119
|
+
|
|
120
|
+
Most users won't need to make any changes. However, if you have custom Vite configuration that previously relied on the `isSsrBuild` flag — such as a custom server build that sets `build.rollupOptions.input` — you'll need to move that configuration under the per-environment [Environment API][vite-environment] config instead.
|
|
121
|
+
|
|
122
|
+
For example, a custom server build should move its SSR `rollupOptions` from the top-level `build` config into `environments.ssr.build`:
|
|
123
|
+
|
|
124
|
+
```diff filename=vite.config.ts
|
|
125
|
+
import { reactRouter } from "@react-router/dev/vite";
|
|
126
|
+
import { defineConfig } from "vite";
|
|
127
|
+
|
|
128
|
+
-export default defineConfig(({ isSsrBuild }) => ({
|
|
129
|
+
- build: {
|
|
130
|
+
- rollupOptions: isSsrBuild
|
|
131
|
+
- ? {
|
|
132
|
+
- input: "./server/app.ts",
|
|
133
|
+
- }
|
|
134
|
+
- : undefined,
|
|
135
|
+
- },
|
|
136
|
+
+export default defineConfig({
|
|
137
|
+
+ environments: {
|
|
138
|
+
+ ssr: {
|
|
139
|
+
+ build: {
|
|
140
|
+
+ rollupOptions: {
|
|
141
|
+
+ input: "./server/app.ts",
|
|
142
|
+
+ },
|
|
143
|
+
+ },
|
|
144
|
+
+ },
|
|
145
|
+
+ },
|
|
146
|
+
plugins: [reactRouter()],
|
|
147
|
+
-}));
|
|
148
|
+
+});
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
See the [`node-custom-server` template][node-custom-server-template] for a complete example.
|
|
152
|
+
|
|
153
|
+
## `future.v8_passThroughRequests`
|
|
154
|
+
|
|
155
|
+
[MODES: framework]
|
|
156
|
+
|
|
157
|
+
<br/>
|
|
158
|
+
<br/>
|
|
159
|
+
|
|
160
|
+
**Background**
|
|
161
|
+
|
|
162
|
+
By default, React Router normalizes the `request.url` passed to your `loader`, `action`, and `middleware` functions by removing React Router's internal implementation details. Specifically, it removes `.data` suffixes and internal search parameters like `?index` and `?_routes`.
|
|
163
|
+
|
|
164
|
+
This flag eliminates that normalization and passes the raw HTTP `request` instance to your handlers. This provides a few benefits:
|
|
165
|
+
|
|
166
|
+
- Reduces server-side overhead by eliminating multiple `new Request()` calls on the critical path
|
|
167
|
+
- Allows you to distinguish document from data requests in your handlers based on the presence of a `.data` suffix (useful for [observability] purposes)
|
|
168
|
+
|
|
169
|
+
If you were previously relying on the normalization of `request.url`, you can switch to use the new sibling `url` parameter which contains a `URL` instance representing the normalized location.
|
|
170
|
+
|
|
171
|
+
👉 **Enable the Flag**
|
|
172
|
+
|
|
173
|
+
```ts filename=react-router.config.ts
|
|
174
|
+
import type { Config } from "@react-router/dev/config";
|
|
175
|
+
|
|
176
|
+
export default {
|
|
177
|
+
future: {
|
|
178
|
+
v8_passThroughRequests: true,
|
|
179
|
+
},
|
|
180
|
+
} satisfies Config;
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
**Update your Code**
|
|
184
|
+
|
|
185
|
+
If your code relies on inspecting the request URL, you should review it for any assumptions about the URL format:
|
|
186
|
+
|
|
187
|
+
```tsx
|
|
188
|
+
// ❌ Before: assuming no `.data` suffix in `request.url` pathname
|
|
189
|
+
export async function loader({
|
|
190
|
+
request,
|
|
191
|
+
}: Route.LoaderArgs) {
|
|
192
|
+
let url = new URL(request.url);
|
|
193
|
+
if (url.pathname === "/path") {
|
|
194
|
+
// This check might now behave differently because the request pathname will
|
|
195
|
+
// contain the `.data` suffix on data requests
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// ✅ After: use `url` for normalized routing logic and `request.url`
|
|
200
|
+
// for raw routing logic
|
|
201
|
+
export async function loader({
|
|
202
|
+
request,
|
|
203
|
+
url,
|
|
204
|
+
}: Route.LoaderArgs) {
|
|
205
|
+
if (url.pathname === "/path") {
|
|
206
|
+
// This will always have the `.data` suffix stripped
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// And now you can distinguish between document versus data requests
|
|
210
|
+
let isDataRequest = new URL(
|
|
211
|
+
request.url,
|
|
212
|
+
).pathname.endsWith(".data");
|
|
213
|
+
}
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
## `future.v8_trailingSlashAwareDataRequests`
|
|
217
|
+
|
|
218
|
+
[MODES: framework]
|
|
219
|
+
|
|
220
|
+
<br/>
|
|
221
|
+
<br/>
|
|
222
|
+
|
|
223
|
+
**Background**
|
|
224
|
+
|
|
225
|
+
React Router serves Framework mode data requests from `.data` URLs. Previously, data requests for routes with and without trailing slashes could map to the same `.data` URL because trailing slashes were not considered during URL generation. This flag preserves trailing slash semantics for data request URLs to avoid ambiguity when your app distinguishes between trailing-slash and non-trailing-slash URLs.
|
|
226
|
+
|
|
227
|
+
Currently, your HTTP and `request` pathnames would be as follows for `/a/b/c` and `/a/b/c/`
|
|
228
|
+
|
|
229
|
+
| URL `/a/b/c` | **HTTP pathname** | **`request` pathname`** |
|
|
230
|
+
| ------------ | ----------------- | ----------------------- |
|
|
231
|
+
| **Document** | `/a/b/c` | `/a/b/c` ✅ |
|
|
232
|
+
| **Data** | `/a/b/c.data` | `/a/b/c` ✅ |
|
|
233
|
+
|
|
234
|
+
| URL `/a/b/c/` | **HTTP pathname** | **`request` pathname`** |
|
|
235
|
+
| ------------- | ----------------- | ----------------------- |
|
|
236
|
+
| **Document** | `/a/b/c/` | `/a/b/c/` ✅ |
|
|
237
|
+
| **Data** | `/a/b/c.data` | `/a/b/c` ⚠️ |
|
|
238
|
+
|
|
239
|
+
With this flag enabled, these pathnames will be made consistent though a new `_.data` format for client-side `.data` requests:
|
|
240
|
+
|
|
241
|
+
| URL `/a/b/c` | **HTTP pathname** | **`request` pathname`** |
|
|
242
|
+
| ------------ | ----------------- | ----------------------- |
|
|
243
|
+
| **Document** | `/a/b/c` | `/a/b/c` ✅ |
|
|
244
|
+
| **Data** | `/a/b/c.data` | `/a/b/c` ✅ |
|
|
245
|
+
|
|
246
|
+
| URL `/a/b/c/` | **HTTP pathname** | **`request` pathname`** |
|
|
247
|
+
| ------------- | ------------------ | ----------------------- |
|
|
248
|
+
| **Document** | `/a/b/c/` | `/a/b/c/` ✅ |
|
|
249
|
+
| **Data** | `/a/b/c/_.data` ⬅️ | `/a/b/c/` ✅ |
|
|
250
|
+
|
|
251
|
+
This flag also aligns the root data request to match this behavior by changing it from `/_root.data` to `/_.data`.
|
|
252
|
+
|
|
253
|
+
👉 **Enable the Flag**
|
|
254
|
+
|
|
255
|
+
```ts filename=react-router.config.ts
|
|
256
|
+
import type { Config } from "@react-router/dev/config";
|
|
257
|
+
|
|
258
|
+
export default {
|
|
259
|
+
future: {
|
|
260
|
+
v8_trailingSlashAwareDataRequests: true,
|
|
261
|
+
},
|
|
262
|
+
} satisfies Config;
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
**Update your Code**
|
|
266
|
+
|
|
267
|
+
If you have custom app, CDN, cache, or rewrite logic that matches `.data` request URLs, update it to handle the new trailing-slash-aware `/_.data` format.
|
|
268
|
+
|
|
269
|
+
## Unstable Future Flags (Optional)
|
|
270
|
+
|
|
271
|
+
We document some [unstable] flags here as a reference for folks contributing to the project via beta testing, but they are not generally recommended for production use and may having breaking changes patch/minor releases - adopt with caution!
|
|
272
|
+
|
|
273
|
+
_No current unstable flags to document_
|
|
274
|
+
|
|
275
|
+
[api-development-strategy]: ../community/api-development-strategy
|
|
276
|
+
[unstable]: ../community/api-development-strategy#unstable-flags
|
|
277
|
+
[observability]: ../how-to/instrumentation
|
|
278
|
+
[Response]: https://developer.mozilla.org/en-US/docs/Web/API/Response
|
|
279
|
+
[vite-environment]: https://vite.dev/guide/api-environment
|
|
280
|
+
[node-custom-server-template]: https://github.com/remix-run/react-router-templates/blob/7c617a435510bc3add3a5395c07bc65328b65e9e/node-custom-server/vite.config.ts
|