A modern web application template using Astro, Tailwind CSS, Cloudflare Pages, D1 Database, and R2 Storage.
- ⚡️ Astro 5 - Fast, modern web framework
- 🎨 Tailwind CSS 4 - Utility-first CSS framework
- 📊 Cloudflare D1 - Serverless SQL database
- 📦 Cloudflare R2 - Object storage for static assets
- 🔒 Simple Auth - Basic authentication for demo purposes
- 🚀 Cloudflare Pages - Fast global deployments
- Node.js 18+
- Cloudflare account with Pages, D1, and R2 access
-
Clone this repository
-
Install dependencies:
npm install
-
Configure Cloudflare bindings in
wrangler.toml
:[[d1_databases]] binding = "DB" database_name = "your-d1-database" database_id = "your-d1-database-id" [[r2_buckets]] binding = "STORAGE" bucket_name = "your-r2-bucket"
-
Create D1 database tables:
wrangler d1 execute your-d1-database --file=schema.sql
-
Upload test image to R2:
npm run test-r2
-
Start development server:
npm run dev
This template includes R2 storage integration for serving large static assets.
Use the built-in upload utility:
# Upload a file to R2
npm run upload-to-r2 path/to/local/file path/in/r2 -- --remote
# Example
npm run upload-to-r2 src/assets/images/myimage.jpg images/myimage.jpg -- --remote
In your Astro components or HTML, reference assets using the /storage/
path prefix:
<img src="/storage/images/astroflare.jpg" alt="Astroflare" />
- Static assets are stored in Cloudflare R2 bucket
- An Astro API route (
src/pages/storage/[...path].ts
) handles requests to/storage/*
paths - The route fetches the requested file from R2 and serves it with proper headers
- Your Astro site references these assets using the
/storage/path/to/file
URL pattern
The template includes a simple CRUD application to demonstrate D1 database usage.
CREATE TABLE IF NOT EXISTS items (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
description TEXT,
created_at TEXT DEFAULT CURRENT_TIMESTAMP
);
In Astro pages and API routes, access D1 via:
const db = Astro.locals.runtime.env.DB;
const results = await db.prepare("SELECT * FROM items").all();
This template includes a simple authentication mechanism for demonstration purposes.
For production applications, consider using:
- Cloudflare Access
- JWT authentication
- Auth providers like Auth0, Clerk, etc.
npm run dev
- Start development servernpm run build
- Build for productionnpm run preview
- Preview production build locallynpm run upload-to-r2
- Upload files to R2npm run test-r2
- Upload test image to R2npm run r2-help
- Show R2 commands help
- Set up a new Cloudflare Pages project
- Connect your GitHub repository
- Configure build settings:
- Build command:
npm run build
- Build output directory:
dist
- Build command:
- Add required environment variables
- Set up D1 and R2 bindings in Cloudflare Pages dashboard
MIT