Skip to content

Template for deploying Workers for Platforms with subdomain routing and custom hostnames

License

Notifications You must be signed in to change notification settings

dinasaur404/platform-template

Repository files navigation

Workers for Platforms Template

Build your own website hosting platform using Cloudflare Workers for Platforms. Users can create and deploy websites through a simple web interface.

Deploy to Cloudflare

What You Get

  • Website Builder UI - Web interface for creating and deploying sites
  • Static Site Hosting - Drag & drop HTML/CSS/JS files
  • Custom Worker Code - Write dynamic sites with Workers
  • Subdomain Routing - Each site gets sitename.yourdomain.com
  • Custom Domains - Users can connect their own domains with SSL
  • Admin Dashboard - Manage all sites at /admin

Quick Start

Click the Deploy to Cloudflare button above. Everything is auto-configured!

Optional: Custom Domain

If you want to use your own domain instead of *.workers.dev:

Variable Description
CUSTOM_DOMAIN Your root domain (e.g., platform.com)

How It Works

┌─────────────────────────────────────────────────────────────┐
│  Your Platform (this template)                              │
├─────────────────────────────────────────────────────────────┤
│  platform.com              → Website Builder UI             │
│  platform.com/admin        → Admin Dashboard                │
├─────────────────────────────────────────────────────────────┤
│  User Sites (Workers for Platforms)                         │
│  ├── site1.platform.com    → User's deployed Worker         │
│  ├── site2.platform.com    → User's deployed Worker         │
│  └── custom.userdomain.com → Custom domain with SSL         │
├─────────────────────────────────────────────────────────────┤
│  my.platform.com           → Fallback origin for CNAMEs     │
└─────────────────────────────────────────────────────────────┘

Manual Deployment

# Clone
git clone https://github.com/dinasaur404/platform-template.git
cd platform-template

# Install
npm install

# Run interactive setup (creates tokens, configures everything)
npm run setup

# Deploy
npm run deploy

The setup script will:

  • Validate your Cloudflare credentials
  • Create the dispatch namespace for Workers for Platforms
  • Auto-create API tokens with correct permissions (if needed)
  • Generate .dev.vars with all required configuration
  • Update wrangler.toml with your settings

Custom Domain Setup

To use your own domain instead of *.workers.dev:

1. Update wrangler.toml

[vars]
CUSTOM_DOMAIN = "platform.com"
CLOUDFLARE_ZONE_ID = "your-zone-id-here"
FALLBACK_ORIGIN = "my.platform.com"

routes = [
  { pattern = "*/*", zone_name = "platform.com" }
]

workers_dev = false

2. Add DNS Records

In your Cloudflare DNS settings for platform.com:

Type Name Content Result Proxy
A * 192.0.2.1 *.platform.com Proxied
A my 192.0.2.1 my.platform.com Proxied

Note: The root domain (platform.com) is automatically configured when you add a custom domain to your Worker in the Cloudflare dashboard. The 192.0.2.1 is a dummy IP - Cloudflare's proxy handles the actual routing.

About the Fallback Origin (my.platform.com):

This is the hostname your customers will CNAME their custom domains to. When a user wants to connect their own domain (e.g., shop.example.com), they add:

CNAME  shop.example.com  →  my.platform.com

Cloudflare uses this fallback origin to route traffic for custom hostnames.

3. Redeploy

npm run deploy

Security

The admin page (/admin) shows all projects. Protect it with Cloudflare Access:

  1. Go to Zero TrustAccessApplications
  2. Add application for platform.com/admin*
  3. Configure authentication policy

Troubleshooting

Problem Solution
"Dispatch namespace not found" Enable Workers for Platforms: dash.cloudflare.com/?to=/:account/workers-for-platforms
"Custom domain not working" Check Zone ID and DNS records are correct
"Custom hostnames require additional setup" Provide CLOUDFLARE_API_TOKEN with SSL permissions during deploy, or add it post-deploy as a secret
"404 on deployed sites" Ensure uploaded files include index.html at the root
Database errors Visit /admin to check status, or /init to reset

View logs:

npx wrangler tail

Prerequisites


Learn More

License

Apache-2.0

About

Template for deploying Workers for Platforms with subdomain routing and custom hostnames

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published