Skip to content

Commit

Permalink
Ensure env is loaded before next config (#22879)
Browse files Browse the repository at this point in the history
This ensures we load all env values before loading `next.config.js` since these values can be used in there. This also updates to ensure we're testing these values are available while loading `next.config.js` so we don't regress on this. 

Fixes: #22811
  • Loading branch information
ijjk authored Mar 8, 2021
1 parent 9e06f82 commit e950677
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 8 deletions.
6 changes: 5 additions & 1 deletion packages/next/next-server/server/config-utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { loadEnvConfig } from '@next/env'
import Worker from 'jest-worker'
import findUp from 'next/dist/compiled/find-up'
import { init as initWebpack } from 'next/dist/compiled/webpack/webpack'
import { CONFIG_FILE } from '../lib/constants'
import { CONFIG_FILE, PHASE_DEVELOPMENT_SERVER } from '../lib/constants'
import { NextConfig, normalizeConfig } from './config-shared'
import * as Log from '../../build/output/log'

let installed: boolean = false

Expand All @@ -23,6 +25,8 @@ export async function shouldLoadWithWebpack5(
phase: string,
dir: string
): Promise<boolean> {
await loadEnvConfig(dir, phase === PHASE_DEVELOPMENT_SERVER, Log)

const path = await findUp(CONFIG_FILE, {
cwd: dir,
})
Expand Down
4 changes: 3 additions & 1 deletion packages/next/next-server/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ import findUp from 'next/dist/compiled/find-up'
import { basename, extname } from 'path'
import * as Log from '../../build/output/log'
import { hasNextSupport } from '../../telemetry/ci-info'
import { CONFIG_FILE } from '../lib/constants'
import { CONFIG_FILE, PHASE_DEVELOPMENT_SERVER } from '../lib/constants'
import { execOnce } from '../lib/utils'
import { defaultConfig, normalizeConfig } from './config-shared'
import { loadWebpackHook } from './config-utils'
import { ImageConfig, imageConfigDefault, VALID_LOADERS } from './image-config'
import { loadEnvConfig } from '@next/env'

export { DomainLocales, NextConfig, normalizeConfig } from './config-shared'

Expand Down Expand Up @@ -394,6 +395,7 @@ export default async function loadConfig(
dir: string,
customConfig?: object | null
) {
await loadEnvConfig(dir, phase === PHASE_DEVELOPMENT_SERVER, Log)
await loadWebpackHook(phase, dir)

if (customConfig) {
Expand Down
4 changes: 3 additions & 1 deletion test/integration/env-config/app/.env
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ ENV_FILE_EXPANDED=$ENV_FILE_KEY
ENV_FILE_EXPANDED_CONCAT=hello-${ENV_FILE_KEY}
ENV_FILE_EXPANDED_ESCAPED=\$ENV_FILE_KEY
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"
NEXT_PUBLIC_ENV_KEY_IN_NEXT_CONFIG="hello again from next.config.js"
4 changes: 4 additions & 0 deletions test/integration/env-config/app/next.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
module.exports = {
// update me
env: {
nextConfigEnv: process.env.ENV_KEY_IN_NEXT_CONFIG,
nextConfigPublicEnv: process.env.NEXT_PUBLIC_ENV_KEY_IN_NEXT_CONFIG,
},
async redirects() {
return [
{
Expand Down
5 changes: 4 additions & 1 deletion test/integration/env-config/app/pages/api/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ const variables = [
'ENV_FILE_KEY_EXCLAMATION',
]

const items = {}
const items = {
nextConfigEnv: process.env.nextConfigEnv,
nextConfigPublicEnv: process.env.nextConfigPublicEnv,
}

variables.forEach((variable) => {
items[variable] = process.env[variable]
Expand Down
8 changes: 7 additions & 1 deletion test/integration/env-config/app/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,10 @@ export async function getStaticProps() {
}
}

export default ({ env }) => <p>{JSON.stringify(env)}</p>
export default ({ env }) => (
<>
<p>{JSON.stringify(env)}</p>
<div id="nextConfigEnv">{process.env.nextConfigEnv}</div>
<div id="nextConfigPublicEnv">{process.env.nextConfigPublicEnv}</div>
</>
)
8 changes: 7 additions & 1 deletion test/integration/env-config/app/pages/some-ssg.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,10 @@ export async function getStaticProps() {
}
}

export default ({ env }) => <p>{JSON.stringify(env)}</p>
export default ({ env }) => (
<>
<p>{JSON.stringify(env)}</p>
<div id="nextConfigEnv">{process.env.nextConfigEnv}</div>
<div id="nextConfigPublicEnv">{process.env.nextConfigPublicEnv}</div>
</>
)
8 changes: 7 additions & 1 deletion test/integration/env-config/app/pages/some-ssp.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,10 @@ export async function getServerSideProps() {
}
}

export default ({ env }) => <p>{JSON.stringify(env)}</p>
export default ({ env }) => (
<>
<p>{JSON.stringify(env)}</p>
<div id="nextConfigEnv">{process.env.nextConfigEnv}</div>
<div id="nextConfigPublicEnv">{process.env.nextConfigPublicEnv}</div>
</>
)
9 changes: 8 additions & 1 deletion test/integration/env-config/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ const appDir = join(__dirname, '../app')

const getEnvFromHtml = async (path) => {
const html = await renderViaHTTP(appPort, path)
return JSON.parse(cheerio.load(html)('p').text())
const $ = cheerio.load(html)
const env = JSON.parse($('p').text())
env.nextConfigEnv = $('#nextConfigEnv').text()
env.nextConfigPublicEnv = $('#nextConfigPublicEnv').text()
return env
}

const runTests = (mode = 'dev') => {
Expand Down Expand Up @@ -53,6 +57,9 @@ const runTests = (mode = 'dev') => {
expect(data.ENV_FILE_KEY_EXCLAMATION).toBe('hello!')
expect(data.ENV_FILE_EMPTY_FIRST).toBe(isTestEnv ? '' : '$escaped')
expect(data.ENV_FILE_PROCESS_ENV).toBe('env-cli')

expect(data.nextConfigEnv).toBe('hello from next.config.js')
expect(data.nextConfigPublicEnv).toBe('hello again from next.config.js')
}

it('should have process environment override .env', async () => {
Expand Down

0 comments on commit e950677

Please sign in to comment.