Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Narrow down from string | undefined to string #65248

Open
wants to merge 7 commits into
base: canary
Choose a base branch
from
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') {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the other test pages on this integration suite, do the very same check

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