Commit c572030a authored by Joseph's avatar Joseph Committed by Zack Tanner

fix: Narrow down from `string | undefined` to `string` (#65248)

Co-authored-by: 's avatarJiwon Choi <devjiwonchoi@gmail.com>
parent b5db7042
......@@ -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
}
}
......@@ -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
}
......
......@@ -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
......@@ -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 }) {
<div id="nextConfigNewPublicEnv">
{process.env.NEXT_PUBLIC_NEW_NEXT_CONFIG_VALUE}
</div>
<div id="nextPublicEmptyEnvVar">
{`${process.env.NEXT_PUBLIC_EMPTY_ENV_VAR}`}
</div>
</>
)
}
......@@ -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('')
})
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment