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

feat: copy .env file in standalone mode #34143

Merged
merged 8 commits into from Feb 10, 2022
1 change: 1 addition & 0 deletions packages/next/build/index.ts
Expand Up @@ -2037,6 +2037,7 @@ export default async function build(
for (const file of [
...requiredServerFiles.files,
path.join(config.distDir, SERVER_FILES_MANIFEST),
'.env',
ijjk marked this conversation as resolved.
Show resolved Hide resolved
]) {
const filePath = path.join(dir, file)
await promises.copyFile(
Expand Down
11 changes: 11 additions & 0 deletions test/production/required-server-files.test.ts
Expand Up @@ -19,6 +19,7 @@ describe('should set-up next', () => {
let appPort
let errors = []
let requiredFilesManifest
const envPath = join(__dirname, 'required-server-files/.env')

beforeAll(async () => {
// test build against environment with next support
Expand All @@ -31,6 +32,7 @@ describe('should set-up next', () => {
'data.txt': new FileRef(
join(__dirname, 'required-server-files/data.txt')
),
'.env': new FileRef(envPath),
},
nextConfig: {
eslint: {
Expand Down Expand Up @@ -722,4 +724,13 @@ describe('should set-up next', () => {
const $ = cheerio.load(html)
expect($('#slug-page').text()).toBe('[slug] page')
})

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

const envVariable = await res.text()
const envFromFile = fs.readFileSync(envPath).toString().split('=')[1]

expect(envFromFile).toBe(envVariable)
})
})
1 change: 1 addition & 0 deletions test/production/required-server-files/.env
@@ -0,0 +1 @@
FOO=bar
3 changes: 3 additions & 0 deletions test/production/required-server-files/pages/api/env.js
@@ -0,0 +1,3 @@
export default function handler(_, res) {
res.send(process.env.FOO)
}