How do we access production env vars and secrets with the vite-plugin?

I'm unable to read any env and the vite-plugin sidesteps writing the fetch where we can access. The docs are pretty minimal and never shows any example of consuming the env vars and secrets... https://developers.cloudflare.com/workers/vite-plugin/reference/secrets/
Cloudflare Docs
Secrets
Using secrets with the Vite plugin
3 Replies
Viktor
Viktor7mo ago
Did you check the env object (second parameter in your fetch() callback in your worker)? At least when building a regular worker env vars and secrets are in there.
export default {
async fetch(_request, env) {
//const myEnv = env.CLOUDFLARE_SECRET;
return new Response(JSON.stringify({ ... env }))
},
},
export default {
async fetch(_request, env) {
//const myEnv = env.CLOUDFLARE_SECRET;
return new Response(JSON.stringify({ ... env }))
},
},
Viktor
Viktor7mo ago
Information on how to add a worker script in case you don't have one -> https://developers.cloudflare.com/workers/vite-plugin/tutorial/#add-your-api-worker
Cloudflare Docs
Tutorial - React SPA with an API
Create a React SPA with an API Worker using the Vite plugin
verdverm.com
verdverm.comOP7mo ago
I just removed the vite-plugin and things work as expected / documented

Did you find this page helpful?