diff --git a/.changeset/shaggy-berries-accept.md b/.changeset/shaggy-berries-accept.md new file mode 100644 index 0000000000..6c6c9f7619 --- /dev/null +++ b/.changeset/shaggy-berries-accept.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Inline `process.env` boolean values (`0`, `1`, `true`, `false`) during the build. This helps with DCE and allows for better `export const prerender` detection. diff --git a/packages/astro/src/vite-plugin-env/index.ts b/packages/astro/src/vite-plugin-env/index.ts index ab88160005..1f21696e30 100644 --- a/packages/astro/src/vite-plugin-env/index.ts +++ b/packages/astro/src/vite-plugin-env/index.ts @@ -31,7 +31,14 @@ function getPrivateEnv( // Ignore public env var if (envPrefixes.every((prefix) => !key.startsWith(prefix))) { if (typeof process.env[key] !== 'undefined') { - privateEnv[key] = `process.env.${key}`; + const value = process.env[key]; + // 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') { + privateEnv[key] = value; + } else { + privateEnv[key] = `process.env.${key}`; + } } else { privateEnv[key] = JSON.stringify(fullEnv[key]); }