本页还没有译文
本页 21 段还没译成繁體中文,下面显示的是英文原文。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-routeryarn create cloudflare my-react-router-app --framework=react-routerpnpm create cloudflare@latest my-react-router-app --framework=react-routerOr just deploy: Create a full-stack app using React Router v8, with CI/CD and previews all set up for you.
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 deployyarn wrangler deploypnpm wrangler deployLearn 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
-
Create a new project with the create-cloudflare CLI (C3)
npm create cloudflare@latest -- my-react-router-app --framework=react-routeryarn create cloudflare my-react-router-app --framework=react-routerpnpm create cloudflare@latest my-react-router-app --framework=react-routerHow is this project set up?
Below is a simplified file tree of the project.
my-react-router-app
app
routes
- ⋯
- entry.server.ts
- root.tsx
- routes.ts
workers
- app.ts
- react-router.config.ts
- vite.config.ts
- wrangler.jsonc
react-router.config.tsis your React Router config file. In this file:ssris set totrue, meaning that your application will use server-side rendering.
vite.config.tsis your Vite config file. The React Router and Cloudflare plugins are included in thepluginsarray. The Cloudflare Vite plugin runs your server code in the Workers runtime, ensuring your local development environment is as close to production as possible.wrangler.jsoncis your Worker config file. In this file:mainpoints to./workers/app.ts. This is the entry file for your Worker. The default export includes afetchhandler, which delegates the request to React Router.- If you want to add bindings to resources on Cloudflare's developer platform, you configure them here.
-
Develop locally
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 React Router in combination with the Cloudflare Vite plugin. This means that your application runs 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
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:
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
Here is an example of how to set up a simple Workflow in your Worker entry file.
import { createRequestHandler } from "react-router";
import { WorkflowEntrypoint, type WorkflowStep, type WorkflowEvent } from 'cloudflare:workers';
declare global {
interface CloudflareEnvironment extends Env {}
}
type Env = {
MY_WORKFLOW: Workflow;
};
export class MyWorkflow extends WorkflowEntrypoint<Env> {
override async run(event: WorkflowEvent<{ hello: string }>, step: WorkflowStep) {
await step.do("first step", async () => {
return { output: "First step result" };
});
await step.sleep("sleep", "1 second");
await step.do("second step", async () => {
return { output: "Second step result" };
});
return "Workflow output";
}
}
const requestHandler = createRequestHandler(
() => import("virtual:react-router/server-build"),
import.meta.env.MODE
);
export default {
async fetch(request, env, ctx) {
return requestHandler(request, {
cloudflare: { env, ctx },
});
},
} satisfies ExportedHandler<CloudflareEnvironment>;Configure it in your Wrangler configuration file:
{
"workflows": [
{
"name": "my-workflow",
"binding": "MY_WORKFLOW",
"class_name": "MyWorkflow"
}
]
}[[workflows]]
name = "my-workflow"
binding = "MY_WORKFLOW"
class_name = "MyWorkflow"
And then use it in your application:
export async function action({ context }: Route.ActionArgs) {
const env = context.cloudflare.env;
const instance = await env.MY_WORKFLOW.create({ params: { "hello": "world" } })
return { id: instance.id, details: await instance.status() };
}With bindings, your application can be fully integrated with the Cloudflare Developer Platform, giving you access to compute, storage, AI and more.