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
[BUG] Environment variables declared in the Oxygen admin are not available via import.meta.env #2033
Comments
Currently, the variables added in Oxygen admin are only available using
|
Thank you @frandiox, your solution worked for me! |
I am completely unable to access Oxygen environment variables (in the client) on any environment, running locally or deployed/published to Oxygen. Environment variables are created in the Oxygen admin (Oxyven.env locally), and flagged for Preview and Production. declare const Oxygen: any;
if (typeof Oxygen !== 'undefined') {
console.log('FOUND OXYGEN', Oxygen);
} else {
console.log('OXYGEN NOT FOUND');
}
const oxygenConfig = () => ({
PUBLIC_DEBUG: 'PUBLIC_DEBUG',
});
...
const debug = oxygenConfig()["PUBLIC_DEBUG"]; Oxygen.env contents for local run
Results in: |
@9001-Sols Separately, environment variables can be injected into the client bundle of code at build time. Use Make sure whatever you do, that no secret variables are passed to client components. |
While not directly related, I figured I'd post some findings for other people having issues with .env's in Hydrogen. It seems that console.log('|mport.meta.env.PUBLIC_DEBUG', import.meta.env.PUBLIC_DEBUG); // Works as expected
console.log('|mport.meta.env[PUBLIC_DEBUG]', import.meta.env['PUBLIC_DEBUG']); // Works as expected
// console.log('|mport.meta?.env.PUBLIC_DEBUG', import.meta?.env.PUBLIC_DEBUG); // <-- Breaks the build if uncommented
console.log('|mport.meta?.env?.PUBLIC_DEBUG', import.meta?.env?.PUBLIC_DEBUG); // undefined
console.log("|mport.meta?.env?.['PUBLIC_DEBUG']", import.meta?.env?.['PUBLIC_DEBUG']); // undefined Notice the usage of |
I think we are going to update the docs to mainly focus on runtime variables through |
Thanks so much, Frandiox. Can you explain why Oxygen should be wrapped by function? |
@jasonheo729 Because code that is not wrapped in functions will be executed before we can polyfill the global The whole story is that, in workers runtime, the const v1 = Oxygen?.env?.VALUE_1; // First thing to run, Oxygen.env is undefined
const getV2 = () => Oxygen.env.VALUE_2;
export default {
fetch(request, env) {
// Oxygen setup
global.Oxygen = {env};
// Run the rest of your app
v1; // undefined
getV2(); // This now has access to Oxygen.env
}
} We are removing the global |
Ah, I see. That helps me a lot.
Thank you again.
…On Tue, Jan 3, 2023 at 6:35 PM Fran Dios ***@***.***> wrote:
@jasonheo729 <https://github.com/jasonheo729> Because code that is not
wrapped in functions will be executed before we can polyfill the global
Oxygen.env and will get undefined. Therefore, instead of running that
code at the very start of the script, you need to delay its execution until
the first time it's needed (e.g. when making the first DB/API request).
The whole story is that, in workers runtime, the env object is scoped to
a request and is only available in the fetch handler function. That's the
first place where we can polyfill the global Oxygen.env:
const v1 = Oxygen?.env?.VALUE_1; // First thing to run, Oxygen.env is undefinedconst getV2 = () => Oxygen.env.VALUE_2;
export default {
fetch(request, env) {
// Oxygen setup
global.Oxygen = {env};
// Run the rest of your app
v1; // undefined
getV2(); // This now has access to Oxygen.env
}}
We are removing the global Oxygen.env in Hydrogen v2 to avoid this kind
of problem. Env vars will be available only by using the original env
parameter.
—
Reply to this email directly, view it on GitHub
<https://github.com/Shopify/hydrogen/issues/2033#issuecomment-1369556375>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ALRTRR2UIFJFVY7CXY4MZTLWQPXHNANCNFSM57NMJGIA>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Can we expose env vars in index.html? |
Describe the bug
I have a
sanity.config.js
file at the root of my project:When I deploy my app to Oxygen,
dataset
is undefined.To Reproduce
PUBLIC_
prefix. E.gPUBLIC_SANITY_DATASET
.js
file and add this line:console.log(import.meta.env.PUBLIC_SANITY_DATASET)
undefined
Expected behaviour
import.meta.env.PUBLIC_SANITY_DATASET
should return the value defined in my Oxygen adminThe text was updated successfully, but these errors were encountered: