Cloudflare Workers SaaS Template

.github/workflows/deploy.yml

This is a SaaS template for Cloudflare Workers. It uses the OpenNext framework to build a SaaS application.

Have a look at the project plan to get an overview of the project.

Tip

This template is brought to you by ๐Ÿ‘‰ StartupStudio.dev ๐Ÿ‘ˆ - where we help businesses automate operations and boost productivity through custom AI implementations. Just like this open-source project demonstrates technical excellence, we deliver:

  • Process automation with LLM-powered workflows
  • AI strategy consulting for sustainable scaling
  • Custom SaaS development using cutting-edge stacks

Hundrets of developers already trust our codebase - Just Imagine what we could build for your business.

Supported Features:

  • ๐Ÿ” Authentication with Lucia Auth
    • ๐Ÿ“ง Email/Password Sign In
    • ๐Ÿ“ Email/Password Sign Up
    • ๐Ÿ”‘ WebAuthn/Passkey Authentication
    • ๐ŸŒ Google OAuth/SSO Integration
    • ๐Ÿ”„ Forgot Password Flow
    • ๐Ÿ”’ Change Password
    • โœ‰๏ธ Email Verification
    • ๐Ÿ—๏ธ Session Management with Cloudflare KV
    • ๐Ÿค– Turnstile Captcha Integration
    • โšก Rate Limiting for Auth Endpoints
    • ๐Ÿ›ก๏ธ Protected Routes and Layouts
    • ๐Ÿ“‹ Session Listing and Management
    • ๐Ÿ”’ Anti-Disposable Email Protection
  • ๐Ÿ’พ Database with Drizzle and Cloudflare D1
    • ๐Ÿ—๏ธ Type-safe Database Operations
    • ๐Ÿ”„ Automatic Migration Generation
    • ๐Ÿ’ป SQLite for Local Development
    • โšก Efficient Data Fetching
  • ๐Ÿ“จ Email Service with React Email and Resend or Brevo
    • ๐ŸŽจ Beautiful Email Templates
    • ๐Ÿ‘€ Email Preview Mode
    • ๐Ÿ”ง Local Email Development Server
    • ๐Ÿ“ฌ Transactional Emails
    • โœ‰๏ธ Email Verification Flow
  • ๐Ÿš€ Deployment with Github Actions
    • โš™๏ธ Automatic Deployments
    • ๐Ÿ” Environment Variables Management
    • ๐Ÿ“ฆ Database Migrations
    • ๐Ÿ”„ Comprehensive CI/CD Pipeline
  • ๐ŸŽจ Modern UI
    • ๐ŸŽจ Tailwind CSS
    • ๐Ÿงฉ Shadcn UI Components
    • ๐ŸŒ“ Dark/Light Mode
    • ๐Ÿ“ฑ Responsive Design
    • โšก Loading States and Animations
    • ๐Ÿ”” Toast Notifications
    • โš™๏ธ Settings Dashboard
    • ๐Ÿ  Landing Page
    • โœจ Beautiful Email Templates
    • ๐Ÿ‘ค Profile Settings Page
  • โœจ Validations with Zod and React Hook Form
    • ๐Ÿ›ก๏ธ Type-safe Form Validations
    • ๐Ÿ”’ Server-side Validations
    • ๐Ÿ” Client-side Validations
    • ๐Ÿงน Input Sanitization
  • ๐Ÿ‘จโ€๐Ÿ’ป Developer Experience
    • ๐Ÿ“˜ TypeScript Support
    • ๐Ÿ” ESLint Configuration
    • โœจ Prettier Configuration
    • ๐Ÿ” Type-safe Environment Variables
    • ๐Ÿ—๏ธ Cloudflare Types Generation
    • ๐Ÿค– AI-powered Development with Cursor
    • ๐Ÿ“š Comprehensive Documentation
    • ๐Ÿ“ Project Structure Best Practices
  • โšก Edge Computing
    • ๐ŸŒ Global Deployment with Cloudflare Workers
    • ๐Ÿš€ Zero Cold Starts
    • ๐Ÿ’จ Edge Caching
    • โš›๏ธ React Server Components
    • ๐Ÿ–ฅ๏ธ Server-side Rendering
    • ๐Ÿ’พ Edge Database with D1
    • ๐Ÿ—„๏ธ Session Storage with KV

