Skip to main content

React + Vite

还没翻译译自英文(developers.cloudflare.com)本页 76 段都还没译

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.

Create new projectDeploy existing 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.

npmyarnpnpm

npm create cloudflare@latest -- my-react-app --framework=react

Or just deploy - create a full-stack app using React, a Workers API, and Vite, with CI/CD and previews all set up for you.

Deploy to Workers

  1. Create a new project with the create-cloudflare CLI (C3)

    npmyarnpnpm

    npm create cloudflare@latest -- my-react-app --framework=react

    How is this project set up?

  2. 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.

    npmyarnpnpm

    npm run dev

    What's happening in local development?

  3. Deploy your project

    Your project can be deployed to a *.workers.dev subdomain 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.

    npmyarnpnpm

    npm run deploy

Asset Routing

If you're using React as a SPA, you will want to set not_found_handling = "single-page-application" in your Wrangler configuration file.

By default, Cloudflare first tries to match a request path against a static asset path, which is based on the file structure of the uploaded asset directory. This is either the directory specified by assets.directory in your Wrangler config or, in the case of the Cloudflare Vite plugin, the output directory of the client build. Failing that, we invoke a Worker if one is present. If there is no Worker, or the Worker then uses the asset binding, Cloudflare will fallback to the behaviour set by not_found_handling.

Refer to the routing documentation for more information about how routing works with static assets, and how to customize this behavior.

Use bindings with React

Your project can also contain a Worker at ./worker/index.ts, which you can use as a backend API for your React application. While your React application cannot directly access Workers bindings, it can interact with them through this Worker. You can make fetch() requests from your React application to the Worker, which can then handle the request and use bindings. Learn how to configure Workers bindings.

With bindings, your application can be fully integrated with the Cloudflare Developer Platform, giving you access to compute, storage, AI and more.

Bindings

Access to compute, storage, AI and more.