router: server function calls not working on Cloudflare
Which project does this relate to?
Start
Describe the bug
Server functions don’t work on Cloudflare deployments (via cloudflare-pages preset), wrangler dev, and apparently also on Stackblitz.
[wrangler:inf] GET / 200 OK (56ms)
✘ [ERROR] Error: Context is not available
at Object.use
(file:///home/nize/dev/web/tss-cloudflare-server-fn/.wrangler/tmp/pages-liyiqW/chunks/runtime.mjs:1:103891)
at V
(file:///home/nize/dev/web/tss-cloudflare-server-fn/.wrangler/tmp/pages-liyiqW/chunks/runtime.mjs:455:96187)
at _
(file:///home/nize/dev/web/tss-cloudflare-server-fn/.wrangler/tmp/pages-liyiqW/chunks/runtime.mjs:455:108302)
at null.<anonymous>
(file:///home/nize/dev/web/tss-cloudflare-server-fn/.wrangler/tmp/pages-liyiqW/chunks/runtime.mjs:455:107762)
at Object.assign._ [as url]
(file:///home/nize/dev/web/tss-cloudflare-server-fn/.wrangler/tmp/pages-liyiqW/chunks/runtime.mjs:455:108277)
at Object.assign.url
(file:///home/nize/dev/web/tss-cloudflare-server-fn/.wrangler/tmp/pages-liyiqW/chunks/runtime.mjs:455:106760)
at Object.beforeLoad
(file:///home/nize/dev/web/tss-cloudflare-server-fn/.wrangler/tmp/pages-liyiqW/chunks/runtime.mjs:455:108503)
at null.<anonymous>
(file:///home/nize/dev/web/tss-cloudflare-server-fn/.wrangler/tmp/pages-liyiqW/chunks/runtime.mjs:1:148928)
at null.<anonymous>
(file:///home/nize/dev/web/tss-cloudflare-server-fn/.wrangler/tmp/pages-liyiqW/chunks/runtime.mjs:1:152252)
at new Promise (<anonymous>) {
routerCode: 'BEFORE_LOAD'
}
Your Example Website or App
https://github.com/dotnize/tss-cloudflare-server-fn
Steps to Reproduce the Bug or Issue
or stackblitz: https://stackblitz.com/~/github.com/dotnize/tss-cloudflare-server-fn
pnpm installpnpm buildpnpm previewvia wrangler
The app doesn’t work at all due to a server fn call in __root.tsx’s beforeLoad. Server fn calls from other routes/loaders (like in index.tsx) also don’t work.
Expected behavior
Server functions should work as expected, as it already works on local vinxi dev, node-server builds, or Vercel deployments
repo deployment on vercel, working as expected: https://tss-cloudflare-server-fn.vercel.app/
Screenshots or Videos
No response
Platform
- OS: Linux
- Browser: Chrome
- Version: 130
Additional context
No response
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 5
- Comments: 15 (11 by maintainers)
I got it working, this is my config
Hi, @SeanCassiere.
I don’t think this will be fixed by Nitro in the near future since no one has had looked at the issue for a year now.
For now, we have to set
compatibility_flags = [ "nodejs_compat" ]androllupConfig: { external: ["node:async_hooks"], }as the configuration I posted previously.Perhaps we should update the documentation to tell people to enable
compatibility_flags = [ "nodejs_compat" ]and update thecloudflare-pagesdefault to includerollupConfig.external.What are your thoughts?