Skip to content

Commit

Permalink
fix: Narrow down from string | undefined to string (#65248)
Browse files Browse the repository at this point in the history
Co-authored-by: Jiwon Choi <[email protected]>
  • Loading branch information
2 people authored and ztanner committed Aug 26, 2024
1 parent b5db704 commit c572030
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
4 changes: 2 additions & 2 deletions packages/next/src/build/webpack/plugins/define-env-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,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
}
}
Expand All @@ -79,7 +79,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
}
Expand Down
3 changes: 2 additions & 1 deletion test/integration/env-config/app/.env
Original file line number Diff line number Diff line change
Expand Up @@ -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"
NEXT_PUBLIC_ENV_KEY_IN_NEXT_CONFIG="hello again from next.config.js"
NEXT_PUBLIC_EMPTY_ENV_VAR=
6 changes: 5 additions & 1 deletion test/integration/env-config/app/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
})
Expand All @@ -53,6 +54,9 @@ export default function Page({ env }) {
<div id="nextConfigNewPublicEnv">
{process.env.NEXT_PUBLIC_NEW_NEXT_CONFIG_VALUE}
</div>
<div id="nextPublicEmptyEnvVar">
{`${process.env.NEXT_PUBLIC_EMPTY_ENV_VAR}`}
</div>
</>
)
}
7 changes: 7 additions & 0 deletions test/integration/env-config/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('')
})
}

Expand Down

0 comments on commit c572030

Please sign in to comment.