#env vars don't work as expected in SSR mode with Cloudflare adapter (when when prerender = true)

4 messages · Page 1 of 1 (latest)

kindred arrow
#

The TL;DR is that import.meta.env doesn't work inside getStaticPaths in SSR mode, even when prerender is set to true and my .env file is set up properly.

Hey, all. I am trying to use Notion as a custom CMS for my blog. To do so, I need to make authenticated fetch requests to my Notion site. No problem, right? Except that I cannot get environment variables to work inside my getStaticPaths function! Turning off SSR mode fixes the issue, but I need SSR for some routes, just not the blog ones.

It looks like this:

  const NOTION_SECRET_VITE = import.meta.env.NOTION_SECRET; // This is undefined
  const NOTION_SECRET_REGULAR = process.env.NOTION_SECRET; // This is also undefined

Reading the docs, I couldn't find any reason why this should be happening, so maybe it's a bug?

Some preemptive answers to questions I expect:

  • The .env file is set up as properly
  • This only happens with SSR mode turned on
  • I am exporting const prerender = true, so I would have thought this page would behave in SSG mode.
  • I'm using the Cloudflare adapter
  • I'm using Astro 2.01
kindred arrow
#

It seems I posted this during a time when people are offline 😦

#

OH I FIXED IT.

#

I'm not sure why, but making the envvar public fixed the issue.