Skip to content

Commit c572030

Browse files
icyJosephdevjiwonchoi
authored andcommittedAug 26, 2024
fix: Narrow down from string | undefined to string (#65248)
Co-authored-by: Jiwon Choi <devjiwonchoi@gmail.com>

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ function getNextPublicEnvironmentVariables(): DefineEnv {
6262
for (const key in process.env) {
6363
if (key.startsWith('NEXT_PUBLIC_')) {
6464
const value = process.env[key]
65-
if (value) {
65+
if (value != null) {
6666
defineEnv[`process.env.${key}`] = value
6767
}
6868
}
@@ -79,7 +79,7 @@ function getNextConfigEnv(config: NextConfigComplete): DefineEnv {
7979
const env = config.env
8080
for (const key in env) {
8181
const value = env[key]
82-
if (value) {
82+
if (value != null) {
8383
errorIfEnvConflicted(config, key)
8484
defineEnv[`process.env.${key}`] = value
8585
}

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

+2-1
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

+5-1
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

+7
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)
Please sign in to comment.