Skip to content

Commit

Permalink
Fix cloudflare runtime env var handling (#7679)
Browse files Browse the repository at this point in the history
Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
Co-authored-by: Matthew Phillips <matthew@skypack.dev>
  • Loading branch information
3 people authored Jul 17, 2023
1 parent d69fe3a commit 1a6f833
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/light-jars-cheat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Handle inlining non-string boolean environment variables
5 changes: 5 additions & 0 deletions .changeset/spicy-pugs-wonder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/cloudflare': patch
---

Fix runtime env var handling
6 changes: 5 additions & 1 deletion packages/astro/src/vite-plugin-env/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ function getPrivateEnv(
// Ignore public env var
if (envPrefixes.every((prefix) => !key.startsWith(prefix))) {
if (typeof process.env[key] !== 'undefined') {
const value = process.env[key];
let value = process.env[key];
// Replacements are always strings, so try to convert to strings here first
if (typeof value !== 'string') {
value = `${value}`;
}
// Boolean values should be inlined to support `export const prerender`
// We already know that these are NOT sensitive values, so inlining is safe
if (value === '0' || value === '1' || value === 'true' || value === 'false') {
Expand Down
8 changes: 8 additions & 0 deletions packages/integrations/cloudflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ export default function createIntegration(args?: Options): AstroIntegration {
}
vite.ssr ||= {};
vite.ssr.target = 'webworker';

// Cloudflare env is only available per request. This isn't feasible for code that access env vars
// in a global way, so we shim their access as `process.env.*`. We will populate `process.env` later
// in its fetch handler.
vite.define = {
'process.env': 'process.env',
...vite.define,
};
}
},
'astro:build:ssr': ({ entryPoints }) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/integrations/cloudflare/test/basics.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { loadFixture, runCLI } from './test-utils.js';
import { expect } from 'chai';
import * as cheerio from 'cheerio';

describe.skip('Basic app', () => {
describe('Basic app', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
/** @type {import('./test-utils').WranglerCLI} */
Expand Down

0 comments on commit 1a6f833

Please sign in to comment.