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

$env/dynamic/public isn't available in hooks.client.js in prod #8464

Closed
dmkret opened this issue Jan 11, 2023 · 3 comments · Fixed by #8957
Closed

$env/dynamic/public isn't available in hooks.client.js in prod #8464

dmkret opened this issue Jan 11, 2023 · 3 comments · Fixed by #8957
Labels
bug Something isn't working $env Bugs related to the `$env` family of modules low hanging fruit ready to implement please submit PRs for these issues!
Milestone

Comments

@dmkret
Copy link

dmkret commented Jan 11, 2023

Describe the bug

Values from env from $env/dynamic/public should be available in hooks.client.js after build just like in other client-side code, but env is empty {}.

Reproduction

https://stackblitz.com/edit/sveltejs-kit-template-default-yf9uq9?file=src/hooks.server.js

  1. Open browser terminal.
  2. Run npm run build && npm run preview in stackblitz project terminal;
  3. View into browser console.

Expected:

hooks.server.js: dynamic.PUBLIC_VARIABLE = value ✅
hooks.server.js: static.PUBLIC_VARIABLE = value ✅
hooks.client.js: dynamic.PUBLIC_VARIABLE = value ❌ // undefined
hooks.client.js: static.PUBLIC_VARIABLE = value ✅

Logs

hooks.server.js: dynamic.PUBLIC_VARIABLE = value
hooks.server.js: static.PUBLIC_VARIABLE = value
hooks.client.js: dynamic.PUBLIC_VARIABLE = undefined
hooks.client.js: static.PUBLIC_VARIABLE = value

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (7) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 16.14.2 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 7.17.0 - /usr/local/bin/npm
  npmPackages:
    @sveltejs/adapter-auto: ^1.0.0 => 1.0.0 
    @sveltejs/kit: ^1.0.0 => 1.0.11 
    svelte: ^3.54.0 => 3.55.1 
    vite: ^4.0.0 => 4.0.4

Severity

annoyance

Additional Information

Workaround is accessing env inside root layout script.

@benmccann benmccann changed the title $env/dynamic/public doesn't available in hooks.client.js in prod $env/dynamic/public isn't available in hooks.client.js in prod Jan 11, 2023
@dummdidumm
Copy link
Member

I fear we can't do much about this. Accessing env eagerly in the root means that it wasn't set at that time yet. If you do it lazily or inside a setTimeout you'll see it's defined then.

I tend to make this a documentation issue. An alternative solution could be to use await to import the client code dynamically after setting the environment. I'm not sure how much this has an impact on startup time thought, and if it's worth it fixing this.

@dummdidumm dummdidumm added documentation Improvements or additions to documentation $env Bugs related to the `$env` family of modules labels Jan 12, 2023
@Rich-Harris
Copy link
Member

I think we probably could fix it, no? We just need to set the env before importing the hooks. Adds some complexity but feels doable

@Rich-Harris Rich-Harris added bug Something isn't working low hanging fruit ready to implement please submit PRs for these issues! and removed documentation Improvements or additions to documentation labels Jan 28, 2023
@Rich-Harris Rich-Harris added this to the soon milestone Jan 28, 2023
@DoisKoh
Copy link

DoisKoh commented Feb 13, 2023

Go go merge ranger!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working $env Bugs related to the `$env` family of modules low hanging fruit ready to implement please submit PRs for these issues!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants