Jimmy Cai Avatar
Home

Migrate Astro Site From Cloudflare Pages to Workers

A brief write-up on deploying this personal website with Cloudflare Workers

Until yesterday, this site was hosted on Cloudflare Pages and connected to my GitHub repository. This was the recommended way to deploy an Astro site using Cloudflare. Page Functions allows Astro SSR to work, but not all APIs are supported. For instance, Cloudflare's Rate Limit binding is only available for Worker.

Last week, Cloudflare announced an enhancement to the Worker architecture. It was not possible to host an Astro site directly on a Worker due to the lack of support for hosting static assets. With this new update, almost all features of Pages are available on Workers, so there's no reason not to migrate to Worker (see compatibility matrix).

Note: Workers does not support any domain whose nameservers are not managed by Cloudflare.


First, we need to create a Wrangler configuration file: wrangler.jsonc in the root directory. wrangler.json or wrangler.toml can also work.

jsonc
{
    "$schema": "node_modules/wrangler/config-schema.json",
    "name": "cai-im",
    "main": "./dist/_worker.js/index.js",
    "compatibility_date": "2025-04-16",
    "compatibility_flags": ["nodejs_compat"],
    "assets": {
        "binding": "ASSETS",
        "directory": "./dist",
        "not_found_handling": "404-page",
    },
    "observability": {
        "enabled": true,
    }
}

Then, add .assetsignore to the public folder with the following content:

_worker.js
_routes.json

I decided to use Cloudflare's own CI system (still in beta). For some reason, it can't detect that I'm using pnpm and throws the following error:

22:40:39.532	Initializing build environment...
22:40:45.814	Success: Finished initializing build environment
22:40:45.979	Cloning repository...
22:40:47.314	Detected the following tools from environment: pnpm@9.10.0, nodejs@22.9.0
22:40:47.315	Installing project dependencies: pnpm install --frozen-lockfile
22:40:47.627	! The local project doesn't define a 'packageManager' field. Corepack will now add one referencing pnpm@9.10.0+sha512.73a29afa36a0d092ece5271de5177ecbf8318d454ecd701343131b8ebc0c1a91c487da46ab77c8e596d6acf1461e3594ced4becedf8921b074fbd8653ed7051c.
22:40:47.628	! For more details about this field, consult the documentation at https://nodejs.org/api/packages.html#packagemanager
22:40:47.628
22:40:48.122	 ERROR  packages field missing or empty
22:40:48.123	For help, run: pnpm help install
22:40:48.138	Failed: error occurred while installing tools or dependencies

To fix this, run corepack use pnpm, which will define the packageManager field in package.json:

json
{
    "packageManager": "pnpm@10.8.1+sha512.c50088ba998c67b8ca8c99df8a5e02fd2ae2e2b29aaf238feaa9e124248d3f48f9fb6db2424949ff901cffbb5e0f0cc1ad6aedb602cd29450751d11c35023677"
}