Skip to content

Commit c839d58

Browse files
fix: Narrow down from string | undefined to string (#65248)
Co-authored-by: Jiwon Choi <[email protected]>
1 parent a832c91 commit c839d58

File tree

4 files changed

+16
-4
lines changed

4 files changed

+16
-4
lines changed

packages/next/src/build/webpack/plugins/define-env-plugin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ function getNextPublicEnvironmentVariables(): DefineEnv {
6363
for (const key in process.env) {
6464
if (key.startsWith('NEXT_PUBLIC_')) {
6565
const value = process.env[key]
66-
if (value) {
66+
if (value != null) {
6767
defineEnv[`process.env.${key}`] = value
6868
}
6969
}
@@ -80,7 +80,7 @@ function getNextConfigEnv(config: NextConfigComplete): DefineEnv {
8080
const env = config.env
8181
for (const key in env) {
8282
const value = env[key]
83-
if (value) {
83+
if (value != null) {
8484
errorIfEnvConflicted(config, key)
8585
defineEnv[`process.env.${key}`] = value
8686
}

test/integration/env-config/app/.env

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ ENV_FILE_EXPANDED_ESCAPED=\$ENV_FILE_KEY
1515
ENV_FILE_KEY_EXCLAMATION="hello!"
1616
ENV_FILE_PROCESS_ENV="env-file"
1717
ENV_KEY_IN_NEXT_CONFIG="hello from next.config.js"
18-
NEXT_PUBLIC_ENV_KEY_IN_NEXT_CONFIG="hello again from next.config.js"
18+
NEXT_PUBLIC_ENV_KEY_IN_NEXT_CONFIG="hello again from next.config.js"
19+
NEXT_PUBLIC_EMPTY_ENV_VAR=

test/integration/env-config/app/pages/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@ const variables = [
2525
'NEW_ENV_LOCAL_KEY',
2626
'NEW_ENV_DEV_KEY',
2727
'NEXT_PUBLIC_HELLO_WORLD',
28+
'NEXT_PUBLIC_EMPTY_ENV_VAR',
2829
]
2930

3031
export async function getStaticProps() {
3132
const items = {}
3233

3334
variables.forEach((variable) => {
34-
if (process.env[variable]) {
35+
if (typeof process.env[variable] !== 'undefined') {
3536
items[variable] = process.env[variable]
3637
}
3738
})
@@ -53,6 +54,9 @@ export default function Page({ env }) {
5354
<div id="nextConfigNewPublicEnv">
5455
{process.env.NEXT_PUBLIC_NEW_NEXT_CONFIG_VALUE}
5556
</div>
57+
<div id="nextPublicEmptyEnvVar">
58+
{`${process.env.NEXT_PUBLIC_EMPTY_ENV_VAR}`}
59+
</div>
5660
</>
5761
)
5862
}

test/integration/env-config/test/index.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,13 @@ const runTests = (mode = 'dev', didReload = false) => {
128128
expect(data.ENV_FILE_PRODUCTION_LOCAL_OVERRIDEOVERRIDE_TEST).toEqual(
129129
isDev ? 'env' : 'localproduction'
130130
)
131+
expect(data.NEXT_PUBLIC_EMPTY_ENV_VAR).toEqual('')
132+
133+
const browser = await webdriver(appPort, '/')
134+
// Verify that after hydration, the empty env var is not replaced by undefined
135+
expect(
136+
await browser.waitForElementByCss('#nextPublicEmptyEnvVar').text()
137+
).toBe('')
131138
})
132139
}
133140

0 commit comments

Comments
 (0)