Overview
The@react-router/cloudflare package provides Cloudflare platform abstractions for React Router, enabling deployment to Cloudflare Pages and Workers.
Installation
Cloudflare Pages
Deploy your React Router app as a Cloudflare Pages Function.Basic Setup
Create afunctions/[[path]].ts file in your project:
With Custom Load Context
Pass environment bindings and Cloudflare-specific context to your routes:Cloudflare Workers
For standalone Workers deployments, use thecreateRequestHandler function.
Basic Worker
With Environment Bindings
Access KV namespaces, Durable Objects, and other bindings:Session Storage
Use Cloudflare Workers KV for session storage:Load Context API
The Cloudflare load context includes:env: Environment bindings (KV, Durable Objects, secrets, etc.)cf: Request metadata (geolocation, colo, etc.)ctx.waitUntil: Extend request lifetime for background tasksctx.passThroughOnException: Fail open on errorscaches: Cache API for Workers
wrangler.toml Configuration
Configure your Worker inwrangler.toml:
Deployment
Cloudflare Pages
-
Build your application:
-
Deploy with Wrangler:
Cloudflare Workers
-
Build your application:
-
Deploy with Wrangler:
Best Practices
- Store secrets in environment variables, not in code
- Use KV namespaces for session storage and caching
- Leverage
ctx.waitUntilfor analytics and non-blocking operations - Enable Cloudflare caching for static assets
- Use Durable Objects for stateful applications
The Cloudflare runtime has different APIs than Node.js. Use Web APIs (fetch, streams, etc.) instead of Node.js-specific modules.