hydrogen icon indicating copy to clipboard operation
hydrogen copied to clipboard

Use Process.env instead of Oxygen.env

Open cartogram opened this issue 3 years ago • 8 comments

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 add if this PR cause a version bump based on Keep a Changelog and adheres to Semantic Versioning

cartogram avatar May 19 '22 11:05 cartogram

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.

github-actions[bot] avatar May 25 '22 13:05 github-actions[bot]

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.

jplhomer avatar May 26 '22 13:05 jplhomer

@wizardlyhel @blittle — should we close this PR in favour of import.meta?

benjaminsehl avatar Jul 13 '22 14:07 benjaminsehl

@frandiox import.meta is a vite thing right?

wizardlyhel avatar Jul 13 '22 15:07 wizardlyhel

@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.

blittle avatar Jul 13 '22 17:07 blittle

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.

frandiox avatar Jul 14 '22 03:07 frandiox

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?

blittle avatar Jul 14 '22 14:07 blittle

Now tracking in #1879

benjaminsehl avatar Jul 22 '22 02:07 benjaminsehl