Skip to content
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

Environment variables when using the cloudflare-worker adapter #2606

Closed
kevmodrome opened this issue Oct 14, 2021 · 2 comments
Closed

Environment variables when using the cloudflare-worker adapter #2606

kevmodrome opened this issue Oct 14, 2021 · 2 comments

Comments

@kevmodrome
Copy link

Describe the problem

On the latest Svelte Summit website I had to use environment variables and ran into some pretty rough DX. Cloudflare workers require environment variables to be supplied either via a wrangler.toml file or manually added using the wrangler CLI or the web interface. These environment variables are then available as top level variables in the script.

This presents a bit of a problem when developing applications as these variables are not available. To get around it you need to use some other way of storing environment variables. I ended up using a package called env-cmd to run dev/build and preview with. However, since you grab the variables from different places (process.env.X vs X) you end up having to write conditionals in your code to handle this.

Here's one I wrote for an email API key:

api_key: process.env['EMAIL_API_KEY'] || EMAIL_API_KEY

I understand I can probably write some wrapping function around this, but I still need to define the variables in two places.

Describe the proposed solution

Ideally you would only need to keep your variables in one place and having a way to reach the variables in a consistent way would be great.

Alternatives considered

Using another adapter.

Importance

would make my life easier

Additional Information

Maybe there needs to be a bigger discussion around environment variables in general here. I'm really only familiar with CF adapter but I imagine there are probably other deploy targets that also handle variables differently.

@antony
Copy link
Member

antony commented Oct 15, 2021

I actually have a similar issue with Vercel. I've not tried SvelteKit for comparison, but to deploy our Sapper apps I have multiple deployment configurations. It's fairly annoying. I wonder if we could do something clever with the toml/vercel config during deploy, like pulling in the required variables from .env and putting them into these configs (we already write some custom config for begin, for example).

Or maybe it's not for SvelteKit to solve. It's hard to tell!

@Rich-Harris
Copy link
Member

i'm going to close this in favour of #4296

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants