Setting up Cloudflare Workers hosting
Deploy config for hosting a Bun + TanStack Start app on Cloudflare Workers.
Prerequisites
- Cloudflare account
- Bun project using TanStack Start + Vite
Steps
Install
bun add -D @cloudflare/vite-plugin wranglerConfigure the Cloudflare plugin in
vite.config.tsimport { defineConfig } from "vite"; import { tanstackStart } from "@tanstack/react-start/plugin/vite"; import { cloudflare } from "@cloudflare/vite-plugin"; export default defineConfig({ plugins: [cloudflare({ viteEnvironment: { name: "ssr" } }), tanstackStart()], });Add
wrangler.jsoncat the project root{ "$schema": "node_modules/wrangler/config-schema.json", "name": "<project-name>", "compatibility_date": "<today, YYYY-MM-DD>", "compatibility_flags": ["nodejs_compat"], "main": "@tanstack/react-start/server-entry", "cache": { "enabled": true }, "observability": { "enabled": true, }, }nameshould match thenamekey inpackage.jsoncompatibility_dateshould be today's date
Add scripts to
package.json- Prefix the
buildscript withwrangler typesso generated worker types exist before the app build runs
{ "scripts": { "build": "wrangler types && bun --bun vite build && ..." } }- Prefix the
Ignore generated Cloudflare files
.wrangler worker-configuration.d.tsDeploy
- Commit and push the changes in Git
- Create the project via the Cloudflare dashboard, importing the git repo
Verification
- Run
bun run buildand confirm it completes without errors
Gotchas
wrangler typesmust run before the Vite build, or the build picks up stale/missing generated worker types