From 1a6f833c404ba2e64e3497929b64c863b5a348c8 Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Mon, 17 Jul 2023 20:57:08 +0800 Subject: [PATCH] Fix cloudflare runtime env var handling (#7679) Co-authored-by: Emanuele Stoppa Co-authored-by: Matthew Phillips --- .changeset/light-jars-cheat.md | 5 +++++ .changeset/spicy-pugs-wonder.md | 5 +++++ packages/astro/src/vite-plugin-env/index.ts | 6 +++++- packages/integrations/cloudflare/src/index.ts | 8 ++++++++ packages/integrations/cloudflare/test/basics.test.js | 2 +- 5 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 .changeset/light-jars-cheat.md create mode 100644 .changeset/spicy-pugs-wonder.md diff --git a/.changeset/light-jars-cheat.md b/.changeset/light-jars-cheat.md new file mode 100644 index 0000000000..c286810b4a --- /dev/null +++ b/.changeset/light-jars-cheat.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Handle inlining non-string boolean environment variables diff --git a/.changeset/spicy-pugs-wonder.md b/.changeset/spicy-pugs-wonder.md new file mode 100644 index 0000000000..568538810e --- /dev/null +++ b/.changeset/spicy-pugs-wonder.md @@ -0,0 +1,5 @@ +--- +'@astrojs/cloudflare': patch +--- + +Fix runtime env var handling diff --git a/packages/astro/src/vite-plugin-env/index.ts b/packages/astro/src/vite-plugin-env/index.ts index 8d5a9c1c3d..75a37d693b 100644 --- a/packages/astro/src/vite-plugin-env/index.ts +++ b/packages/astro/src/vite-plugin-env/index.ts @@ -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') { diff --git a/packages/integrations/cloudflare/src/index.ts b/packages/integrations/cloudflare/src/index.ts index fa064e0ac2..40ee006f3e 100644 --- a/packages/integrations/cloudflare/src/index.ts +++ b/packages/integrations/cloudflare/src/index.ts @@ -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 }) => { diff --git a/packages/integrations/cloudflare/test/basics.test.js b/packages/integrations/cloudflare/test/basics.test.js index b97079b8f1..9aa78f98e4 100644 --- a/packages/integrations/cloudflare/test/basics.test.js +++ b/packages/integrations/cloudflare/test/basics.test.js @@ -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} */