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

Support environment variables in Cloudflare and Netlify Edge functions #5301

Merged
merged 24 commits into from
Nov 8, 2022

Conversation

bluwy
Copy link
Member

@bluwy bluwy commented Nov 4, 2022

Changes

Fix #5105
Fix #5234

  • Refactor vite-plugin-env. Does lesser work and should be more performant.
  • Handle netlify edge functions environment variables similar to Deno integration.
  • Handle cloudflare environment variables by assigning request env to global process.env.

Note: From the linked issue, we discussed of the possibility of build.envKey, but I later found it's not needed as we can assign platform env vars to a shimmed process.env and it should be enough. Currently the Deno integration does this so there's a precedence to follow. Also one less configuration 😬

Note 2: Thanks @AirBorne04 for the discussion and ideas that reached here.

Testing

Existing env var test should pass. Added new tests for Cloudflare and Netlify Edge.

Cloudflare and Netlify Edge tests are previously skipped and I can't seem to get it passing for CI. However it was working when testing locally.

Docs

Updated docs in Cloudflare integration README.

@changeset-bot
Copy link

changeset-bot bot commented Nov 4, 2022

🦋 Changeset detected

Latest commit: b30d076

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added pkg: astro Related to the core `astro` package (scope) pkg: integration Related to any renderer integration (scope) labels Nov 4, 2022
@AirBorne04
Copy link
Contributor

hey @bluwy , i love it!

Code

I think for the Cloudflare adapter there are multiple process.env shims in place now, which should probably be cleaned up?

Is this still needed?

(globalThis as any).process = {
argv: [],
env: {},
};

This is run at build time so it could be used to map all build env vars into the process env shim

const SHIM = `globalThis.process = {
argv: [],
env: {},
};`;


If it is changed to env: JSON.stringify(${process.env}) making the guard/proxy obsolete (this is not 100% the same as the request env vars though which can only be added later)

Build Runtime
Env Vars (Build system + User) Env vars (User)
- Bindings to KV, Durable, R2, D1

Having said that, i think it is fine either way (guard/proxy or build vars) probably the proxy is a bit more straight forward to understand for the user and creates less headaches for maintance.

Docs

For the docs i think the Cloudflare adapter docs for Environment vars need to be adjusted.

Copy link
Member

@natemoo-re natemoo-re left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! LGTM.

Comment on lines +40 to +42
privateEnv.SITE = astroConfig.site ? `'${astroConfig.site}'` : 'undefined';
privateEnv.SSR = JSON.stringify(true);
privateEnv.BASE_URL = astroConfig.base ? `'${astroConfig.base}'` : 'undefined';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Greatly appreciate these refactors! I knew this plugin was messy and never had time to come back and clean it up. 🎉

{
get: (target, prop) => {
console.warn(
// NOTE: \0 prevents Vite replacement
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WHAT! I totally missed this. That's very smart.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's slightly leaning towards a hack that should be fixed in Vite 4 (hopefully) 😬

@bluwy
Copy link
Member Author

bluwy commented Nov 7, 2022

I think for the Cloudflare adapter there are multiple process.env shims in place now, which should probably be cleaned up?

Nice find! Yeah we should clean them up.

If it is changed to env: JSON.stringify(${process.env}) making the guard/proxy obsolete (this is not 100% the same as the request env vars though which can only be added later)

Interesting fix, I wondered why you did that before. I'm not really sure about this yet as it brings up the bundle size by default, but we can revisit this idea if the Proxy is confusing for users.

For the docs i think the Cloudflare adapter docs for Environment vars need to be adjusted.

Yes 👍 This should be updated too. Thanks for the review!

Copy link
Member

@sarah11918 sarah11918 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, @bluwy!

I've left a couple of comments for your consideration regarding the README. You can consider this reviewed by Team Docs now! 🥳

packages/integrations/cloudflare/README.md Outdated Show resolved Hide resolved
packages/integrations/cloudflare/README.md Outdated Show resolved Hide resolved
@sarah11918
Copy link
Member

Final LGTM approval from me! 🚀

@bluwy bluwy merged commit a79a37c into main Nov 8, 2022
@bluwy bluwy deleted the env-key branch November 8, 2022 13:54
@astrobot-houston astrobot-houston mentioned this pull request Nov 8, 2022
matthewp pushed a commit that referenced this pull request Nov 8, 2022
#5301)

Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: astro Related to the core `astro` package (scope) pkg: integration Related to any renderer integration (scope)
Projects
None yet
5 participants