A modern, production-ready template for building full-stack React applications using React Router, hosted on Cloudflare Workers with D1 as the database.
You can quickly create a new React Router application from this template by running:
npx create-react-router@latest --template matthewlynch/react-router-cloudflare-d1
Some of the code in this repo was adapted from the React Router Cloudflare D1 template.
- 🚀 Server-side rendering
- ⚡️ Hot Module Replacement (HMR)
- 📦 Asset bundling and optimization
- 🔄 Data loading and mutations
- 🔒 TypeScript by default
- 🎉 TailwindCSS for styling
- 🟧️ Setup to deploy to Cloudflare Workers
- 📊 Cloudflare D1 database for production and SQLite database for local development
- 📜 Pre-render routes at build time
- 🌍 Separate environments for preview and production
- 📟
cloudflareDevProxy
to make Cloudflare bindings work locally - 📖 React Router docs
- 📖 Cloudflare Workers docs
- 📖 D1 database docs
- Run
cp .dev-example.vars .dev.vars
to create an .env file you can use to override variables defined inwrangler.toml
or set secret values you don't want to check into source control - Update the
name
field inwranlger.toml
- Install dependencies
pnpm install
- Create a database by running
wrangler d1 create <name>
and updatewranlger.toml
with the UUID and name for the database- Create an additional database for the "preview" environment and update
env.preview.d1_databases
inwranlger.toml
with the UUID and name for the preview database - OR delete
env.preview*
if you don't want to deploy a preview version of your app
- Create an additional database for the "preview" environment and update
- Add your Cloudflare Account ID/Database UUID/Token to
.dev.vars
(you only need this when you want to view data via Drizzle Studio for your remote database) - Run
pnpm typegen
any time you make changes towranlger.toml
to ensure types from bindings and module rules are up to date for type safety
Run an initial database migration:
pnpm db:migrate
Start the development server with HMR:
pnpm dev
Your application will be available at http://localhost:5173
.
You can develop against a local SQLite database then push changes to your remote D1 database.
- Make changes to the schema in
./database/schema.ts
- Run
pnpm db:generate
to generate SQL migration files - Run
pnpm db:migrate
to apply the generated migration files to your local SQLite database - Run
pnpm db:migrate:production
to apply the changes to your remote D1 database
Run pnpm db:studio
to browse data in your local database on disk or pnpm db:studio:production
to browse your remote D1 database.
You need to have added your Cloudflare Account ID/Database UUID/Token to .dev.vars
if you want to run pnpm db:studio:production
.
- Log in to Cloudflare and visit https://dash.cloudflare.com/profile/api-tokens and click "Create Token"
- Scroll down to "Create Custom Token" and click on "Get Started"
- Enter a name for the token
- "Permissions" needs to be set to "Account" / "D1" / "Edit"
- Click "Continue to summary" and copy the token value so you can set the
CLOUDFLARE_TOKEN
environment variable in.dev.vars
Create a production build:
pnpm build
Deployment is done using the Wrangler CLI.
Make sure you have run pnpm db:generate
& pnpm db:migrate:production
so the deployed app can query the database.
To deploy directly to production from your machine:
npx wrangler deploy
Or to deploy to the preview environment:
npx wrangler versions upload --env preview
The CLI will output the URL you can use to view the app and the UUID of the deployment.
You can then promote a version to production after verification or roll it out progressively.
npx wrangler versions deploy
Select the UUID of the deployment you want to promote.
You can configure CD via GitHub once you have run the deployment steps above
- Visit the "Workers & Pages" page in the Cloudflare Dashboard
- Click on the worker you deployed (the name will match what is defined in
wrangler.toml#name
field) - Click "Settings"
- Scroll down to "Build" and click on "Connect"
- Select your repository and branch
- Click "connect"
- Push changes to the repo for automatic deployments
This template comes with Tailwind CSS already configured for a simple default starting experience. You can use whatever CSS framework you prefer.
Built with ❤️ by Matt