Skip to main content

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 a functions/[[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 the createRequestHandler function.

Basic Worker

With Environment Bindings

Access KV namespaces, Durable Objects, and other bindings:

Session Storage

Use Cloudflare Workers KV for session storage:
Use in your routes:

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 tasks
  • ctx.passThroughOnException: Fail open on errors
  • caches: Cache API for Workers
Access these in your loaders and actions:

wrangler.toml Configuration

Configure your Worker in wrangler.toml:

Deployment

Cloudflare Pages

  1. Build your application:
  2. Deploy with Wrangler:

Cloudflare Workers

  1. Build your application:
  2. Deploy with Wrangler:

Best Practices

  • Store secrets in environment variables, not in code
  • Use KV namespaces for session storage and caching
  • Leverage ctx.waitUntil for 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.