-
Notifications
You must be signed in to change notification settings - Fork 511
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(cloudflare): expose event.context.cf
#927
Conversation
Codecov Report
@@ Coverage Diff @@
## main #927 +/- ##
=======================================
Coverage 67.77% 67.77%
=======================================
Files 59 59
Lines 5992 5992
Branches 679 679
=======================================
Hits 4061 4061
Misses 1922 1922
Partials 9 9 Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
Is there a way to use this to get bindings from .env? I saw a screenshot from Atinux about storing state in cloudflare KV workers, so it seems that conceptually that's possible? But the above exposed request.cf does not include the .env (nor the secrets stored in cloudflare) nor the service, KV, queue bindings. Is there a way that I'm just missing? |
You can use storage layer to store data in KV, Filesystem, memory and anywhere else. |
@altryne you're right you cannot access KVs or other Cloudflare services from what it's exposed, we need to pass the env and ctx to the user as I am doing here for the cloudflare-pages adaptor: #997 After I (hopefully) get the cloudflare-pages adaptor PR merged I can look into adding access to the env and ctx to the cloudflare worker adaptor as well, although in order to do so we'd fist need to convert the adaptor to the ESM syntax as now it uses the old service worker one in which the various variables are just put in the global scope (and some like DO aren't even available). |
I actually found a way! it's possible via self. https://discord.com/channels/473401852243869706/1002541842409193563/1077016381662433372 |
ah wow, that's interesting! ๐ฎ are you deploying your Nuxt app via the worker adaptor? (not the pages one) |
Yeah not via the pages one. |
ah ok I see, that makes sense, thanks ๐๐ |
@pi0 I'm so sorry I missed your comment on this, however, this isn't answering the question of accessing cloudflare bindings on .env. The storage layer adds storage, however, on the .env, cloudflare is storing the following things that are not storage:
All those things are needed when developing a server side application on cloudflare, all their examples aways require to take this information from the request context and I think it would be great to have them, and not have to do hacks like this one: I think for many worker devs, this would be more important than the context of the IP and country (which is also important) |
๐ Linked issue
resolves #391
closes #677
โ Type of change
๐ Description
This PR exposes cloudflare request context as
event.context.cf
.Implementation is based on new unenv support. Any other platform based on localFetch, can expose the same context by passing
{ context: {} }
to the request init.A new generic handler is added to nitro. It will be used later for other purposes as well.
Note: For nested
$fetch
, we should explicitly pass thecf
to be proxied:๐ Checklist