Skip to content

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

Closed
@dmkret

Description

@dmkret

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    $envBugs related to the `$env` family of modulesbugSomething isn't workinglow hanging fruitready to implementplease submit PRs for these issues!

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions