diff --git a/packages/next/src/build/webpack/plugins/define-env-plugin.ts b/packages/next/src/build/webpack/plugins/define-env-plugin.ts index 66c6cc90efb5b..644593b95d34b 100644 --- a/packages/next/src/build/webpack/plugins/define-env-plugin.ts +++ b/packages/next/src/build/webpack/plugins/define-env-plugin.ts @@ -63,7 +63,7 @@ function getNextPublicEnvironmentVariables(): DefineEnv { for (const key in process.env) { if (key.startsWith('NEXT_PUBLIC_')) { const value = process.env[key] - if (value) { + if (value != null) { defineEnv[`process.env.${key}`] = value } } @@ -80,7 +80,7 @@ function getNextConfigEnv(config: NextConfigComplete): DefineEnv { const env = config.env for (const key in env) { const value = env[key] - if (value) { + if (value != null) { errorIfEnvConflicted(config, key) defineEnv[`process.env.${key}`] = value } diff --git a/test/integration/env-config/app/.env b/test/integration/env-config/app/.env index 12e4845832a63..6dca94d1f566b 100644 --- a/test/integration/env-config/app/.env +++ b/test/integration/env-config/app/.env @@ -15,4 +15,5 @@ ENV_FILE_EXPANDED_ESCAPED=\$ENV_FILE_KEY ENV_FILE_KEY_EXCLAMATION="hello!" ENV_FILE_PROCESS_ENV="env-file" ENV_KEY_IN_NEXT_CONFIG="hello from next.config.js" -NEXT_PUBLIC_ENV_KEY_IN_NEXT_CONFIG="hello again from next.config.js" \ No newline at end of file +NEXT_PUBLIC_ENV_KEY_IN_NEXT_CONFIG="hello again from next.config.js" +NEXT_PUBLIC_EMPTY_ENV_VAR= \ No newline at end of file diff --git a/test/integration/env-config/app/pages/index.js b/test/integration/env-config/app/pages/index.js index 13edfd1c3d338..230ee07a28275 100644 --- a/test/integration/env-config/app/pages/index.js +++ b/test/integration/env-config/app/pages/index.js @@ -25,13 +25,14 @@ const variables = [ 'NEW_ENV_LOCAL_KEY', 'NEW_ENV_DEV_KEY', 'NEXT_PUBLIC_HELLO_WORLD', + 'NEXT_PUBLIC_EMPTY_ENV_VAR', ] export async function getStaticProps() { const items = {} variables.forEach((variable) => { - if (process.env[variable]) { + if (typeof process.env[variable] !== 'undefined') { items[variable] = process.env[variable] } }) @@ -53,6 +54,9 @@ export default function Page({ env }) {
{process.env.NEXT_PUBLIC_NEW_NEXT_CONFIG_VALUE}
+
+ {`${process.env.NEXT_PUBLIC_EMPTY_ENV_VAR}`} +
) } diff --git a/test/integration/env-config/test/index.test.js b/test/integration/env-config/test/index.test.js index 40c26cd0904df..4069b05112025 100644 --- a/test/integration/env-config/test/index.test.js +++ b/test/integration/env-config/test/index.test.js @@ -128,6 +128,13 @@ const runTests = (mode = 'dev', didReload = false) => { expect(data.ENV_FILE_PRODUCTION_LOCAL_OVERRIDEOVERRIDE_TEST).toEqual( isDev ? 'env' : 'localproduction' ) + expect(data.NEXT_PUBLIC_EMPTY_ENV_VAR).toEqual('') + + const browser = await webdriver(appPort, '/') + // Verify that after hydration, the empty env var is not replaced by undefined + expect( + await browser.waitForElementByCss('#nextPublicEmptyEnvVar').text() + ).toBe('') }) }