Skip to content

Commit eca0bee

Browse files
authored
fix: force React to use production env (#1056)
* fix: force React to use production env * chore: add cypress test
1 parent 8a26064 commit eca0bee

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

cypress/integration/default/default.spec.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,24 @@ describe('Default site', () => {
55

66
it('loads home page', () => {
77
cy.findByText('Next Demo!')
8-
cy.findByTestId("list-server-side").within(() => {
9-
cy.findAllByRole("link").should('have.length', 5)
8+
cy.findByTestId('list-server-side').within(() => {
9+
cy.findAllByRole('link').should('have.length', 5)
1010
})
1111

12-
cy.findByTestId("list-dynamic-pages").within(() => {
13-
cy.findAllByRole("link").should('have.length', 3)
12+
cy.findByTestId('list-dynamic-pages').within(() => {
13+
cy.findAllByRole('link').should('have.length', 3)
1414
})
1515

16-
cy.findByTestId("list-catch-all").within(() => {
17-
cy.findAllByRole("link").should('have.length', 3)
16+
cy.findByTestId('list-catch-all').within(() => {
17+
cy.findAllByRole('link').should('have.length', 3)
1818
})
1919

20-
cy.findByTestId("list-static").within(() => {
21-
cy.findAllByRole("link").should('have.length', 2)
20+
cy.findByTestId('list-static').within(() => {
21+
cy.findAllByRole('link').should('have.length', 2)
2222
})
2323
})
24-
})
24+
25+
it('sets NODE_ENV', () => {
26+
cy.findByText('NODE_ENV: production')
27+
})
28+
})

demos/default/pages/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import dynamic from 'next/dynamic'
33
const Header = dynamic(() => import(/* webpackChunkName: 'header' */ '../components/Header'), { ssr: true })
44
import { useRouter } from 'next/router'
55

6-
const Index = ({ shows }) => {
6+
const Index = ({ shows, nodeEnv }) => {
77
const { locale } = useRouter()
88

99
return (
@@ -23,6 +23,7 @@ const Index = ({ shows }) => {
2323
<br />
2424
Refresh this page to see it change.
2525
</p>
26+
<p>NODE_ENV: {nodeEnv}</p>
2627

2728
<ul data-testid="list-server-side">
2829
{shows.map(({ id, name }) => (
@@ -215,7 +216,7 @@ Index.getInitialProps = async function () {
215216
const res = await fetch(server)
216217
const data = await res.json()
217218

218-
return { shows: data.slice(0, 5) }
219+
return { shows: data.slice(0, 5), nodeEnv: process.env.NODE_ENV || null }
219220
}
220221

221222
export default Index

src/templates/getHandler.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ const { Bridge } = require('@vercel/node/dist/bridge')
1111

1212
const { augmentFsModule, getMaxAge, getMultiValueHeaders, getNextServer } = require('./handlerUtils')
1313

14+
type Mutable<T> = {
15+
-readonly [K in keyof T]: T[K]
16+
}
17+
1418
const makeHandler =
1519
() =>
1620
// We return a function and then call `toString()` on it to serialise it as the launcher function
@@ -21,13 +25,16 @@ const makeHandler =
2125
// eslint-disable-next-line node/no-missing-require
2226
require.resolve('./pages.js')
2327
} catch {}
24-
// eslint-disable-next-line no-underscore-dangle
25-
process.env._BYPASS_SSG = 'true'
2628

2729
const ONE_YEAR_IN_SECONDS = 31536000
2830

31+
// React assumes you want development mode if NODE_ENV is unset.
32+
;(process.env as Mutable<NodeJS.ProcessEnv>).NODE_ENV ||= 'production'
33+
2934
// We don't want to write ISR files to disk in the lambda environment
3035
conf.experimental.isrFlushToDisk = false
36+
// eslint-disable-next-line no-underscore-dangle
37+
process.env._BYPASS_SSG = 'true'
3138

3239
// Set during the request as it needs the host header. Hoisted so we can define the function once
3340
let base

0 commit comments

Comments
 (0)