-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Secret env variables undefined during server build with Cloudflare SSR #4416
Comments
Things I'd like to know:
|
It looks like it's build-time I believe this is because Astro's env handling looks for a Perhaps we can try reading the |
Actually we do read from A workaround for now is to use export default defineConfig({
vite: {
define: {
'process.env.SECRET': JSON.stringify(process.env.SECRET)
}
}
}) I don't think there's much we can do for this issue, other than documenting this, and probably re-work env handling in the future to support Cloudflare (?). |
Thanks, that’s working fine 😁 |
@bluwy that workaround is not working for me. Did something change recently? |
I just ran into the same issue. I was following Astro's documentation for using environment variables with the cloudflare adapter (https://docs.astro.build/en/guides/integrations-guide/cloudflare/#environment-variables) but environment variables weren't defined when deployed to Cloudflare Pages. The issue doesn't pop up locally when running // pages/index.astro
---
const apiBaseUrl = import.meta.env.API_BASE_URL
---
<div>{apiBaseUrl}</div> Only after adding the following I was able to access the env variables in Cloudflare pages export default defineConfig({
vite: {
define: {
'process.env.API_BASE_URL': JSON.stringify(process.env.API_BASE_URL)
}
} Now, I'm unsure, whether the whole thing is a bug, or if it custom env variables should always be defined that way when using the cloudflare adapter (which would mean the docs are misleading) |
Hello, I have been fighting this exact same issue still and have resorted to the vite: { define: {} } solution. Is this still how we are supposed to access Cloudflare env vars? Or is another way supposed to work? |
I'd suggest creating a new issue with a repro as we don't usually track long-closed issues. |
I finally fixed by:
|
Problem Description: Solution: export default defineConfig({
integrations: [tailwind(), react()],
output: "hybrid",
adapter: cloudflare(),
vite: {
define: {
"process.env": process.env
}
}
}); This change allows the environment variables to be accessed correctly and everything works smoothly now. They can be read as usual: |
^ This could be added to docs. |
For anyone looking at this issue and still having no luck after adding the extra vite config... I've twice now experienced an issue where the secrets/env-vars needed to be added to the worker via the dashboard prior to deployment. I had initially been using wrangler CLI to do this but only had success with the above solution once the env vars had been added to my worker settings (in the cloudflare dashboard) prior to running the deployment. I can't explain why this makes a difference but it has solved the issue twice now (on totally different projects) |
What version of
astro
are you using?1.0.6
Are you using an SSR adapter? If so, which one?
Cloudflare
What package manager are you using?
pnpm
What operating system are you using?
Mac
Describe the Bug
Non-public environment variable is not defined during Cloudflare server build, though it works fine in local build. I created a minimal reproduction with test to demonstrate this.
Test
I read env variables in API routes:
Then output them on page (for testing purposes):
Local build
run
pnpm build
andpnpm wrangler pages dev ./dist
and see expected result:Although the site builds fine, the CLI outputs the following error:
Cloudflare server build
Added the environment variables in the Cloudflare Pages console:
Build itself runs OK with no errors output, but unlike local build, it looks like the secret is not available. That is neither when directly used in the function (get function above in api.ts), nor in the frontmatter of the Astro file (put function). PUBLIC_ prefixed variable works as expected:
I expect the length of the secret variable to be output form this Cloudflare server build, just like on the local build.
I need to be able to access a private key in the API function. Is there a workaround other than making it PUBLIC_?
Extra detail
Examining
/dist/_worker.js
locally after build I can see the variables are present:Local versions
I set Node version to 16 for server build using .nvmrc file.
Link to Minimal Reproducible Example
https://github.com/rodneylab/astro-cloudflare
Participation
The text was updated successfully, but these errors were encountered: