Skip to main content

React Router (formerly Remix)

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

Start from CLI: Scaffold a full-stack app with React Router v8 and the Cloudflare Vite plugin for lightning-fast development.

npm create cloudflare@latest -- my-react-router-app --framework=react-router
yarn create cloudflare my-react-router-app --framework=react-router
pnpm create cloudflare@latest my-react-router-app --framework=react-router

Or just deploy: Create a full-stack app using React Router v8, with CI/CD and previews all set up for you.

Deploy to Workers

Note

SPA mode and prerendering are not currently supported when using the Cloudflare Vite plugin. If you wish to use React Router in an SPA then we recommend starting with the React template and using React Router as a library.

Already have a React Router project?

Run wrangler deploy in a project without a Wrangler configuration file and Wrangler will automatically detect React Router, generate the necessary configuration, and deploy your project.

npx wrangler deploy
yarn wrangler deploy
pnpm wrangler deploy

Learn more about automatic project configuration.

What is React Router?

React Router v8 is a full-stack React framework for building web applications. It combines with the Cloudflare Vite plugin to provide a first-class experience for developing, building and deploying your apps on Cloudflare.

Creating a full-stack React Router app

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

    npm create cloudflare@latest -- my-react-router-app --framework=react-router
    yarn create cloudflare my-react-router-app --framework=react-router
    pnpm create cloudflare@latest my-react-router-app --framework=react-router

    How is this project set up?

  2. Develop locally

    After creating your project, run the following command in your project directory to start a local development server.

    npm run dev
    yarn run dev
    pnpm 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.

    npm run deploy
    yarn run deploy
    pnpm run deploy

Use bindings with React Router

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

Once you have configured the bindings in the Wrangler configuration file, they are then available within context.cloudflare in your loader or action functions:

app/routes/home.tsx
export function loader({ context }: Route.LoaderArgs) {
	return { message: context.cloudflare.env.VALUE_FROM_CLOUDFLARE };
}

export default function Home({ loaderData }: Route.ComponentProps) {
	return <Welcome message={loaderData.message} />;
}

As you have direct access to your Worker entry file (workers/app.ts), you can also add additional exports such as Durable Objects and Workflows

Example: Using Workflows

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.