Skip to content

Commit

Permalink
Fix process.env overriden in web runtime (#31261)
Browse files Browse the repository at this point in the history
Fixes #31258

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`
  • Loading branch information
huozhi committed Nov 10, 2021
1 parent fd95935 commit edb5060
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 2 deletions.
4 changes: 3 additions & 1 deletion packages/next/build/webpack-config.ts
Expand Up @@ -1337,8 +1337,10 @@ export default async function getBaseWebpackConfig(
// Makes sure `Buffer` and `process` are polyfilled in client and flight bundles (same behavior as webpack 4)
targetWeb &&
new webpack.ProvidePlugin({
// Buffer is used by getInlineScriptSource
Buffer: [require.resolve('buffer'), 'Buffer'],
process: [require.resolve('process')],
// Avoid process being overridden when in web run time
...(!isServer && { process: [require.resolve('process')] }),
}),
new webpack.DefinePlugin({
...Object.keys(process.env).reduce(
Expand Down
@@ -0,0 +1 @@
ENV_VAR_TEST="env_var_test"
@@ -1,10 +1,15 @@
import Foo from '../components/foo.client'

const envVar = process.env.ENV_VAR_TEST

export default function Index() {
return (
<div>
<h1>{`thisistheindexpage.server`}</h1>
<Foo />
<div>{envVar}</div>
<div>
<Foo />
</div>
</div>
)
}
Expand Up @@ -224,6 +224,7 @@ async function runBasicTests(context) {
)

expect(homeHTML).toContain('thisistheindexpage.server')
expect(homeHTML).toContain('env_var_test')
expect(homeHTML).toContain('foo.client')

expect(dynamicRouteHTML1).toContain('[pid]')
Expand Down

0 comments on commit edb5060

Please sign in to comment.