React Router on Cloudflare Example
This example shows how to deploy a React Router v7 application to Cloudflare Workers with environment bindings and secrets.Features
- React Router v7: File-based routing with server rendering
- Cloudflare Workers: Edge deployment
- Environment Bindings: Secrets and configuration
- Type Safety: Full TypeScript support
Project Setup
import alchemy from "alchemy";
import { ReactRouter } from "alchemy/cloudflare";
const app = await alchemy("cloudflare-react-router");
export const website = await ReactRouter("website", {
name: `${app.name}-${app.stage}-website`,
bindings: {
ALCHEMY_TEST_VALUE: alchemy.secret("Hello from Alchemy!"),
},
});
console.log({
url: website.url,
});
await app.finalize();
import type { Config } from "@react-router/dev/config";
export default {
appDirectory: "app",
ssr: true,
} satisfies Config;
import { useLoaderData } from "react-router";
export async function loader({ context }: { context: any }) {
// Access bindings from Alchemy
const message = context.cloudflare.env.ALCHEMY_TEST_VALUE;
return {
message,
timestamp: new Date().toISOString(),
};
}
export default function Index() {
const data = useLoaderData<typeof loader>();
return (
<div>
<h1>React Router on Cloudflare</h1>
<p>Message: {data.message}</p>
<p>Timestamp: {data.timestamp}</p>
</div>
);
}
import type { website } from "./alchemy.run";
declare module "react-router" {
interface AppLoadContext {
cloudflare: {
env: typeof website.Env;
};
}
}