Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
c572030a
Commit
c572030a
authored
Jun 26, 2024
by
Joseph
Committed by
Zack Tanner
Aug 26, 2024
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: Narrow down from `string | undefined` to `string` (#65248)
Co-authored-by:
Jiwon Choi
<
devjiwonchoi@gmail.com
>
parent
b5db7042
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
16 additions
and
4 deletions
+16
-4
define-env-plugin.ts
packages/next/src/build/webpack/plugins/define-env-plugin.ts
+2
-2
.env
test/integration/env-config/app/.env
+2
-1
index.js
test/integration/env-config/app/pages/index.js
+5
-1
index.test.js
test/integration/env-config/test/index.test.js
+7
-0
No files found.
packages/next/src/build/webpack/plugins/define-env-plugin.ts
View file @
c572030a
...
@@ -62,7 +62,7 @@ function getNextPublicEnvironmentVariables(): DefineEnv {
...
@@ -62,7 +62,7 @@ function getNextPublicEnvironmentVariables(): DefineEnv {
for
(
const
key
in
process
.
env
)
{
for
(
const
key
in
process
.
env
)
{
if
(
key
.
startsWith
(
'NEXT_PUBLIC_'
))
{
if
(
key
.
startsWith
(
'NEXT_PUBLIC_'
))
{
const
value
=
process
.
env
[
key
]
const
value
=
process
.
env
[
key
]
if
(
value
)
{
if
(
value
!=
null
)
{
defineEnv
[
`process.env.
${
key
}
`
]
=
value
defineEnv
[
`process.env.
${
key
}
`
]
=
value
}
}
}
}
...
@@ -79,7 +79,7 @@ function getNextConfigEnv(config: NextConfigComplete): DefineEnv {
...
@@ -79,7 +79,7 @@ function getNextConfigEnv(config: NextConfigComplete): DefineEnv {
const
env
=
config
.
env
const
env
=
config
.
env
for
(
const
key
in
env
)
{
for
(
const
key
in
env
)
{
const
value
=
env
[
key
]
const
value
=
env
[
key
]
if
(
value
)
{
if
(
value
!=
null
)
{
errorIfEnvConflicted
(
config
,
key
)
errorIfEnvConflicted
(
config
,
key
)
defineEnv
[
`process.env.
${
key
}
`
]
=
value
defineEnv
[
`process.env.
${
key
}
`
]
=
value
}
}
...
...
test/integration/env-config/app/.env
View file @
c572030a
...
@@ -15,4 +15,5 @@ ENV_FILE_EXPANDED_ESCAPED=\$ENV_FILE_KEY
...
@@ -15,4 +15,5 @@ ENV_FILE_EXPANDED_ESCAPED=\$ENV_FILE_KEY
ENV_FILE_KEY_EXCLAMATION="hello!"
ENV_FILE_KEY_EXCLAMATION="hello!"
ENV_FILE_PROCESS_ENV="env-file"
ENV_FILE_PROCESS_ENV="env-file"
ENV_KEY_IN_NEXT_CONFIG="hello from next.config.js"
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"
\ No newline at end of file
NEXT_PUBLIC_EMPTY_ENV_VAR=
\ No newline at end of file
test/integration/env-config/app/pages/index.js
View file @
c572030a
...
@@ -25,13 +25,14 @@ const variables = [
...
@@ -25,13 +25,14 @@ const variables = [
'NEW_ENV_LOCAL_KEY'
,
'NEW_ENV_LOCAL_KEY'
,
'NEW_ENV_DEV_KEY'
,
'NEW_ENV_DEV_KEY'
,
'NEXT_PUBLIC_HELLO_WORLD'
,
'NEXT_PUBLIC_HELLO_WORLD'
,
'NEXT_PUBLIC_EMPTY_ENV_VAR'
,
]
]
export
async
function
getStaticProps
()
{
export
async
function
getStaticProps
()
{
const
items
=
{}
const
items
=
{}
variables
.
forEach
((
variable
)
=>
{
variables
.
forEach
((
variable
)
=>
{
if
(
process
.
env
[
variable
]
)
{
if
(
typeof
process
.
env
[
variable
]
!==
'undefined'
)
{
items
[
variable
]
=
process
.
env
[
variable
]
items
[
variable
]
=
process
.
env
[
variable
]
}
}
})
})
...
@@ -53,6 +54,9 @@ export default function Page({ env }) {
...
@@ -53,6 +54,9 @@ export default function Page({ env }) {
<
div
id
=
"nextConfigNewPublicEnv"
>
<
div
id
=
"nextConfigNewPublicEnv"
>
{
process
.
env
.
NEXT_PUBLIC_NEW_NEXT_CONFIG_VALUE
}
{
process
.
env
.
NEXT_PUBLIC_NEW_NEXT_CONFIG_VALUE
}
<
/div
>
<
/div
>
<
div
id
=
"nextPublicEmptyEnvVar"
>
{
`
${
process
.
env
.
NEXT_PUBLIC_EMPTY_ENV_VAR
}
`
}
<
/div
>
<
/
>
<
/
>
)
)
}
}
test/integration/env-config/test/index.test.js
View file @
c572030a
...
@@ -128,6 +128,13 @@ const runTests = (mode = 'dev', didReload = false) => {
...
@@ -128,6 +128,13 @@ const runTests = (mode = 'dev', didReload = false) => {
expect
(
data
.
ENV_FILE_PRODUCTION_LOCAL_OVERRIDEOVERRIDE_TEST
).
toEqual
(
expect
(
data
.
ENV_FILE_PRODUCTION_LOCAL_OVERRIDEOVERRIDE_TEST
).
toEqual
(
isDev
?
'env'
:
'localproduction'
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
(
''
)
})
})
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment