Push V1 app
This commit is contained in:
+363
@@ -0,0 +1,363 @@
|
||||
---
|
||||
title: Framework Adoption from Component Routes
|
||||
order: 4
|
||||
---
|
||||
|
||||
# Framework Adoption from Component Routes
|
||||
|
||||
If you are using `<RouterProvider>` please see [Framework Adoption from RouterProvider][upgrade-router-provider] instead.
|
||||
|
||||
If you are using `<Routes>` this is the right place.
|
||||
|
||||
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).
|
||||
|
||||
## Features
|
||||
|
||||
The Vite plugin adds:
|
||||
|
||||
- Route loaders, actions, and automatic data revalidation
|
||||
- Type-safe Routes Modules
|
||||
- Automatic route code-splitting
|
||||
- Automatic scroll restoration across navigations
|
||||
- Optional Static pre-rendering
|
||||
- Optional Server rendering
|
||||
|
||||
The initial setup requires the most work. However, once complete, you can adopt new features incrementally, one route at a time.
|
||||
|
||||
## Prerequisites
|
||||
|
||||
To use the Vite plugin, your project requires:
|
||||
|
||||
- Node.js 20+ (if using Node as your runtime)
|
||||
- Vite 5+
|
||||
|
||||
## 1. Install the Vite plugin
|
||||
|
||||
**👉 Install the React Router Vite plugin**
|
||||
|
||||
```shellscript nonumber
|
||||
npm install -D @react-router/dev
|
||||
```
|
||||
|
||||
**👉 Install a runtime adapter**
|
||||
|
||||
We will assume you are using Node as your runtime.
|
||||
|
||||
```shellscript nonumber
|
||||
npm install @react-router/node
|
||||
```
|
||||
|
||||
**👉 Swap out the React plugin for React Router.**
|
||||
|
||||
```diff filename=vite.config.ts
|
||||
-import react from '@vitejs/plugin-react'
|
||||
+import { reactRouter } from "@react-router/dev/vite";
|
||||
import { defineConfig } from "vite";
|
||||
|
||||
|
||||
export default defineConfig({
|
||||
plugins: [
|
||||
- react()
|
||||
+ reactRouter()
|
||||
],
|
||||
});
|
||||
```
|
||||
|
||||
## 2. Add the React Router config
|
||||
|
||||
**👉 Create a `react-router.config.ts` file**
|
||||
|
||||
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.
|
||||
|
||||
```shellscript nonumber
|
||||
touch react-router.config.ts
|
||||
```
|
||||
|
||||
```ts filename=react-router.config.ts
|
||||
import type { Config } from "@react-router/dev/config";
|
||||
|
||||
export default {
|
||||
appDirectory: "src",
|
||||
ssr: false,
|
||||
} satisfies Config;
|
||||
```
|
||||
|
||||
## 3. Add the Root entry point
|
||||
|
||||
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.
|
||||
|
||||
**👉 Move your existing `index.html` to `root.tsx`**
|
||||
|
||||
For example, if your current `index.html` looks like this:
|
||||
|
||||
```html filename=index.html
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta
|
||||
name="viewport"
|
||||
content="width=device-width, initial-scale=1.0"
|
||||
/>
|
||||
<title>My App</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="root"></div>
|
||||
<script type="module" src="/src/main.tsx"></script>
|
||||
</body>
|
||||
</html>
|
||||
```
|
||||
|
||||
You would move that markup into `src/root.tsx` and delete `index.html`:
|
||||
|
||||
```shellscript nonumber
|
||||
touch src/root.tsx
|
||||
```
|
||||
|
||||
```tsx filename=src/root.tsx
|
||||
import {
|
||||
Links,
|
||||
Meta,
|
||||
Outlet,
|
||||
Scripts,
|
||||
ScrollRestoration,
|
||||
} from "react-router";
|
||||
|
||||
export function Layout({
|
||||
children,
|
||||
}: {
|
||||
children: React.ReactNode;
|
||||
}) {
|
||||
return (
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charSet="UTF-8" />
|
||||
<meta
|
||||
name="viewport"
|
||||
content="width=device-width, initial-scale=1.0"
|
||||
/>
|
||||
<title>My App</title>
|
||||
<Meta />
|
||||
<Links />
|
||||
</head>
|
||||
<body>
|
||||
{children}
|
||||
<ScrollRestoration />
|
||||
<Scripts />
|
||||
</body>
|
||||
</html>
|
||||
);
|
||||
}
|
||||
|
||||
export default function Root() {
|
||||
return <Outlet />;
|
||||
}
|
||||
```
|
||||
|
||||
## 4. Add client entry module
|
||||
|
||||
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.
|
||||
|
||||
**👉 Make `src/entry.client.tsx` your entry point**
|
||||
|
||||
If your current `src/main.tsx` looks like this:
|
||||
|
||||
```tsx filename=src/main.tsx
|
||||
import React from "react";
|
||||
import ReactDOM from "react-dom/client";
|
||||
import { BrowserRouter } from "react-router";
|
||||
import "./index.css";
|
||||
import App from "./App";
|
||||
|
||||
ReactDOM.createRoot(
|
||||
document.getElementById("root")!,
|
||||
).render(
|
||||
<React.StrictMode>
|
||||
<BrowserRouter>
|
||||
<App />
|
||||
</BrowserRouter>
|
||||
</React.StrictMode>,
|
||||
);
|
||||
```
|
||||
|
||||
You would rename it to `entry.client.tsx` and change it to this:
|
||||
|
||||
```tsx filename=src/entry.client.tsx
|
||||
import React from "react";
|
||||
import ReactDOM from "react-dom/client";
|
||||
import { HydratedRouter } from "react-router/dom";
|
||||
import "./index.css";
|
||||
|
||||
ReactDOM.hydrateRoot(
|
||||
document,
|
||||
<React.StrictMode>
|
||||
<HydratedRouter />
|
||||
</React.StrictMode>,
|
||||
);
|
||||
```
|
||||
|
||||
- Use `hydrateRoot` instead of `createRoot`
|
||||
- Render a `<HydratedRouter>` instead of your `<App/>` component
|
||||
- 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.
|
||||
|
||||
## 5. Shuffle stuff around
|
||||
|
||||
Between `root.tsx` and `entry.client.tsx`, you may want to shuffle some stuff around between them.
|
||||
|
||||
In general:
|
||||
|
||||
- `root.tsx` contains any rendering things like context providers, layouts, styles, etc.
|
||||
- `entry.client.tsx` should be as minimal as possible
|
||||
- Remember to _not_ try to render your existing `<App/>` component yet, we'll do that in a later step
|
||||
|
||||
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.
|
||||
|
||||
## 6. Set up your routes
|
||||
|
||||
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.
|
||||
|
||||
**👉 Set up a `catchall.tsx` route**
|
||||
|
||||
```shellscript nonumber
|
||||
touch src/routes.ts src/catchall.tsx
|
||||
```
|
||||
|
||||
```ts filename=src/routes.ts
|
||||
import {
|
||||
type RouteConfig,
|
||||
route,
|
||||
} from "@react-router/dev/routes";
|
||||
|
||||
export default [
|
||||
// * matches all URLs, the ? makes it optional so it will match / as well
|
||||
route("*?", "catchall.tsx"),
|
||||
] satisfies RouteConfig;
|
||||
```
|
||||
|
||||
**👉 Render a placeholder route**
|
||||
|
||||
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.
|
||||
|
||||
```tsx filename=src/catchall.tsx
|
||||
export default function Component() {
|
||||
return <div>Hello, world!</div>;
|
||||
}
|
||||
```
|
||||
|
||||
[View our guide on configuring routes][configuring-routes] to learn more about the `routes.ts` file.
|
||||
|
||||
## 7. Boot the app
|
||||
|
||||
At this point you should be able to boot the app and see the root layout.
|
||||
|
||||
**👉 Add `dev` script and run the app**
|
||||
|
||||
```json filename=package.json
|
||||
"scripts": {
|
||||
"dev": "react-router dev"
|
||||
}
|
||||
```
|
||||
|
||||
Now make sure you can boot your app at this point before moving on:
|
||||
|
||||
```shellscript
|
||||
npm run dev
|
||||
```
|
||||
|
||||
You will probably want to add `.react-router/` to your `.gitignore` file to avoid tracking unnecessary files in your repository.
|
||||
|
||||
```txt
|
||||
.react-router/
|
||||
```
|
||||
|
||||
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.
|
||||
|
||||
## 8. Render your app
|
||||
|
||||
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.
|
||||
|
||||
**👉 Update the catchall route to render your app**
|
||||
|
||||
```tsx filename=src/catchall.tsx
|
||||
import App from "./App";
|
||||
|
||||
export default function Component() {
|
||||
return <App />;
|
||||
}
|
||||
```
|
||||
|
||||
Your app should be back on the screen and working as usual!
|
||||
|
||||
## 9. Migrate a route to a Route Module
|
||||
|
||||
You can now incrementally migrate your routes to route modules.
|
||||
|
||||
Given an existing route like this:
|
||||
|
||||
```tsx filename=src/App.tsx
|
||||
// ...
|
||||
import About from "./containers/About";
|
||||
|
||||
export default function App() {
|
||||
return (
|
||||
<Routes>
|
||||
<Route path="/about" element={<About />} />
|
||||
</Routes>
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
**👉 Add the route definition to `routes.ts`**
|
||||
|
||||
```tsx filename=src/routes.ts
|
||||
import {
|
||||
type RouteConfig,
|
||||
route,
|
||||
} from "@react-router/dev/routes";
|
||||
|
||||
export default [
|
||||
route("/about", "./pages/about.tsx"),
|
||||
route("*?", "catchall.tsx"),
|
||||
] satisfies RouteConfig;
|
||||
```
|
||||
|
||||
**👉 Add the route module**
|
||||
|
||||
Edit the route module to use the [Route Module API][route-modules]:
|
||||
|
||||
```tsx filename=src/pages/about.tsx
|
||||
export async function clientLoader() {
|
||||
// you can now fetch data here
|
||||
return {
|
||||
title: "About page",
|
||||
};
|
||||
}
|
||||
|
||||
export default function Component({ loaderData }) {
|
||||
return <h1>{loaderData.title}</h1>;
|
||||
}
|
||||
```
|
||||
|
||||
See [Type Safety][type-safety] to set up autogenerated type safety for params, loader data, and more.
|
||||
|
||||
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.
|
||||
|
||||
## Enable SSR and/or Pre-rendering
|
||||
|
||||
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.
|
||||
|
||||
```ts filename=react-router.config.ts
|
||||
import type { Config } from "@react-router/dev/config";
|
||||
|
||||
export default {
|
||||
ssr: true,
|
||||
async prerender() {
|
||||
return ["/", "/about", "/contact"];
|
||||
},
|
||||
} satisfies Config;
|
||||
```
|
||||
|
||||
[upgrade-router-provider]: ./router-provider
|
||||
[configuring-routes]: ../start/framework/routing
|
||||
[route-modules]: ../start/framework/route-module
|
||||
[type-safety]: ../how-to/route-module-type-safety
|
||||
+280
@@ -0,0 +1,280 @@
|
||||
---
|
||||
title: Future Flags
|
||||
order: 1
|
||||
---
|
||||
|
||||
# Future Flags and Deprecations
|
||||
|
||||
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].
|
||||
|
||||
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.
|
||||
|
||||
## Update to latest v7.x
|
||||
|
||||
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.
|
||||
|
||||
👉 Update to latest v7
|
||||
|
||||
```sh
|
||||
npm install react-router@7 @react-router/{dev,node,etc.}@7
|
||||
```
|
||||
|
||||
## `future.v8_middleware`
|
||||
|
||||
[MODES: framework, data]
|
||||
|
||||
<br/>
|
||||
<br/>
|
||||
|
||||
**Background**
|
||||
|
||||
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.
|
||||
|
||||
👉 **Enable the Flag**
|
||||
|
||||
In Framework mode:
|
||||
|
||||
```ts filename=react-router.config.ts
|
||||
import type { Config } from "@react-router/dev/config";
|
||||
|
||||
export default {
|
||||
future: {
|
||||
v8_middleware: true,
|
||||
},
|
||||
} satisfies Config;
|
||||
```
|
||||
|
||||
In Data mode:
|
||||
|
||||
```ts
|
||||
import { createBrowserRouter } from "react-router/dom";
|
||||
|
||||
const router = createBrowserRouter(routes, {
|
||||
future: {
|
||||
v8_middleware: true,
|
||||
},
|
||||
});
|
||||
```
|
||||
|
||||
**Update your Code**
|
||||
|
||||
If you're using the `context` parameter in `loader` and `action` functions, you may need to update your code:
|
||||
|
||||
- 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).
|
||||
- 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.
|
||||
|
||||
## `future.v8_splitRouteModules`
|
||||
|
||||
[MODES: framework]
|
||||
|
||||
<br/>
|
||||
<br/>
|
||||
|
||||
**Background**
|
||||
|
||||
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.
|
||||
|
||||
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).
|
||||
|
||||
👉 **Enable the Flag**
|
||||
|
||||
```ts filename=react-router.config.ts
|
||||
import type { Config } from "@react-router/dev/config";
|
||||
|
||||
export default {
|
||||
future: {
|
||||
v8_splitRouteModules: true,
|
||||
},
|
||||
} satisfies Config;
|
||||
```
|
||||
|
||||
**Update your Code**
|
||||
|
||||
No code changes are required. This is an optimization feature that works automatically once enabled.
|
||||
|
||||
## `future.v8_viteEnvironmentApi`
|
||||
|
||||
[MODES: framework]
|
||||
|
||||
<br/>
|
||||
<br/>
|
||||
|
||||
**Background**
|
||||
|
||||
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+.
|
||||
|
||||
👉 **Enable the Flag**
|
||||
|
||||
```ts filename=react-router.config.ts
|
||||
import type { Config } from "@react-router/dev/config";
|
||||
|
||||
export default {
|
||||
future: {
|
||||
v8_viteEnvironmentApi: true,
|
||||
},
|
||||
} satisfies Config;
|
||||
```
|
||||
|
||||
**Update your Code**
|
||||
|
||||
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.
|
||||
|
||||
For example, a custom server build should move its SSR `rollupOptions` from the top-level `build` config into `environments.ssr.build`:
|
||||
|
||||
```diff filename=vite.config.ts
|
||||
import { reactRouter } from "@react-router/dev/vite";
|
||||
import { defineConfig } from "vite";
|
||||
|
||||
-export default defineConfig(({ isSsrBuild }) => ({
|
||||
- build: {
|
||||
- rollupOptions: isSsrBuild
|
||||
- ? {
|
||||
- input: "./server/app.ts",
|
||||
- }
|
||||
- : undefined,
|
||||
- },
|
||||
+export default defineConfig({
|
||||
+ environments: {
|
||||
+ ssr: {
|
||||
+ build: {
|
||||
+ rollupOptions: {
|
||||
+ input: "./server/app.ts",
|
||||
+ },
|
||||
+ },
|
||||
+ },
|
||||
+ },
|
||||
plugins: [reactRouter()],
|
||||
-}));
|
||||
+});
|
||||
```
|
||||
|
||||
See the [`node-custom-server` template][node-custom-server-template] for a complete example.
|
||||
|
||||
## `future.v8_passThroughRequests`
|
||||
|
||||
[MODES: framework]
|
||||
|
||||
<br/>
|
||||
<br/>
|
||||
|
||||
**Background**
|
||||
|
||||
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`.
|
||||
|
||||
This flag eliminates that normalization and passes the raw HTTP `request` instance to your handlers. This provides a few benefits:
|
||||
|
||||
- Reduces server-side overhead by eliminating multiple `new Request()` calls on the critical path
|
||||
- Allows you to distinguish document from data requests in your handlers based on the presence of a `.data` suffix (useful for [observability] purposes)
|
||||
|
||||
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.
|
||||
|
||||
👉 **Enable the Flag**
|
||||
|
||||
```ts filename=react-router.config.ts
|
||||
import type { Config } from "@react-router/dev/config";
|
||||
|
||||
export default {
|
||||
future: {
|
||||
v8_passThroughRequests: true,
|
||||
},
|
||||
} satisfies Config;
|
||||
```
|
||||
|
||||
**Update your Code**
|
||||
|
||||
If your code relies on inspecting the request URL, you should review it for any assumptions about the URL format:
|
||||
|
||||
```tsx
|
||||
// ❌ Before: assuming no `.data` suffix in `request.url` pathname
|
||||
export async function loader({
|
||||
request,
|
||||
}: Route.LoaderArgs) {
|
||||
let url = new URL(request.url);
|
||||
if (url.pathname === "/path") {
|
||||
// This check might now behave differently because the request pathname will
|
||||
// contain the `.data` suffix on data requests
|
||||
}
|
||||
}
|
||||
|
||||
// ✅ After: use `url` for normalized routing logic and `request.url`
|
||||
// for raw routing logic
|
||||
export async function loader({
|
||||
request,
|
||||
url,
|
||||
}: Route.LoaderArgs) {
|
||||
if (url.pathname === "/path") {
|
||||
// This will always have the `.data` suffix stripped
|
||||
}
|
||||
|
||||
// And now you can distinguish between document versus data requests
|
||||
let isDataRequest = new URL(
|
||||
request.url,
|
||||
).pathname.endsWith(".data");
|
||||
}
|
||||
```
|
||||
|
||||
## `future.v8_trailingSlashAwareDataRequests`
|
||||
|
||||
[MODES: framework]
|
||||
|
||||
<br/>
|
||||
<br/>
|
||||
|
||||
**Background**
|
||||
|
||||
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.
|
||||
|
||||
Currently, your HTTP and `request` pathnames would be as follows for `/a/b/c` and `/a/b/c/`
|
||||
|
||||
| URL `/a/b/c` | **HTTP pathname** | **`request` pathname`** |
|
||||
| ------------ | ----------------- | ----------------------- |
|
||||
| **Document** | `/a/b/c` | `/a/b/c` ✅ |
|
||||
| **Data** | `/a/b/c.data` | `/a/b/c` ✅ |
|
||||
|
||||
| URL `/a/b/c/` | **HTTP pathname** | **`request` pathname`** |
|
||||
| ------------- | ----------------- | ----------------------- |
|
||||
| **Document** | `/a/b/c/` | `/a/b/c/` ✅ |
|
||||
| **Data** | `/a/b/c.data` | `/a/b/c` ⚠️ |
|
||||
|
||||
With this flag enabled, these pathnames will be made consistent though a new `_.data` format for client-side `.data` requests:
|
||||
|
||||
| URL `/a/b/c` | **HTTP pathname** | **`request` pathname`** |
|
||||
| ------------ | ----------------- | ----------------------- |
|
||||
| **Document** | `/a/b/c` | `/a/b/c` ✅ |
|
||||
| **Data** | `/a/b/c.data` | `/a/b/c` ✅ |
|
||||
|
||||
| URL `/a/b/c/` | **HTTP pathname** | **`request` pathname`** |
|
||||
| ------------- | ------------------ | ----------------------- |
|
||||
| **Document** | `/a/b/c/` | `/a/b/c/` ✅ |
|
||||
| **Data** | `/a/b/c/_.data` ⬅️ | `/a/b/c/` ✅ |
|
||||
|
||||
This flag also aligns the root data request to match this behavior by changing it from `/_root.data` to `/_.data`.
|
||||
|
||||
👉 **Enable the Flag**
|
||||
|
||||
```ts filename=react-router.config.ts
|
||||
import type { Config } from "@react-router/dev/config";
|
||||
|
||||
export default {
|
||||
future: {
|
||||
v8_trailingSlashAwareDataRequests: true,
|
||||
},
|
||||
} satisfies Config;
|
||||
```
|
||||
|
||||
**Update your Code**
|
||||
|
||||
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.
|
||||
|
||||
## Unstable Future Flags (Optional)
|
||||
|
||||
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!
|
||||
|
||||
_No current unstable flags to document_
|
||||
|
||||
[api-development-strategy]: ../community/api-development-strategy
|
||||
[unstable]: ../community/api-development-strategy#unstable-flags
|
||||
[observability]: ../how-to/instrumentation
|
||||
[Response]: https://developer.mozilla.org/en-US/docs/Web/API/Response
|
||||
[vite-environment]: https://vite.dev/guide/api-environment
|
||||
[node-custom-server-template]: https://github.com/remix-run/react-router-templates/blob/7c617a435510bc3add3a5395c07bc65328b65e9e/node-custom-server/vite.config.ts
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
---
|
||||
title: Upgrading
|
||||
order: 2
|
||||
---
|
||||
+403
@@ -0,0 +1,403 @@
|
||||
---
|
||||
title: Upgrading from Remix
|
||||
order: 3
|
||||
---
|
||||
|
||||
# Upgrading from Remix
|
||||
|
||||
<docs-info>
|
||||
|
||||
React Router v7 requires the following minimum versions:
|
||||
|
||||
- `node@20`
|
||||
- `react@18`
|
||||
- `react-dom@18`
|
||||
|
||||
</docs-info>
|
||||
|
||||
React Router v7 is the next major version of Remix after v2 (see our ["Incremental Path to React 19" blog post][incremental-path-to-react-19] for more information).
|
||||
|
||||
If you have enabled all [Remix v2 future flags][v2-future-flags], upgrading from Remix v2 to React Router v7 mainly involves updating dependencies.
|
||||
|
||||
<docs-info>
|
||||
|
||||
The majority of steps 2-8 can be automatically updated using a [codemod][codemod] created by community member [James Restall][jrestall].
|
||||
|
||||
</docs-info>
|
||||
|
||||
## 1. Adopt future flags
|
||||
|
||||
**👉 Adopt future flags**
|
||||
|
||||
Adopt all existing [future flags][v2-future-flags] in your Remix v2 application.
|
||||
|
||||
## 2. Update dependencies
|
||||
|
||||
Most of the "shared" APIs that used to be re-exported through the runtime-specific packages (`@remix-run/node`, `@remix-run/cloudflare`, etc.) have all been collapsed into `react-router` in v7. So instead of importing from `@react-router/node` or `@react-router/cloudflare`, you'll import those directly from `react-router`.
|
||||
|
||||
```diff
|
||||
-import { redirect } from "@remix-run/node";
|
||||
+import { redirect } from "react-router";
|
||||
```
|
||||
|
||||
The only APIs you should be importing from the runtime-specific packages in v7 are APIs that are specific to that runtime, such as `createFileSessionStorage` for Node and `createWorkersKVSessionStorage` for Cloudflare.
|
||||
|
||||
**👉 Run the codemod (automated)**
|
||||
|
||||
You can automatically update your packages and imports with the following [codemod][codemod]. This codemod updates all of your packages and imports. Be sure to commit any pending changes before running the codemod, in case you need to revert.
|
||||
|
||||
```shellscript nonumber
|
||||
npx codemod remix/2/react-router/upgrade
|
||||
```
|
||||
|
||||
**👉 Install the new dependencies**
|
||||
|
||||
After the codemod updates your dependencies, you need to install the dependencies to remove Remix packages and add the new React Router packages.
|
||||
|
||||
```shellscript nonumber
|
||||
npm install
|
||||
```
|
||||
|
||||
**👉 Update your dependencies (manual)**
|
||||
|
||||
If you prefer not to use the codemod, you can manually update your dependencies.
|
||||
|
||||
<details>
|
||||
<summary>Expand to see a table of package name changes in alphabetical order</summary>
|
||||
|
||||
| Remix v2 Package | | React Router v7 Package |
|
||||
| ---------------------------------- | --- | ------------------------------------------- |
|
||||
| `@remix-run/architect` | ➡️ | `@react-router/architect` |
|
||||
| `@remix-run/cloudflare` | ➡️ | `@react-router/cloudflare` |
|
||||
| `@remix-run/dev` | ➡️ | `@react-router/dev` |
|
||||
| `@remix-run/express` | ➡️ | `@react-router/express` |
|
||||
| `@remix-run/fs-routes` | ➡️ | `@react-router/fs-routes` |
|
||||
| `@remix-run/node` | ➡️ | `@react-router/node` |
|
||||
| `@remix-run/react` | ➡️ | `react-router` |
|
||||
| `@remix-run/route-config` | ➡️ | `@react-router/dev` |
|
||||
| `@remix-run/routes-option-adapter` | ➡️ | `@react-router/remix-routes-option-adapter` |
|
||||
| `@remix-run/serve` | ➡️ | `@react-router/serve` |
|
||||
| `@remix-run/server-runtime` | ➡️ | `react-router` |
|
||||
| `@remix-run/testing` | ➡️ | `react-router` |
|
||||
|
||||
</details>
|
||||
|
||||
## 3. Change `scripts` in `package.json`
|
||||
|
||||
<docs-info>
|
||||
|
||||
If you used the codemod you can skip this step as it was automatically completed.
|
||||
|
||||
</docs-info>
|
||||
|
||||
**👉 Update the scripts in your `package.json`**
|
||||
|
||||
| Script | Remix v2 | | React Router v7 |
|
||||
| ----------- | ----------------------------------- | --- | ------------------------------------------ |
|
||||
| `dev` | `remix vite:dev` | ➡️ | `react-router dev` |
|
||||
| `build` | `remix vite:build` | ➡️ | `react-router build` |
|
||||
| `start` | `remix-serve build/server/index.js` | ➡️ | `react-router-serve build/server/index.js` |
|
||||
| `typecheck` | `tsc` | ➡️ | `react-router typegen && tsc` |
|
||||
|
||||
## 4. Add a `routes.ts` file
|
||||
|
||||
<docs-info>
|
||||
|
||||
If you used the codemod _and_ Remix v2 `v3_routeConfig` flag, you can skip this step as it was automatically completed.
|
||||
|
||||
</docs-info>
|
||||
|
||||
In React Router v7 you define your routes using the `app/routes.ts` file. View the [routing documentation][routing] for more information.
|
||||
|
||||
**👉 Update dependencies (if using Remix v2 `v3_routeConfig` flag)**
|
||||
|
||||
```diff filename=app/routes.ts
|
||||
-import { type RouteConfig } from "@remix-run/route-config";
|
||||
-import { flatRoutes } from "@remix-run/fs-routes";
|
||||
-import { remixRoutesOptionAdapter } from "@remix-run/routes-option-adapter";
|
||||
+import { type RouteConfig } from "@react-router/dev/routes";
|
||||
+import { flatRoutes } from "@react-router/fs-routes";
|
||||
+import { remixRoutesOptionAdapter } from "@react-router/remix-routes-option-adapter";
|
||||
|
||||
export default [
|
||||
// however your routes are defined
|
||||
] satisfies RouteConfig;
|
||||
```
|
||||
|
||||
**👉 Add a `routes.ts` file (if _not_ using Remix v2 `v3_routeConfig` flag)**
|
||||
|
||||
```shellscript nonumber
|
||||
touch app/routes.ts
|
||||
```
|
||||
|
||||
For backwards-compatibility, there are a few ways to adopt `routes.ts` to align with your route setup in Remix v2:
|
||||
|
||||
1. If you were using the "flat routes" [file-based convention][fs-routing], you can continue to use that via the new `@react-router/fs-routes` package:
|
||||
|
||||
```ts filename=app/routes.ts
|
||||
import { type RouteConfig } from "@react-router/dev/routes";
|
||||
import { flatRoutes } from "@react-router/fs-routes";
|
||||
|
||||
export default flatRoutes() satisfies RouteConfig;
|
||||
```
|
||||
|
||||
2. If you were using the "nested" convention from Remix v1 via the `@remix-run/v1-route-convention` package, you can continue using that as well in conjunction with `@react-router/remix-routes-option-adapter`:
|
||||
|
||||
```ts filename=app/routes.ts
|
||||
import { type RouteConfig } from "@react-router/dev/routes";
|
||||
import { remixRoutesOptionAdapter } from "@react-router/remix-routes-option-adapter";
|
||||
import { createRoutesFromFolders } from "@remix-run/v1-route-convention";
|
||||
|
||||
export default remixRoutesOptionAdapter(
|
||||
createRoutesFromFolders,
|
||||
) satisfies RouteConfig;
|
||||
```
|
||||
|
||||
3. If you were using the `routes` option to define config-based routes, you can keep that config via `@react-router/remix-routes-option-adapter`:
|
||||
|
||||
```ts filename=app/routes.ts
|
||||
import { type RouteConfig } from "@react-router/dev/routes";
|
||||
import { remixRoutesOptionAdapter } from "@react-router/remix-routes-option-adapter";
|
||||
|
||||
export default remixRoutesOptionAdapter(
|
||||
(defineRoutes) => {
|
||||
return defineRoutes((route) => {
|
||||
route("/", "home/route.tsx", { index: true });
|
||||
route("about", "about/route.tsx");
|
||||
route("", "concerts/layout.tsx", () => {
|
||||
route("trending", "concerts/trending.tsx");
|
||||
route(":city", "concerts/city.tsx");
|
||||
});
|
||||
});
|
||||
},
|
||||
) satisfies RouteConfig;
|
||||
```
|
||||
|
||||
- Be sure to also remove the `routes` option in your `vite.config.ts`:
|
||||
|
||||
```diff filename=vite.config.ts
|
||||
export default defineConfig({
|
||||
plugins: [
|
||||
remix({
|
||||
ssr: true,
|
||||
- ignoredRouteFiles: ['**/*'],
|
||||
- routes(defineRoutes) {
|
||||
- return defineRoutes((route) => {
|
||||
- route("/somewhere/cool/*", "catchall.tsx");
|
||||
- });
|
||||
- },
|
||||
})
|
||||
tsconfigPaths(),
|
||||
],
|
||||
});
|
||||
```
|
||||
|
||||
## 5. Add a React Router config
|
||||
|
||||
**👉 Add `react-router.config.ts` your project**
|
||||
|
||||
The config that was previously passed to the `remix` plugin in `vite.config.ts` is now exported from `react-router.config.ts`.
|
||||
|
||||
Note: At this point you should remove the v3 future flags you added in step 1.
|
||||
|
||||
```shellscript nonumber
|
||||
touch react-router.config.ts
|
||||
```
|
||||
|
||||
```diff filename=vite.config.ts
|
||||
export default defineConfig({
|
||||
plugins: [
|
||||
- remix({
|
||||
- ssr: true,
|
||||
- future: {/* all the v3 flags */}
|
||||
- }),
|
||||
+ reactRouter(),
|
||||
tsconfigPaths(),
|
||||
],
|
||||
});
|
||||
```
|
||||
|
||||
```diff filename=react-router.config.ts
|
||||
+import type { Config } from "@react-router/dev/config";
|
||||
+export default {
|
||||
+ ssr: true,
|
||||
+} satisfies Config;
|
||||
```
|
||||
|
||||
## 6. Add React Router plugin to `vite.config`
|
||||
|
||||
<docs-info>
|
||||
|
||||
If you used the codemod you can skip this step as it was automatically completed.
|
||||
|
||||
</docs-info>
|
||||
|
||||
**👉 Add `reactRouter` plugin to `vite.config`**
|
||||
|
||||
Change `vite.config.ts` to import and use the new `reactRouter` plugin from `@react-router/dev/vite`:
|
||||
|
||||
```diff filename=vite.config.ts
|
||||
-import { vitePlugin as remix } from "@remix-run/dev";
|
||||
+import { reactRouter } from "@react-router/dev/vite";
|
||||
import { defineConfig } from "vite";
|
||||
import tsconfigPaths from "vite-tsconfig-paths";
|
||||
|
||||
export default defineConfig({
|
||||
plugins: [
|
||||
- remix(),
|
||||
+ reactRouter(),
|
||||
tsconfigPaths(),
|
||||
],
|
||||
});
|
||||
```
|
||||
|
||||
## 7. Enable type safety
|
||||
|
||||
<docs-info>
|
||||
|
||||
If you are not using TypeScript, you can skip this step.
|
||||
|
||||
</docs-info>
|
||||
|
||||
React Router automatically generates types for your route modules into a `.react-router/` directory at the root of your app. This directory is fully managed by React Router and should be gitignore'd. Learn more about the [new type safety features][type-safety].
|
||||
|
||||
**👉 Add `.react-router/` to `.gitignore`**
|
||||
|
||||
```txt
|
||||
.react-router/
|
||||
```
|
||||
|
||||
**👉 Update `tsconfig.json`**
|
||||
|
||||
Update the `types` field in your `tsconfig.json` to include:
|
||||
|
||||
- `.react-router/types/**/*` path in the `include` field
|
||||
- The appropriate `@react-router/*` package in the `types` field
|
||||
- `rootDirs` for simplified relative imports
|
||||
|
||||
```diff filename=tsconfig.json
|
||||
{
|
||||
"include": [
|
||||
/* ... */
|
||||
+ ".react-router/types/**/*"
|
||||
],
|
||||
"compilerOptions": {
|
||||
- "types": ["@remix-run/node", "vite/client"],
|
||||
+ "types": ["@react-router/node", "vite/client"],
|
||||
/* ... */
|
||||
+ "rootDirs": [".", "./.react-router/types"]
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 8. Rename components in entry files
|
||||
|
||||
<docs-info>
|
||||
|
||||
If you used the codemod you can skip this step as it was automatically completed.
|
||||
|
||||
</docs-info>
|
||||
|
||||
If you have an `entry.server.tsx` and/or an `entry.client.tsx` file in your application, you will need to update the main components in these files:
|
||||
|
||||
```diff filename=app/entry.server.tsx
|
||||
-import { RemixServer } from "@remix-run/react";
|
||||
+import { ServerRouter } from "react-router";
|
||||
|
||||
-<RemixServer context={remixContext} url={request.url} />,
|
||||
+<ServerRouter context={remixContext} url={request.url} />,
|
||||
```
|
||||
|
||||
```diff filename=app/entry.client.tsx
|
||||
-import { RemixBrowser } from "@remix-run/react";
|
||||
+import { HydratedRouter } from "react-router/dom";
|
||||
|
||||
hydrateRoot(
|
||||
document,
|
||||
<StrictMode>
|
||||
- <RemixBrowser />
|
||||
+ <HydratedRouter />
|
||||
</StrictMode>,
|
||||
);
|
||||
```
|
||||
|
||||
## 9. Update types for `AppLoadContext`
|
||||
|
||||
<docs-info>
|
||||
|
||||
If you were using `remix-serve` you can skip this step. This is only applicable if you were using a custom server in Remix v2.
|
||||
|
||||
</docs-info>
|
||||
|
||||
Since React Router can be used as both a React framework _and_ a stand-alone routing library, the `context` argument for `LoaderFunctionArgs` and `ActionFunctionArgs` is now optional and typed as `any` by default. You can register types for your load context to get type safety for your loaders and actions.
|
||||
|
||||
👉 **Register types for your load context**
|
||||
|
||||
Before you migrate to the new `Route.LoaderArgs` and `Route.ActionArgs` types, you can temporarily augment `LoaderFunctionArgs` and `ActionFunctionArgs` with your load context type to ease migration.
|
||||
|
||||
```ts filename=app/env.ts
|
||||
declare module "react-router" {
|
||||
// Your AppLoadContext used in v2
|
||||
interface AppLoadContext {
|
||||
whatever: string;
|
||||
}
|
||||
|
||||
// TODO: remove this once we've migrated to `Route.LoaderArgs` instead for our loaders
|
||||
interface LoaderFunctionArgs {
|
||||
context: AppLoadContext;
|
||||
}
|
||||
|
||||
// TODO: remove this once we've migrated to `Route.ActionArgs` instead for our actions
|
||||
interface ActionFunctionArgs {
|
||||
context: AppLoadContext;
|
||||
}
|
||||
}
|
||||
|
||||
export {}; // necessary for TS to treat this as a module
|
||||
```
|
||||
|
||||
<docs-info>
|
||||
|
||||
Using `declare module` to register types is a standard TypeScript technique called [module augmentation][ts-module-augmentation].
|
||||
You can do this in any TypeScript file covered by your `tsconfig.json`'s `include` field, but we recommend a dedicated `env.ts` within your app directory.
|
||||
|
||||
</docs-info>
|
||||
|
||||
👉 **Use the new types**
|
||||
|
||||
Once you adopt the [new type generation][type-safety], you can remove the `LoaderFunctionArgs`/`ActionFunctionArgs` augmentations and use the `context` argument from [`Route.LoaderArgs`][server-loaders] and [`Route.ActionArgs`][server-actions] instead.
|
||||
|
||||
```ts filename=app/env.ts
|
||||
declare module "react-router" {
|
||||
// Your AppLoadContext used in v2
|
||||
interface AppLoadContext {
|
||||
whatever: string;
|
||||
}
|
||||
}
|
||||
|
||||
export {}; // necessary for TS to treat this as a module
|
||||
```
|
||||
|
||||
```ts filename=app/routes/my-route.tsx
|
||||
import type { Route } from "./+types/my-route";
|
||||
|
||||
export function loader({ context }: Route.LoaderArgs) {}
|
||||
// { whatever: string } ^^^^^^^
|
||||
|
||||
export function action({ context }: Route.ActionArgs) {}
|
||||
// { whatever: string } ^^^^^^^
|
||||
```
|
||||
|
||||
Congratulations! You are now on React Router v7. Go ahead and run your application to make sure everything is working as expected.
|
||||
|
||||
[incremental-path-to-react-19]: https://remix.run/blog/incremental-path-to-react-19
|
||||
[v2-future-flags]: https://remix.run/docs/start/future-flags
|
||||
[routing]: ../start/framework/routing
|
||||
[fs-routing]: ../how-to/file-route-conventions
|
||||
[v7-changelog-types]: https://github.com/remix-run/react-router/blob/release-next/CHANGELOG.md#type-safety-improvements
|
||||
[server-loaders]: ../start/framework/data-loading#server-data-loading
|
||||
[server-actions]: ../start/framework/actions#server-actions
|
||||
[ts-module-augmentation]: https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation
|
||||
[type-safety]: ../explanation/type-safety
|
||||
[codemod]: https://app.codemod.com/registry/remix/2/react-router/upgrade
|
||||
[jrestall]: https://github.com/jrestall
|
||||
+442
@@ -0,0 +1,442 @@
|
||||
---
|
||||
title: Framework Adoption from RouterProvider
|
||||
order: 5
|
||||
---
|
||||
|
||||
# Framework Adoption from RouterProvider
|
||||
|
||||
If you are not using `<RouterProvider>` please see [Framework Adoption from Component Routes][upgrade-component-routes] instead.
|
||||
|
||||
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).
|
||||
|
||||
## Features
|
||||
|
||||
The Vite plugin adds:
|
||||
|
||||
- Route loaders, actions, and automatic data revalidation
|
||||
- Type-safe Routes Modules
|
||||
- Automatic route code-splitting
|
||||
- Automatic scroll restoration across navigations
|
||||
- Optional Static pre-rendering
|
||||
- Optional Server rendering
|
||||
|
||||
The initial setup requires the most work. However, once complete, you can adopt new features incrementally.
|
||||
|
||||
## Prerequisites
|
||||
|
||||
To use the Vite plugin, your project requires:
|
||||
|
||||
- Node.js 20+ (if using Node as your runtime)
|
||||
- Vite 5+
|
||||
|
||||
## 1. Move route definitions into route modules
|
||||
|
||||
The React Router Vite plugin renders its own `RouterProvider`, so you can't render an existing `RouterProvider` within it. Instead, you will need to format all of your route definitions to match the [Route Module API][route-modules].
|
||||
|
||||
This step will take the longest, however there are several benefits to doing this regardless of adopting the React Router Vite plugin:
|
||||
|
||||
- Route modules will be lazy loaded, decreasing the initial bundle size of your app
|
||||
- Route definitions will be uniform, simplifying your app's architecture
|
||||
- Moving to route modules is incremental, you can migrate one route at a time
|
||||
|
||||
**👉 Move your route definitions into route modules**
|
||||
|
||||
Export each piece of your route definition as a separate named export, following the [Route Module API][route-modules].
|
||||
|
||||
```tsx filename=src/routes/about.tsx
|
||||
export async function clientLoader() {
|
||||
return {
|
||||
title: "About",
|
||||
};
|
||||
}
|
||||
|
||||
export default function About() {
|
||||
let data = useLoaderData();
|
||||
return <div>{data.title}</div>;
|
||||
}
|
||||
|
||||
// clientAction, ErrorBoundary, etc.
|
||||
```
|
||||
|
||||
**👉 Create a convert function**
|
||||
|
||||
Create a helper function to convert route module definitions into the format expected by your data router:
|
||||
|
||||
```tsx filename=src/main.tsx
|
||||
function convert(m: any) {
|
||||
let {
|
||||
clientLoader,
|
||||
clientAction,
|
||||
default: Component,
|
||||
...rest
|
||||
} = m;
|
||||
return {
|
||||
...rest,
|
||||
loader: clientLoader,
|
||||
action: clientAction,
|
||||
Component,
|
||||
};
|
||||
}
|
||||
```
|
||||
|
||||
**👉 Lazy load and convert your route modules**
|
||||
|
||||
Instead of importing your route modules directly, lazy load and convert them to the format expected by your data router.
|
||||
|
||||
Not only does your route definition now conform to the Route Module API, but you also get the benefits of code-splitting your routes.
|
||||
|
||||
```diff filename=src/main.tsx
|
||||
let router = createBrowserRouter([
|
||||
// ... other routes
|
||||
{
|
||||
path: "about",
|
||||
- loader: aboutLoader,
|
||||
- Component: About,
|
||||
+ lazy: () => import("./routes/about").then(convert),
|
||||
},
|
||||
// ... other routes
|
||||
]);
|
||||
```
|
||||
|
||||
Repeat this process for each route in your app.
|
||||
|
||||
## 2. Install the Vite plugin
|
||||
|
||||
Once all of your route definitions are converted to route modules, you can adopt the React Router Vite plugin.
|
||||
|
||||
**👉 Install the React Router Vite plugin**
|
||||
|
||||
```shellscript nonumber
|
||||
npm install -D @react-router/dev
|
||||
```
|
||||
|
||||
**👉 Install a runtime adapter**
|
||||
|
||||
We will assume you are using Node as your runtime.
|
||||
|
||||
```shellscript nonumber
|
||||
npm install @react-router/node
|
||||
```
|
||||
|
||||
**👉 Swap out the React plugin for React Router**
|
||||
|
||||
```diff filename=vite.config.ts
|
||||
-import react from '@vitejs/plugin-react'
|
||||
+import { reactRouter } from "@react-router/dev/vite";
|
||||
import { defineConfig } from "vite";
|
||||
|
||||
|
||||
export default defineConfig({
|
||||
plugins: [
|
||||
- react()
|
||||
+ reactRouter()
|
||||
],
|
||||
});
|
||||
```
|
||||
|
||||
## 3. Add the React Router config
|
||||
|
||||
**👉 Create a `react-router.config.ts` file**
|
||||
|
||||
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.
|
||||
|
||||
```shellscript nonumber
|
||||
touch react-router.config.ts
|
||||
```
|
||||
|
||||
```ts filename=react-router.config.ts
|
||||
import type { Config } from "@react-router/dev/config";
|
||||
|
||||
export default {
|
||||
appDirectory: "src",
|
||||
ssr: false,
|
||||
} satisfies Config;
|
||||
```
|
||||
|
||||
## 4. Add the Root entry point
|
||||
|
||||
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.
|
||||
|
||||
**👉 Move your existing `index.html` to `root.tsx`**
|
||||
|
||||
For example, if your current `index.html` looks like this:
|
||||
|
||||
```html filename=index.html
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta
|
||||
name="viewport"
|
||||
content="width=device-width, initial-scale=1.0"
|
||||
/>
|
||||
<title>My App</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="root"></div>
|
||||
<script type="module" src="/src/main.tsx"></script>
|
||||
</body>
|
||||
</html>
|
||||
```
|
||||
|
||||
You would move that markup into `src/root.tsx` and delete `index.html`:
|
||||
|
||||
```shellscript nonumber
|
||||
touch src/root.tsx
|
||||
```
|
||||
|
||||
```tsx filename=src/root.tsx
|
||||
import {
|
||||
Links,
|
||||
Meta,
|
||||
Outlet,
|
||||
Scripts,
|
||||
ScrollRestoration,
|
||||
} from "react-router";
|
||||
|
||||
export function Layout({
|
||||
children,
|
||||
}: {
|
||||
children: React.ReactNode;
|
||||
}) {
|
||||
return (
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charSet="UTF-8" />
|
||||
<meta
|
||||
name="viewport"
|
||||
content="width=device-width, initial-scale=1.0"
|
||||
/>
|
||||
<title>My App</title>
|
||||
<Meta />
|
||||
<Links />
|
||||
</head>
|
||||
<body>
|
||||
{children}
|
||||
<ScrollRestoration />
|
||||
<Scripts />
|
||||
</body>
|
||||
</html>
|
||||
);
|
||||
}
|
||||
|
||||
export default function Root() {
|
||||
return <Outlet />;
|
||||
}
|
||||
```
|
||||
|
||||
**👉 Move everything above `RouterProvider` to `root.tsx`**
|
||||
|
||||
Any global styles, context providers, etc. should be moved into `root.tsx` so they can be shared across all routes.
|
||||
|
||||
For example, if your `App.tsx` looks like this:
|
||||
|
||||
```tsx filename=src/App.tsx
|
||||
import "./index.css";
|
||||
|
||||
export default function App() {
|
||||
return (
|
||||
<OtherProviders>
|
||||
<AppLayout>
|
||||
<RouterProvider router={router} />
|
||||
</AppLayout>
|
||||
</OtherProviders>
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
You would move everything above the `RouterProvider` into `root.tsx`.
|
||||
|
||||
```diff filename=src/root.tsx
|
||||
+import "./index.css";
|
||||
|
||||
// ... other imports and Layout
|
||||
|
||||
export default function Root() {
|
||||
return (
|
||||
+ <OtherProviders>
|
||||
+ <AppLayout>
|
||||
<Outlet />
|
||||
+ </AppLayout>
|
||||
+ </OtherProviders>
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
## 5. Add client entry module (optional)
|
||||
|
||||
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.
|
||||
|
||||
If no `entry.client.tsx` exists, the React Router Vite plugin will use a default, hidden one.
|
||||
|
||||
**👉 Make `src/entry.client.tsx` your entry point**
|
||||
|
||||
If your current `src/main.tsx` looks like this:
|
||||
|
||||
```tsx filename=src/main.tsx
|
||||
import React from "react";
|
||||
import ReactDOM from "react-dom/client";
|
||||
import { BrowserRouter } from "react-router";
|
||||
import App from "./App";
|
||||
|
||||
const router = createBrowserRouter([
|
||||
// ... route definitions
|
||||
]);
|
||||
|
||||
ReactDOM.createRoot(
|
||||
document.getElementById("root")!,
|
||||
).render(
|
||||
<React.StrictMode>
|
||||
<RouterProvider router={router} />;
|
||||
</React.StrictMode>,
|
||||
);
|
||||
```
|
||||
|
||||
You would rename it to `entry.client.tsx` and change it to this:
|
||||
|
||||
```tsx filename=src/entry.client.tsx
|
||||
import React from "react";
|
||||
import ReactDOM from "react-dom/client";
|
||||
import { HydratedRouter } from "react-router/dom";
|
||||
|
||||
ReactDOM.hydrateRoot(
|
||||
document,
|
||||
<React.StrictMode>
|
||||
<HydratedRouter />
|
||||
</React.StrictMode>,
|
||||
);
|
||||
```
|
||||
|
||||
- Use `hydrateRoot` instead of `createRoot`
|
||||
- Render a `<HydratedRouter>` instead of your `<App/>` component
|
||||
- Note: We are no longer creating the routes and manually passing them to `<RouterProvider />`. We will migrate our route definitions in the next step.
|
||||
|
||||
## 6. Migrate your routes
|
||||
|
||||
The React Router Vite plugin uses a `routes.ts` file to configure your routes. The format will be pretty similar to the definitions of your data router.
|
||||
|
||||
**👉 Move definitions to a `routes.ts` file**
|
||||
|
||||
```shellscript nonumber
|
||||
touch src/routes.ts src/catchall.tsx
|
||||
```
|
||||
|
||||
Move your route definitions to `routes.ts`. Note that the schemas don't match exactly, so you will get type errors; we'll fix this next.
|
||||
|
||||
```diff filename=src/routes.ts
|
||||
+import type { RouteConfig } from "@react-router/dev/routes";
|
||||
|
||||
-const router = createBrowserRouter([
|
||||
+export default [
|
||||
{
|
||||
path: "/",
|
||||
lazy: () => import("./routes/layout").then(convert),
|
||||
children: [
|
||||
{
|
||||
index: true,
|
||||
lazy: () => import("./routes/home").then(convert),
|
||||
},
|
||||
{
|
||||
path: "about",
|
||||
lazy: () => import("./routes/about").then(convert),
|
||||
},
|
||||
{
|
||||
path: "todos",
|
||||
lazy: () => import("./routes/todos").then(convert),
|
||||
children: [
|
||||
{
|
||||
path: ":id",
|
||||
lazy: () =>
|
||||
import("./routes/todo").then(convert),
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
-]);
|
||||
+] satisfies RouteConfig;
|
||||
```
|
||||
|
||||
**👉 Replace the `lazy` loader with a `file` loader**
|
||||
|
||||
```diff filename=src/routes.ts
|
||||
export default [
|
||||
{
|
||||
path: "/",
|
||||
- lazy: () => import("./routes/layout").then(convert),
|
||||
+ file: "./routes/layout.tsx",
|
||||
children: [
|
||||
{
|
||||
index: true,
|
||||
- lazy: () => import("./routes/home").then(convert),
|
||||
+ file: "./routes/home.tsx",
|
||||
},
|
||||
{
|
||||
path: "about",
|
||||
- lazy: () => import("./routes/about").then(convert),
|
||||
+ file: "./routes/about.tsx",
|
||||
},
|
||||
{
|
||||
path: "todos",
|
||||
- lazy: () => import("./routes/todos").then(convert),
|
||||
+ file: "./routes/todos.tsx",
|
||||
children: [
|
||||
{
|
||||
path: ":id",
|
||||
- lazy: () => import("./routes/todo").then(convert),
|
||||
+ file: "./routes/todo.tsx",
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
] satisfies RouteConfig;
|
||||
```
|
||||
|
||||
[View our guide on configuring routes][configuring-routes] to learn more about the `routes.ts` file and helper functions to further simplify the route definitions.
|
||||
|
||||
## 7. Boot the app
|
||||
|
||||
At this point you should be fully migrated to the React Router Vite plugin. Go ahead and update your `dev` script and run the app to make sure everything is working.
|
||||
|
||||
**👉 Add `dev` script and run the app**
|
||||
|
||||
```json filename=package.json
|
||||
"scripts": {
|
||||
"dev": "react-router dev"
|
||||
}
|
||||
```
|
||||
|
||||
Now make sure you can boot your app at this point before moving on:
|
||||
|
||||
```shellscript
|
||||
npm run dev
|
||||
```
|
||||
|
||||
You will probably want to add `.react-router/` to your `.gitignore` file to avoid tracking unnecessary files in your repository.
|
||||
|
||||
```txt
|
||||
.react-router/
|
||||
```
|
||||
|
||||
You can checkout [Type Safety][type-safety] to learn how to fully setup and use autogenerated type safety for params, loader data, and more.
|
||||
|
||||
## Enable SSR and/or Pre-rendering
|
||||
|
||||
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.
|
||||
|
||||
```ts filename=react-router.config.ts
|
||||
import type { Config } from "@react-router/dev/config";
|
||||
|
||||
export default {
|
||||
ssr: true,
|
||||
async prerender() {
|
||||
return ["/", "/about", "/contact"];
|
||||
},
|
||||
} satisfies Config;
|
||||
```
|
||||
|
||||
[upgrade-component-routes]: ./component-routes
|
||||
[configuring-routes]: ../start/framework/routing
|
||||
[route-modules]: ../start/framework/route-module
|
||||
[type-safety]: ../how-to/route-module-type-safety
|
||||
+382
@@ -0,0 +1,382 @@
|
||||
---
|
||||
title: Upgrading from v6
|
||||
order: 2
|
||||
---
|
||||
|
||||
# Upgrading from v6
|
||||
|
||||
<docs-info>
|
||||
|
||||
React Router v7 requires the following minimum versions:
|
||||
|
||||
- `node@20`
|
||||
- `react@18`
|
||||
- `react-dom@18`
|
||||
|
||||
</docs-info>
|
||||
|
||||
The v7 upgrade has no breaking changes if you have enabled all future flags. These flags allow you to update your app one change at a time. We highly recommend you make a commit after each step and ship it instead of doing everything all at once.
|
||||
|
||||
## Update to latest v6.x
|
||||
|
||||
First update to the latest minor version of v6.x to have the latest future flags and console warnings.
|
||||
|
||||
👉 **Update to latest v6**
|
||||
|
||||
```shellscript nonumber
|
||||
npm install react-router-dom@6
|
||||
```
|
||||
|
||||
### v7_relativeSplatPath
|
||||
|
||||
**Background**
|
||||
|
||||
Changes the relative path matching and linking for multi-segment splats paths like `dashboard/*` (vs. just `*`). [View the CHANGELOG](https://github.com/remix-run/react-router/blob/main/CHANGELOG.md#futurev7_relativesplatpath) for more information.
|
||||
|
||||
👉 **Enable the flag**
|
||||
|
||||
Enabling the flag depends on the type of router:
|
||||
|
||||
```tsx
|
||||
<BrowserRouter
|
||||
future={{
|
||||
v7_relativeSplatPath: true,
|
||||
}}
|
||||
/>
|
||||
```
|
||||
|
||||
```tsx
|
||||
createBrowserRouter(routes, {
|
||||
future: {
|
||||
v7_relativeSplatPath: true,
|
||||
},
|
||||
});
|
||||
```
|
||||
|
||||
**Update your Code**
|
||||
|
||||
If you have any routes with a path + a splat like `<Route path="dashboard/*">` that have relative links like `<Link to="relative">` or `<Link to="../relative">` beneath them, you will need to update your code.
|
||||
|
||||
👉 **Split the `<Route>` into two**
|
||||
|
||||
Split any multi-segment splat `<Route>` into a parent route with the path and a child route with the splat:
|
||||
|
||||
```diff
|
||||
<Routes>
|
||||
<Route path="/" element={<Home />} />
|
||||
- <Route path="dashboard/*" element={<Dashboard />} />
|
||||
+ <Route path="dashboard">
|
||||
+ <Route path="*" element={<Dashboard />} />
|
||||
+ </Route>
|
||||
</Routes>
|
||||
|
||||
// or
|
||||
createBrowserRouter([
|
||||
{ path: "/", element: <Home /> },
|
||||
{
|
||||
- path: "dashboard/*",
|
||||
- element: <Dashboard />,
|
||||
+ path: "dashboard",
|
||||
+ children: [{ path: "*", element: <Dashboard /> }],
|
||||
},
|
||||
]);
|
||||
```
|
||||
|
||||
👉 **Update relative links**
|
||||
|
||||
Update any `<Link>` elements within that route tree to include the extra `..` relative segment to continue linking to the same place:
|
||||
|
||||
```diff
|
||||
function Dashboard() {
|
||||
return (
|
||||
<div>
|
||||
<h2>Dashboard</h2>
|
||||
<nav>
|
||||
<Link to="/">Dashboard Home</Link>
|
||||
- <Link to="team">Team</Link>
|
||||
- <Link to="projects">Projects</Link>
|
||||
+ <Link to="../team">Team</Link>
|
||||
+ <Link to="../projects">Projects</Link>
|
||||
</nav>
|
||||
|
||||
<Routes>
|
||||
<Route path="/" element={<DashboardHome />} />
|
||||
<Route path="team" element={<DashboardTeam />} />
|
||||
<Route
|
||||
path="projects"
|
||||
element={<DashboardProjects />}
|
||||
/>
|
||||
</Routes>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
```
|
||||
|
||||
### v7_startTransition
|
||||
|
||||
**Background**
|
||||
|
||||
This uses `React.useTransition` instead of `React.useState` for Router state updates. View the [CHANGELOG](https://github.com/remix-run/react-router/blob/main/CHANGELOG.md#futurev7_starttransition) for more information.
|
||||
|
||||
👉 **Enable the flag**
|
||||
|
||||
```tsx
|
||||
<BrowserRouter
|
||||
future={{
|
||||
v7_startTransition: true,
|
||||
}}
|
||||
/>
|
||||
|
||||
// or
|
||||
<RouterProvider
|
||||
future={{
|
||||
v7_startTransition: true,
|
||||
}}
|
||||
/>
|
||||
```
|
||||
|
||||
👉 **Update your Code**
|
||||
|
||||
You don't need to update anything unless you are using `React.lazy` _inside_ of a component.
|
||||
|
||||
Using `React.lazy` inside of a component is incompatible with `React.useTransition` (or other code that makes promises inside of components). Move `React.lazy` to the module scope and stop making promises inside of components. This is not a limitation of React Router but rather incorrect usage of React.
|
||||
|
||||
<docs-info>We added a flag to opt-out of `React.startTransition` in v7 so you can use that to upgrade to v7 without adopting React transition-enabled navigations if needed. See the [transition docs][transitions] for more information.</docs-info>
|
||||
|
||||
### v7_fetcherPersist
|
||||
|
||||
<docs-warning>If you are not using a `<RouterProvider>` you can skip this</docs-warning>
|
||||
|
||||
**Background**
|
||||
|
||||
The fetcher lifecycle is now based on when it returns to an idle state rather than when its owner component unmounts: [View the CHANGELOG](https://github.com/remix-run/react-router/blob/main/CHANGELOG.md#persistence-future-flag-futurev7_fetcherpersist) for more information.
|
||||
|
||||
**Enable the Flag**
|
||||
|
||||
```tsx
|
||||
createBrowserRouter(routes, {
|
||||
future: {
|
||||
v7_fetcherPersist: true,
|
||||
},
|
||||
});
|
||||
```
|
||||
|
||||
**Update your Code**
|
||||
|
||||
It's unlikely to affect your app. You may want to check any usage of `useFetchers` as they may persist longer than they did before. Depending on what you're doing, you may render something longer than before.
|
||||
|
||||
### v7_normalizeFormMethod
|
||||
|
||||
<docs-warning>If you are not using a `<RouterProvider>` you can skip this</docs-warning>
|
||||
|
||||
This normalizes `formMethod` fields as uppercase HTTP methods to align with the `fetch()` behavior. [View the CHANGELOG](https://github.com/remix-run/react-router/blob/main/CHANGELOG.md#futurev7_normalizeformmethod) for more information.
|
||||
|
||||
👉 **Enable the Flag**
|
||||
|
||||
```tsx
|
||||
createBrowserRouter(routes, {
|
||||
future: {
|
||||
v7_normalizeFormMethod: true,
|
||||
},
|
||||
});
|
||||
```
|
||||
|
||||
**Update your Code**
|
||||
|
||||
If any of your code is checking for lowercase HTTP methods, you will need to update it to check for uppercase HTTP methods (or call `toLowerCase()` on it).
|
||||
|
||||
👉 **Compare `formMethod` to UPPERCASE**
|
||||
|
||||
```diff
|
||||
-useNavigation().formMethod === "post"
|
||||
-useFetcher().formMethod === "get";
|
||||
+useNavigation().formMethod === "POST"
|
||||
+useFetcher().formMethod === "GET";
|
||||
```
|
||||
|
||||
### v7_partialHydration
|
||||
|
||||
<docs-warning>If you are not using a `<RouterProvider>` you can skip this</docs-warning>
|
||||
|
||||
This enables partial hydration of a data router which is primarily used for SSR frameworks, but it is also useful if you are using `lazy` to load your route modules. It's unlikely you need to worry about this, just turn the flag on. [View the CHANGELOG](https://github.com/remix-run/react-router/blob/main/CHANGELOG.md#partial-hydration) for more information.
|
||||
|
||||
👉 **Enable the Flag**
|
||||
|
||||
```tsx
|
||||
createBrowserRouter(routes, {
|
||||
future: {
|
||||
v7_partialHydration: true,
|
||||
},
|
||||
});
|
||||
```
|
||||
|
||||
**Update your Code**
|
||||
|
||||
With partial hydration, you need to provide a `HydrateFallback` component to render during initial hydration. Additionally, if you were using `fallbackElement` before, you need to remove it as it is now deprecated. In most cases, you will want to reuse the `fallbackElement` as the `HydrateFallback`.
|
||||
|
||||
👉 **Replace `fallbackElement` with `HydrateFallback`**
|
||||
|
||||
```diff
|
||||
const router = createBrowserRouter(
|
||||
[
|
||||
{
|
||||
path: "/",
|
||||
Component: Layout,
|
||||
+ HydrateFallback: Fallback,
|
||||
// or
|
||||
+ hydrateFallbackElement: <Fallback />,
|
||||
children: [],
|
||||
},
|
||||
],
|
||||
);
|
||||
|
||||
|
||||
<RouterProvider
|
||||
router={router}
|
||||
- fallbackElement={<Fallback />}
|
||||
/>
|
||||
```
|
||||
|
||||
### v7_skipActionErrorRevalidation
|
||||
|
||||
<docs-warning>If you are not using a `createBrowserRouter` you can skip this</docs-warning>
|
||||
|
||||
When this flag is enabled, loaders will no longer revalidate by default after an action throws/returns a `Response` with a `4xx`/`5xx` status code. You may opt-into revalidation in these scenarios via `shouldRevalidate` and the `actionStatus` parameter.
|
||||
|
||||
👉 **Enable the Flag**
|
||||
|
||||
```tsx
|
||||
createBrowserRouter(routes, {
|
||||
future: {
|
||||
v7_skipActionErrorRevalidation: true,
|
||||
},
|
||||
});
|
||||
```
|
||||
|
||||
**Update your Code**
|
||||
|
||||
In most cases, you probably won't have to make changes to your app code. Usually, if an action errors, it's unlikely data was mutated and needs revalidation. If any of your code _does_ mutate data in action error scenarios you have 2 options:
|
||||
|
||||
👉 **Option 1: Change the `action` to avoid mutations in error scenarios**
|
||||
|
||||
```js
|
||||
// Before
|
||||
async function action() {
|
||||
await mutateSomeData();
|
||||
if (detectError()) {
|
||||
throw new Response(error, { status: 400 });
|
||||
}
|
||||
await mutateOtherData();
|
||||
// ...
|
||||
}
|
||||
|
||||
// After
|
||||
async function action() {
|
||||
if (detectError()) {
|
||||
throw new Response(error, { status: 400 });
|
||||
}
|
||||
// All data is now mutated after validations
|
||||
await mutateSomeData();
|
||||
await mutateOtherData();
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
👉 **Option 2: Opt-into revalidation via `shouldRevalidate` and `actionStatus`**
|
||||
|
||||
```js
|
||||
async function action() {
|
||||
await mutateSomeData();
|
||||
if (detectError()) {
|
||||
throw new Response(error, { status: 400 });
|
||||
}
|
||||
await mutateOtherData();
|
||||
}
|
||||
|
||||
async function loader() { ... }
|
||||
|
||||
function shouldRevalidate({ actionStatus, defaultShouldRevalidate }) {
|
||||
if (actionStatus != null && actionStatus >= 400) {
|
||||
// Revalidate this loader when actions return a 4xx/5xx status
|
||||
return true;
|
||||
}
|
||||
return defaultShouldRevalidate;
|
||||
}
|
||||
```
|
||||
|
||||
## Deprecations
|
||||
|
||||
The `json` and `defer` methods are deprecated in favor of returning raw objects.
|
||||
|
||||
```diff
|
||||
async function loader() {
|
||||
- return json({ data });
|
||||
+ return { data };
|
||||
```
|
||||
|
||||
If you were using `json` to serialize your data to JSON, you can use the native [Response.json()][response-json] method instead.
|
||||
|
||||
## Upgrade to v7
|
||||
|
||||
Now that your app is caught up, you can simply update to v7 (theoretically!) without issue.
|
||||
|
||||
👉 **Install v7**
|
||||
|
||||
```shellscript nonumber
|
||||
npm install react-router-dom@latest
|
||||
```
|
||||
|
||||
👉 **Replace react-router-dom with react-router**
|
||||
|
||||
In v7 we no longer need `"react-router-dom"` as the packages have been simplified. You can import everything from `"react-router"`:
|
||||
|
||||
```shellscript nonumber
|
||||
npm uninstall react-router-dom
|
||||
npm install react-router@latest
|
||||
```
|
||||
|
||||
Note you only need `"react-router"` in your package.json.
|
||||
|
||||
👉 **Update imports**
|
||||
|
||||
Now you should update your imports to use `react-router`:
|
||||
|
||||
```diff
|
||||
-import { useLocation } from "react-router-dom";
|
||||
+import { useLocation } from "react-router";
|
||||
```
|
||||
|
||||
Instead of manually updating imports, you can use this command. Make sure your git working tree is clean though so you can revert if it doesn't work as expected.
|
||||
|
||||
```shellscript nonumber
|
||||
find ./path/to/src \( -name "*.tsx" -o -name "*.ts" -o -name "*.js" -o -name "*.jsx" \) -type f -exec sed -i '' 's|from "react-router-dom"|from "react-router"|g' {} +
|
||||
```
|
||||
|
||||
If you have GNU `sed` installed (most Linux distributions), use this command instead:
|
||||
|
||||
```shellscript nonumber
|
||||
find ./path/to/src \( -name "*.tsx" -o -name "*.ts" -o -name "*.js" -o -name "*.jsx" \) -type f -exec sed -i 's|from "react-router-dom"|from "react-router"|g' {} +
|
||||
```
|
||||
|
||||
👉 **Update DOM-specific imports**
|
||||
|
||||
`RouterProvider` and `HydratedRouter` come from a deep import because they depend on `"react-dom"`:
|
||||
|
||||
```diff
|
||||
-import { RouterProvider } from "react-router-dom";
|
||||
+import { RouterProvider } from "react-router/dom";
|
||||
```
|
||||
|
||||
Note you should use a top-level import for non-DOM contexts, such as Jest tests:
|
||||
|
||||
```diff
|
||||
-import { RouterProvider } from "react-router-dom";
|
||||
+import { RouterProvider } from "react-router";
|
||||
```
|
||||
|
||||
Congratulations, you're now on v7!
|
||||
|
||||
[react-flushsync]: https://react.dev/reference/react-dom/flushSync
|
||||
[response-json]: https://developer.mozilla.org/en-US/docs/Web/API/Response/json
|
||||
[data-util]: https://api.reactrouter.com/v7/functions/react-router.data.html
|
||||
[transitions]: ../explanation/react-transitions
|
||||
Reference in New Issue
Block a user