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 process.env not being available in standalone mode #54203

Merged
merged 1 commit into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions packages/next/src/server/lib/router-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,21 +198,25 @@ export async function initialize(opts: {
},
} as any)

const { initialEnv } = require('@next/env') as typeof import('@next/env')

if (!!config.experimental.appDir) {
renderWorkers.app = await createWorker(
ipcPort,
ipcValidationKey,
opts.isNodeDebugging,
'app',
config
config,
initialEnv
)
}
renderWorkers.pages = await createWorker(
ipcPort,
ipcValidationKey,
opts.isNodeDebugging,
'pages',
config
config,
initialEnv
)

// pre-initialize workers
Expand Down
5 changes: 3 additions & 2 deletions packages/next/src/server/lib/server-ipc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import isError from '../../../lib/is-error'
import { genRenderExecArgv } from '../worker-utils'
import { deserializeErr } from './request-utils'
import { RenderWorker } from '../router-server'
import type { Env } from '@next/env'

// we can't use process.send as jest-worker relies on
// it already and can cause unexpected message errors
Expand Down Expand Up @@ -87,9 +88,9 @@ export const createWorker = async (
ipcValidationKey: string,
isNodeDebugging: boolean | 'brk' | undefined,
type: 'pages' | 'app',
nextConfig: NextConfigComplete
nextConfig: NextConfigComplete,
initialEnv: NodeJS.ProcessEnv | Env = process.env
): Promise<RenderWorker> => {
const { initialEnv } = require('@next/env') as typeof import('@next/env')
const useServerActions = !!nextConfig.experimental.serverActions
const { Worker } =
require('next/dist/compiled/jest-worker') as typeof import('next/dist/compiled/jest-worker')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ export default function handler(_, res) {
env: process.env.FOO,
envLocal: process.env.LOCAL_SECRET,
envProd: process.env.PROD_SECRET,
envFromHost: process.env.ENV_FROM_HOST,
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ describe('should set-up next', () => {
/ready started server on/,
{
...process.env,
ENV_FROM_HOST: 'FOOBAR',
PORT: appPort,
},
undefined,
Expand Down Expand Up @@ -1266,14 +1267,15 @@ describe('should set-up next', () => {
}
})

it('should copy and read .env file', async () => {
it('should read .env files and process.env', async () => {
const res = await fetchViaHTTP(appPort, '/api/env')

const envVariables = await res.json()

expect(envVariables.env).not.toBeUndefined()
expect(envVariables.envProd).not.toBeUndefined()
expect(envVariables.envLocal).toBeUndefined()
expect(envVariables.envFromHost).toBe('FOOBAR')
})

it('should run middleware correctly (without minimalMode, with wasm)', async () => {
Expand Down