Use Process.env instead of Oxygen.env
Description
Fixes https://github.com/Shopify/hydrogen/issues/1226 by populating process.env in our platforms/worker.ts and hydrogen middleware.
Additional context
Before submitting the PR, please make sure you do the following:
- [ ] Read the Contributing Guidelines
- [ ] Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g.
fixes #123) - [ ] Update docs in this repository according to your change
- [ ] Run
yarn changeset addif this PR cause a version bump based on Keep a Changelog and adheres to Semantic Versioning
We detected some changes in packages/*/package.json or packages/*/src, and there are no updates in the .changeset.
If the changes are user-facing and should cause a version bump, run "yarn changeset add" to track your changes and include them in the next release CHANGELOG.
If you are making simple updates to examples or documentation, you do not need to add a changeset.
Unfortunately, it appears that Vite will find/replace any usage of process.env if the SSR target is a webworker aka our worker bundle: https://github.com/vitejs/vite/blob/c68db4d7ad2c1baee41f280b34ae89a85ba0373d/packages/vite/src/node/plugins/define.ts#L51
This means all process.env instances are wiped from server components 😞
Marking this as blocked until we can find a workaround.
@wizardlyhel @blittle — should we close this PR in favour of import.meta?
@frandiox import.meta is a vite thing right?
@wizardlyhel @blittle — should we close this PR in favour of
import.meta?
It is a vite thing. I'm wondering how it gets bundled / compiled, because runtimes (vercel, netlify, cloudflare, etc) aren't going to have it defined.
import.meta is a standard, but import.meta.env is added by Vite. It basically replaces any matches of this by its value at build time (i.e. inlines values in the bundle).
I remember suggesting adding a custom import.meta.secrets.XYZ for the private variables that would be compiled to process.env.XYZ or Oxygen.env.XYZ at build time. Not sure if feasible/desirable, though.
It's just really hard to know what to compile to, because each runtime does env variables slightly different. Which is why we are pushing for some sort of unification standard: https://github.com/wintercg/environment-metadata/issues/1
I guess the CLI could have an option to compile to different runtime targets? Vercel, cloudflare, oxygen, etc?
Now tracking in #1879