docs(cloudflare): fix env bindings access pattern for Nitro v3 (breaking change)#4064
docs(cloudflare): fix env bindings access pattern for Nitro v3 (breaking change)#4064ruan-cat wants to merge 1 commit intonitrojs:mainfrom
Conversation
Update cloudflare.md to show the correct v3 pattern (event.req.runtime.cloudflare.env) and add a breaking change warning. Add migration section to 99.migration.md.
|
@ruan-cat is attempting to deploy a commit to the Nitro Team on Vercel. A member of the Team first needs to authorize it. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review infoConfiguration used: Organization UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughDocumentation updates guide migration from Changes
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Tip Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs). Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
@pi0 Can you check and review it? I was discovered within the nitro v3 version, with the aid of AI event. The I hope to merge into the main branch soon. This change is documentation only and adds more detail to the already patchy nitro v3 documentation. |
Summary
This PR fixes outdated documentation for accessing Cloudflare Worker environment variables and bindings in Nitro v3. The existing docs still show the Nitro v2 pattern (
event.context.cloudflare.env), which does not work in production with Nitro v3. This has been a source of confusion for developers migrating from v2 to v3.Problem
The current documentation in
docs/2.deploy/20.providers/cloudflare.mdshows:This pattern (
event.context.cloudflare.env) was correct in Nitro v2 but fails silently in Nitro v3 production deployments —event.context.cloudflareisundefinedat runtime, so accessing.envon it throws or returns nothing.Additionally, the migration guide (
docs/1.docs/99.migration.md) has no mention of this breaking change, leaving developers without guidance when upgrading.Root Cause
In Nitro v3, the underlying server layer switched from H3/unenv to srvx. As a result, the Cloudflare preset's production runtime entry now attaches the Cloudflare context to the request's runtime object rather than the event context.
The relevant code is in
src/presets/cloudflare/runtime/_module-handler.ts:The
augmentReq()function is called in the productionfetchhandler:This means in Nitro v3, the correct path to access Cloudflare bindings is:
Why does it seem to work in local dev?
The dev plugin (
src/presets/cloudflare/runtime/plugin.dev.ts) still populatesevent.req.context.cloudflarefor local development via the Wrangler proxy. This creates a confusing discrepancy: the old pattern works locally but fails in production.Evidence
Debug Endpoint
A diagnostic API endpoint was written to probe all possible paths for Cloudflare env access in Nitro v3:
Source:
server/api/debug-env.get.tsKey probe from that file:
Live Reproducible Demo
The endpoint is deployed to Cloudflare Workers using Nitro v3:
URL: https://01s-11.ruan-cat.com/api/debug-env
This live endpoint confirms that:
req.runtime.cloudflare.envcontains the environment variables and bindings ✓event.context.cloudflareisundefinedin production ✗Changes
This PR modifies documentation only — no source code changes.
docs/2.deploy/20.providers/cloudflare.mdevent.req.runtime.cloudflare.env)::warningcallout explaining the breaking change from v2docs/1.docs/99.migration.mddiffmigration example showing the old v2 pattern vs. the new v3 patternTesting
The fix can be verified by deploying a Nitro v3 app to Cloudflare Workers and confirming that
event.req.runtime.cloudflare.envreturns the expected bindings. The live demo at https://01s-11.ruan-cat.com/api/debug-env serves as a reproducible test case.