Repository navigation
Using Cloudflare Agents SDK (Durable Objects) alongside Nitro - current workaround & suggested improvement #4224
lukalafayedemicheaux
started this conversation in
General
Replies: 1 comment
|
Perhaps we could introduce a new Nitro hook for this. Perhaps like |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Hi team 👋
We're building on Nuxt + Cloudflare Workers and wanted to integrate the Cloudflare Agents SDK (
agentspackage) alongside our Nitro server.Nitro's
exports.cloudflare.tssupport already covers the Durable Object export requirement nicely. Since Cloudflare requires Durable Object classes referenced in the Worker config to be exported by name from the Worker entrypoint, we can expose our Agent class like this:This avoids the usual Wrangler error:
The remaining issue is request routing. Cloudflare Agents expects requests to go through
routeAgentRequest(request, env)before falling back to the app handler:As far as I can tell, Nitro's generated Cloudflare Worker entrypoint owns the default
fetchhandler, and there doesn't seem to be a first-class way to wrap it or prepend this Agents routing logic.Our current workaround is a committed
worker.tsthat Wrangler bundles directly:This works well, but only if we set
deployConfig: false. Otherwise Nitro generates.wrangler/deploy/config.json, which redirects Wrangler to.output/server/wrangler.json, wheremainpoints toindex.mjsand bypasses our customworker.ts.Would Nitro consider adding a way to compose custom Cloudflare
fetchlogic before the generated Nitro handler? For example, a request hook that can return aResponseor fall through to Nitro.That would make Cloudflare Agents integration possible without replacing the Worker entrypoint or disabling Nitro's generated deploy config.
Thanks for the great work on Cloudflare Workers support and
exports.cloudflare.ts🙌All reactions