|
| 1 | +--- |
| 2 | +pcx_content_type: how-to |
| 3 | +title: React Router (formerly Remix) |
| 4 | +description: Create a React Router application and deploy it to Cloudflare Workers |
| 5 | +--- |
| 6 | + |
| 7 | +import { |
| 8 | + Render, |
| 9 | + PackageManagers, |
| 10 | + Steps, |
| 11 | + Details, |
| 12 | + FileTree, |
| 13 | +} from "~/components"; |
| 14 | + |
| 15 | +**Start from CLI**: Scaffold a full-stack app with [React Router v7](https://reactrouter.com/) and the [Cloudflare Vite plugin](/workers/vite-plugin/) for lightning-fast development. |
| 16 | + |
| 17 | +<PackageManagers |
| 18 | + type="create" |
| 19 | + pkg="cloudflare@latest my-react-router-app" |
| 20 | + args="--framework=react-router" |
| 21 | +/> |
| 22 | + |
| 23 | +**Or just deploy**: Create a full-stack app using React Router v7, with CI/CD and previews all set up for you. |
| 24 | + |
| 25 | +[](https://deploy.workers.cloudflare.com/?url=https://github.com/cloudflare/templates/tree/main/react-router-starter-template) |
| 26 | + |
| 27 | +## What is React Router? |
| 28 | + |
| 29 | +[React Router v7](https://reactrouter.com/) is a full-stack React framework for building web applications. |
| 30 | +It combines with the [Cloudflare Vite plugin](/workers/vite-plugin/) to provide a first-class experience for developing, building and deploying your apps on Cloudflare. |
| 31 | + |
| 32 | +## Creating a full-stack React Router app |
| 33 | + |
| 34 | +<Steps> |
| 35 | +1. **Create a new project with the create-cloudflare CLI (C3)** |
| 36 | + |
| 37 | + <PackageManagers |
| 38 | + type="create" |
| 39 | + pkg="cloudflare@latest my-react-router-app" |
| 40 | + args="--framework=react-router" |
| 41 | + /> |
| 42 | + <Details header="How is this project set up?"> |
| 43 | + Below is a simplified file tree of the project. |
| 44 | + <FileTree> |
| 45 | + - my-react-router-app |
| 46 | + - app |
| 47 | + - routes |
| 48 | + - ... |
| 49 | + - entry.server.ts |
| 50 | + - root.tsx |
| 51 | + - routes.ts |
| 52 | + - workers |
| 53 | + - app.ts |
| 54 | + - react-router.config.ts |
| 55 | + - vite.config.ts |
| 56 | + - wrangler.jsonc |
| 57 | + </FileTree> |
| 58 | + |
| 59 | + `react-router.config.ts` is your [React Router config file](https://reactrouter.com/explanation/special-files#react-routerconfigts). |
| 60 | + In this file: |
| 61 | + - `ssr` is set to `true`, meaning that your application will use server-side rendering. |
| 62 | + - `future.unstable_viteEnvironmentApi` is set to `true` to enable compatibility with the [Cloudflare Vite plugin](/workers/vite-plugin/). |
| 63 | + |
| 64 | + :::note |
| 65 | + SPA mode and prerendering are not currently supported when using the [Cloudflare Vite plugin](/workers/vite-plugin/). |
| 66 | + If you wish to use React Router in an SPA then we recommend starting with the [React template](/workers/frameworks/framework-guides/react/) and using React Router [as a library](https://reactrouter.com/start/data/installation). |
| 67 | + ::: |
| 68 | + |
| 69 | + `vite.config.ts` is your [Vite config file](https://vite.dev/config/). |
| 70 | + The React Router and Cloudflare plugins are included in the `plugins` array. |
| 71 | + The [Cloudflare Vite plugin](/workers/vite-plugin/) runs your server code in the Workers runtime, ensuring your local development environment is as close to production as possible. |
| 72 | + |
| 73 | + `wrangler.jsonc` is your [Worker config file](/workers/wrangler/configuration/). |
| 74 | + In this file: |
| 75 | + - `main` points to `./workers/app.ts`. |
| 76 | + This is the entry file for your Worker. |
| 77 | + The default export includes a [`fetch` handler](/workers/runtime-apis/fetch/), which delegates the request to React Router. |
| 78 | + - If you want to add [bindings](/workers/runtime-apis/bindings/) to resources on Cloudflare's developer platform, you configure them here. |
| 79 | + </Details> |
| 80 | + |
| 81 | +2. **Develop locally** |
| 82 | + |
| 83 | + After creating your project, run the following command in your project directory to start a local development server. |
| 84 | + <PackageManagers type="run" args="dev" /> |
| 85 | + <Details header="What's happening in local development?"> |
| 86 | + This project uses React Router in combination with the [Cloudflare Vite plugin](/workers/vite-plugin/). |
| 87 | + This means that your application runs in the Cloudflare Workers runtime, just like in production, and enables access to local emulations of bindings. |
| 88 | + </Details> |
| 89 | + |
| 90 | +3. **Deploy your project** |
| 91 | + |
| 92 | + Your project can be deployed to a `*.workers.dev` subdomain or a [Custom Domain](/workers/configuration/routing/custom-domains/) from your own machine or from any CI/CD system, including Cloudflare's own [Workers Builds](/workers/ci-cd/builds/). |
| 93 | + |
| 94 | + The following command will build and deploy your project. If you are using CI, ensure you update your ["deploy command"](/workers/ci-cd/builds/configuration/#build-settings) configuration appropriately. |
| 95 | + |
| 96 | + <PackageManagers type="run" args={"deploy"} /> |
| 97 | + |
| 98 | +</Steps> |
| 99 | + |
| 100 | +## Use bindings with React Router |
| 101 | + |
| 102 | +With bindings, your application can be fully integrated with the Cloudflare Developer Platform, giving you access to compute, storage, AI and more. |
| 103 | +As you have direct access to your Worker entry file (`workers/app.ts`), you can also add additional exports such as [Durable Objects](/durable-objects/) and [Workflows](/workflows/). |
| 104 | + |
| 105 | +<Render file="frameworks-bindings" /> |
0 commit comments