A production-ready Next.js 16 starter template optimized for Cloudflare Pages deployment with better-auth-cloudflare integration.
- โก Next.js 16 with React 19 and Turbopack
- ๐ Authentication via better-auth-cloudflare
- Anonymous login support
- Geolocation tracking
- OpenAPI documentation generation
- ๐๏ธ Database - Drizzle ORM with Cloudflare D1
- ๐ฆ File Storage - Cloudflare R2 bucket integration
- ๐ Key-Value Store - Cloudflare KV namespace support
- ๐จ UI Components - Tailwind CSS 4 + shadcn/ui components
- ๐ง Code Quality - Biome for formatting and linting
- ๐ Cloudflare Workers - Native Cloudflare platform integration
- Framework: Next.js 16 (App Router)
- Runtime: React 19
- Styling: Tailwind CSS 4, shadcn/ui
- Authentication: better-auth + better-auth-cloudflare
- Database: Drizzle ORM + Cloudflare D1
- Storage: Cloudflare R2 (object storage)
- Cache: Cloudflare KV (key-value store)
- Deployment: Cloudflare Pages via @opennextjs/cloudflare
- Code Quality: Biome
- Node.js 18+ or Bun
- Cloudflare account
- Wrangler CLI installed globally
git clone <your-repo-url>
cd nextjs-latest-starter-kit-template
npm install
# or
bun installYou'll need to create the following Cloudflare resources:
wrangler d1 create DATABASECopy the database ID and update your wrangler.jsonc:
wrangler kv namespace create KVAdd the KV namespace to wrangler.jsonc:
{
"kv_namespaces": [
{
"binding": "KV",
"id": "your-kv-id"
}
]
}wrangler r2 bucket create r2-bucketAdd the R2 bucket to wrangler.jsonc:
{
"r2_buckets": [
{
"binding": "R2_BUCKET",
"bucket_name": "r2-bucket"
}
]
}Generate the authentication schema:
npm run auth:updateGenerate Drizzle migrations:
npm run db:generateApply migrations locally:
npm run db:migrate:devnpm run dev
# or
bun devOpen http://localhost:3000 to see your application.
The development server uses Turbopack for fast refresh. You can start editing pages in src/app/ and see changes instantly.
npm run dev- Start development server with Turbopacknpm run build- Build Next.js applicationnpm run build:cf- Build for Cloudflare Pages deployment
npm run auth:generate- Generate Better Auth database schemanpm run auth:format- Format generated schema with Prettiernpm run auth:update- Generate and format schema (combines above two)
npm run db:generate- Generate Drizzle migrationsnpm run db:migrate:dev- Apply migrations to local D1 databasenpm run db:migrate:prod- Apply migrations to production D1 databasenpm run db:studio:dev- Open Drizzle Studio for local databasenpm run db:studio:prod- Open Drizzle Studio for production database
npm run format- Format code with Biomenpm run lint- Lint code with Biome
npm run preview- Build and preview locallynpm run deploy- Build and deploy to Cloudflare Pagesnpm run clean-deploy- Clean install and deploy
npm run cf-typegen- Generate TypeScript types from Wrangler confignpm run clean- Clean build artifacts and dependencies
โโโ src/
โ โโโ app/ # Next.js app router pages
โ โ โโโ api/ # API routes (Better Auth)
โ โ โโโ dashboard/ # Protected dashboard page
โ โ โโโ page.tsx # Home page with anonymous login
โ โโโ auth/ # Authentication configuration
โ โ โโโ index.ts # Better Auth setup
โ โ โโโ authClient.ts # Client-side auth utilities
โ โโโ components/ # React components
โ โ โโโ ui/ # shadcn/ui components
โ โ โโโ FileUploadDemo.tsx
โ โโโ db/ # Database configuration
โ โ โโโ schema.ts # Drizzle schema
โ โ โโโ auth.schema.ts # Generated auth schema
โ โโโ lib/ # Utility functions
โ โโโ middleware.ts # Next.js middleware for auth
โโโ public/ # Static assets
โโโ wrangler.jsonc # Cloudflare Workers configuration
โโโ open-next.config.ts # OpenNext Cloudflare configuration
โโโ next.config.ts # Next.js configuration
โโโ biome.json # Biome configuration
This template uses better-auth-cloudflare for authentication:
- Anonymous Login: Users can log in without credentials
- Session Management: Sessions stored in D1 database
- Geolocation: Automatic IP geolocation via Cloudflare
- Protected Routes: Middleware redirects unauthenticated users
To add OAuth providers (GitHub, Google, etc.), update src/auth/index.ts:
socialProviders: {
github: {
clientId: process.env.GITHUB_CLIENT_ID || "",
clientSecret: process.env.GITHUB_CLIENT_SECRET || "",
},
}Add the secrets to Cloudflare:
wrangler secret put GITHUB_CLIENT_ID
wrangler secret put GITHUB_CLIENT_SECRETThe template includes file upload functionality using Cloudflare R2:
- Maximum file size: 2MB (configurable in
src/auth/index.ts) - Allowed types:
.jpg,.jpeg,.png,.gif(configurable) - Additional metadata: category, description, public flag
Files are uploaded via Better Auth's R2 plugin and can be managed through the dashboard.
- Apply migrations to production:
npm run db:migrate:prod- Deploy:
npm run deployOr use the Cloudflare dashboard:
- Connect your GitHub repository
- Set build command:
npm run build:cf - Set build output directory:
.open-next - Add environment variables/bindings from your
wrangler.jsonc
Configure in Cloudflare Pages settings or wrangler.jsonc:
DATABASE- D1 database binding (required)KV- KV namespace binding (required)R2_BUCKET- R2 bucket binding (required)
- Next.js Documentation - Learn about Next.js features
- Next.js App Router - App Router documentation
- Cloudflare Pages - Pages documentation
- Cloudflare D1 - D1 database documentation
- Cloudflare R2 - R2 storage documentation
- Cloudflare Workers - Workers documentation
- Better Auth - Better Auth documentation
- better-auth-cloudflare - Cloudflare adapter
- Drizzle Documentation - Drizzle ORM documentation
Contributions are welcome! Please feel free to submit a Pull Request.
This project is open source and available under the MIT License.
{ "d1_databases": [ { "binding": "DATABASE", "database_name": "DATABASE", "database_id": "your-database-id" } ] }