React ↗ is a framework for building user interfaces. It allows you to create reusable UI components and manage the state of your application efficiently. You can use React to build a single-page application (SPA), and combine it with a backend API running on Cloudflare Workers to create a full-stack application.
This guide shows you how to deploy a React + Vite application to Cloudflare Workers. You can either create a new project using the create-cloudflare CLI (C3) or adapt an existing React + Vite project.
Start from CLI - scaffold a full-stack app with a React SPA, Cloudflare Workers API, and the Cloudflare Vite plugin for lightning-fast development.
npm create cloudflare@latest -- my-react-app --framework=reactyarn create cloudflare my-react-app --framework=reactpnpm create cloudflare@latest my-react-app --framework=reactOr just deploy - create a full-stack app using React, a Workers API, and Vite, with CI/CD and previews all set up for you.
-
Create a new project with the create-cloudflare CLI (C3)
npm create cloudflare@latest -- my-react-app --framework=reactyarn create cloudflare my-react-app --framework=reactpnpm create cloudflare@latest my-react-app --framework=reactHow is this project set up?
The following is a simplified file tree of the project.
- my-react-app
- src/
- App.tsx
- worker/
- index.ts
- index.html
- vite.config.ts
- wrangler.jsonc
- src/
wrangler.jsoncis your Wrangler configuration file. In this file:mainpoints toworker/index.ts. This is your Worker, which is going to act as your backend API.assets.not_found_handlingis set tosingle-page-application, which means that routes that are handled by your React SPA do not go to the Worker, and are thus free.- If you want to add bindings to resources on Cloudflare's developer platform, you configure them here. Read more about bindings.
vite.config.tsis set up to use the Cloudflare Vite plugin. This runs your Worker in the Cloudflare Workers runtime, ensuring your local development environment is as close to production as possible.worker/index.tsis your backend API, which contains a single endpoint,/api/, that returns a text response. Atsrc/App.tsx, your React app calls this endpoint to get a message back and displays this. - my-react-app
-
Develop locally with the Cloudflare Vite plugin
After creating your project, run the following command in your project directory to start a local development server.
npm run devyarn run devpnpm run devWhat's happening in local development?
This project uses Vite for local development and build, and thus comes with all of Vite's features, including hot module replacement (HMR).
In addition,
vite.config.tsis set up to use the Cloudflare Vite plugin. This runs your application in the Cloudflare Workers runtime, just like in production, and enables access to local emulations of bindings. -
Deploy your project
Your project can be deployed to a
*.workers.devsubdomain or a Custom Domain, from your own machine or from any CI/CD system, including Cloudflare's own Workers Builds.The following command will build and deploy your project. If you are using CI, ensure you update your "deploy command" configuration appropriately.
npm run deployyarn run deploypnpm run deploy
If you already have a React + Vite application, you can adapt it to deploy to Cloudflare Workers using the Cloudflare Vite plugin. This approach preserves your existing code while adding the ability to deploy to Cloudflare's edge network with static assets and an optional API Worker.
-
Navigate to your project directory
Open your existing React + Vite project in your editor of choice. If you do not have one yet, scaffold a new project with Vite first:
npm create vite@latest -- my-react-app --template react-tsyarn create vite my-react-app --template react-tspnpm create vite@latest my-react-app --template react-tsNext, open the
my-react-appdirectory in your editor of choice. -
Add the Cloudflare Vite plugin
npm i -D @cloudflare/vite-plugin wrangleryarn add -D @cloudflare/vite-plugin wranglerpnpm add -D @cloudflare/vite-plugin wranglerbun add -d @cloudflare/vite-plugin wranglerIn your
vite.config.ts, add the Cloudflare Vite plugin after your framework plugin:vite.config.tsts import { defineConfig } from "vite"; import react from "@vitejs/plugin-react"; import { cloudflare } from "@cloudflare/vite-plugin"; export default defineConfig({ plugins: [react(), cloudflare()],