Best practices for customizing Worker entry point in Astro + Cloudflare Workers static assets setup

I'm working on an Astro project with Cloudflare Workers (static assets) integration, and I need to access Worker bindings (D1 database) in my API endpoints. Currently, my wrangler.json has "main": "./dist/_worker.js/index.js" which seems to be set up by Astro.
I want to add custom Worker logic to handle API routes and access D1, but I'm unsure about the best/safest way to do this without breaking Astro's existing Worker setup. I see a few possible approaches:

  • Modify the main entry point in wrangler.json (but this seems risky)
  • Use Astro's middleware system somehow
  • Something else?
My current wrangler.json:

{
  "main": "./dist/_worker.js/index.js",
  "compatibility_date": "2025-02-04",
  "assets": {
    "binding": "ASSETS",
    "directory": "./dist"
  },
  "d1_databases": [
    {
      "binding": "DB",
      "database_name": "users-dev",
      "database_id": "..."
    }
  ]
}

What's the recommended way to add custom Worker logic while maintaining Astro's existing Worker setup? Any help would be appreciated! 🙏
Was this page helpful?