Astro SSR environment variables are undefined

I’m trying to deploy my Astro SSR app to fly.io but all my env vars are undefined.

I created an Astro SSR app just to test this issue. It only has one page, set up in ssr mode with node adapter.

It doesn’t matter if I set it with fly secrets, the fly.toml or in the web interface. Re-deploying the secrets or the whole app doesn’t fix the issue. When I set the secrets with the CLI, it does register on the web interface so the problem must be with the build.

The app can read the env vars in development mode and the local build works too. I need these env vars in runtime. The Astro and Fly.io documentation doesn’t say anything about additional steps or I couldn’t find anything.

I would really appreciate some help here. Thanks in advance.

The env vars prefixed with PUBLIC_ so it should be accessible from the client side code.

---
import Layout from "@/layouts/Layout.astro"

const ENV_VAR = import.meta.env.PUBLIC_ENV_VAR
console.log(ENV_VAR);
---

<Layout title="Welcome to Astro." desc="Welcome to Astro">
  <main>{ENV_VAR}</main>
</Layout>

<script>
  console.log(import.meta.env.PUBLIC_ENV_VAR);
</script>

All the imports are undefined in fly.io

SSH-ing In the VM I can echo $PUBLIC_ENV_VAR and the value is there.

The issue might be in my code but as I said, I can build the app and when I preview it, it works just fine.

Astro uses Vite’s built-in support for environment variables, which are statically replaced at build time

Using environment variables | Docs

This means that Astro can only use ENV variables that are set in the Dockerfile itself. If any of those variables are secrets, you will need to use mounts: Build Secrets · Fly Docs

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.