Planned features (TODO):

  • Update Meta SEO tags ๐Ÿ”
  • Dynamic OpenGraph images ๐Ÿ“ธ
  • sitemap.xml ๐Ÿ“„
  • robots.txt ๐Ÿ“„
  • Multi-language support (i18n) ๐ŸŒ
  • Billing ๐Ÿ’ฐ
  • Admin dashboard ๐Ÿ‘‘
  • Notifications ๐Ÿ””
  • Payments ๐Ÿ’ณ
  • Webhooks ๐Ÿ”—
  • Track bundle size with https://www.npmjs.com/package/webpack-bundle-analyzer ๐Ÿ“Š

Running it locally

  1. pnpm install
  2. Copy .dev.vars.example to .dev.vars and fill in the values.
  3. Copy .env.example to .env and fill in the values.
  4. pnpm db:migrate:dev - Creates a local SQLite database and applies migrations
  5. pnpm dev
  6. Open http://localhost:3000

Changes to wrangler.json

After making a change to wrangler.json, you need to run pnpm cf-typegen to generate the new types.

Things to change and customize before deploying to production

  1. Go to src/constants.ts and update it with your project details
  2. Update the documentation in ./cursor-docs with your project details so that Cursor AI can give you better suggestions
  3. Update the footer in src/components/footer.tsx with your project details and links
  4. Optional: Update the color palette in src/app/globals.css
  5. Update the metadata in src/app/layout.tsx with your project details

Deploying to Cloudflare with Github Actions

  1. Create D1 and KV namespaces
  2. Set either RESEND_API_KEY or SENDGRID_API_KEY as a secret in your Cloudflare Worker depending on which email service you want to use.
  3. Create a Turnstile catcha in your Cloudflare account, and set the NEXT_PUBLIC_TURNSTILE_SITE_KEY as a Github Actions variable.
  4. Set TURNSTILE_SECRET_KEY as a secret in your Cloudflare Worker.
  5. Update the wrangler.json file with the new database and KV namespaces and env variables. If you change the D1 database name, you also need to update the db:migrate:dev script in the package.json file.
  6. Go to https://dash.cloudflare.com/profile/api-tokens and click on "Use template" next to "Edit Cloudflare Workers". On the next, page add the following permissions in addition to the ones from the template:
    • Account:AI Gateway:Edit
    • Account:Workers AI:Edit
    • Account:Queues:Edit
    • Account:Vectorize:Edit
    • Account:D1:Edit
    • Account:Cloudflare Images:Edit
    • Account:Workers KV Storage:Edit
    • Zone:Cache Purge:Purge
  7. Add the API token to the Github repository secrets as CLOUDFLARE_API_TOKEN
  8. Add the Cloudflare account id to the Github repository variables as CLOUDFLARE_ACCOUNT_ID
  9. Optional: If you want clear the CDN cache on deploy, add CLOUDFLARE_ZONE_ID to the Github repository variables for the zone id of your domain. This is the zone id of your domain, not the account id.
  10. Add the database name to the Github repository variables as DATABASE_ID. This should match the database name in the wrangler.json file.
  11. Push to the main branch

Email templates

If you want to preview and edit the email templates you can:

  1. pnpm email:dev
  2. Open http://localhost:3001
  3. Edit the email templates in the src/react-email folder
  4. For inspiration you can checkout https://react.email/templates

How to upgrade this template

Since this template is based on the OpenNext framework we need to make sure that we are following the changes they are making and update this template accordingly.

To see the changes clone https://github.com/cloudflare/workers-sdk and then do git diff f9fd9df...main -- packages/create-cloudflare/templates-experimental/next/ you will see the changes that we need to make to this template.