cloudflare-docs
cloudflare-docs copied to clipboard
[Workers] Callout AsyncLocalStorage as a Workers alternative for getPlatformProxy
Which Cloudflare product(s) does this pertain to?
Workers
Subject Matter
ALS as a way to get req/env/ctx anywhere in Workers
Content Location
getPlatformProxy allows users to access their Workers bindings and mock information like req.cf and ctx.waitUntil(), allowing them to write code for Workers without using Wrangler's development emulation (i.e next dev when using next-on-pages)
The availability of these variables, without having to pipe them from a handler, is a common request on Workers and can be done with AsyncLocalStorage.
The current documentation for getPlatformProxy states...
getPlatformProxy cannot be run inside the Workers runtime.
It would be helpful if there was a guide on using AsyncLocalStorage to do the same, and link out to it - I suspect people will come across this page when trying to figure out if that use-case is possible.
The top 5 results for a Google search of "cloudflare workers asynclocalstorage for env" are:
- process.env docs (doesn't work for bindings)
- a community post (talks about using AsyncLocalStorage to implement this)
- environment variables docs (no mention of AsyncLocalStorage)
- AsyncLocalStorage docs (no mention of using it within imports, libraries or using it to store per-request state)
- a next-on-pages issue (talks about using AsyncLocalStorage to implement this).
Additional information
No response