Skip to content

Commit 22e80d7

Browse files
authored
Fix env file not being copied in V3 (#432)
* fix env file not copied to the server bundle * add e2e test * Create pretty-spies-rush.md
1 parent 71b3347 commit 22e80d7

File tree

9 files changed

+44
-0
lines changed

9 files changed

+44
-0
lines changed

.changeset/pretty-spies-rush.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"open-next": patch
3+
---
4+
5+
Fix env file not being copied in V3

examples/app-pages-router/.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SOME_ENV_VAR=foo

examples/app-pages-router/app/ssr/page.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export default async function SSR() {
1818
<div>
1919
<h1>Time: {time}</h1>
2020
<div> {headerList.get("host")}</div>
21+
<div>Env: {process.env.SOME_ENV_VAR}</div>
2122
</div>
2223
);
2324
}

examples/pages-router/.env.production

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SOME_PROD_VAR=bar

examples/pages-router/src/pages/ssr/index.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,20 @@ export async function getServerSideProps() {
44
return {
55
props: {
66
time: new Date().toISOString(),
7+
envVar: process.env.SOME_PROD_VAR,
78
},
89
};
910
}
1011

1112
export default function Page({
1213
time,
14+
envVar,
1315
}: InferGetServerSidePropsType<typeof getServerSideProps>) {
1416
return (
1517
<>
1618
<h1>SSR</h1>
1719
<div className="flex">Time: {time}</div>
20+
<div>Env: {envVar}</div>
1821
</>
1922
);
2023
}

packages/open-next/src/build/createServerBundle.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { generateEdgeBundle } from "./edge/createEdgeBundle.js";
2121
import type { BuildOptions } from "./helper.js";
2222
import {
2323
compareSemver,
24+
copyEnvFile,
2425
copyOpenNextConfig,
2526
esbuildAsync,
2627
traverseFiles,
@@ -176,6 +177,9 @@ async function generateBundle(
176177
path.join(outputPath, packagePath),
177178
);
178179

180+
//Copy env files
181+
copyEnvFile(appBuildOutputPath, packagePath, outputPath);
182+
179183
// Copy all necessary traced files
180184
copyTracedFiles(
181185
appBuildOutputPath,

packages/open-next/src/build/helper.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,20 @@ export function copyOpenNextConfig(
254254
path.join(outputPath, "open-next.config.mjs"),
255255
);
256256
}
257+
258+
export function copyEnvFile(
259+
appPath: string,
260+
packagePath: string,
261+
outputPath: string,
262+
) {
263+
const baseAppPath = path.join(appPath, ".next/standalone", packagePath);
264+
const baseOutputPath = path.join(outputPath, packagePath);
265+
const envPath = path.join(baseAppPath, ".env");
266+
if (fs.existsSync(envPath)) {
267+
fs.copyFileSync(envPath, path.join(baseOutputPath, ".env"));
268+
}
269+
const envProdPath = path.join(baseAppPath, ".env.production");
270+
if (fs.existsSync(envProdPath)) {
271+
fs.copyFileSync(envProdPath, path.join(baseOutputPath, ".env.production"));
272+
}
273+
}

packages/tests-e2e/tests/appPagesRouter/ssr.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,9 @@ test("Server Side Render", async ({ page }) => {
2626
await wait(250);
2727
}
2828
});
29+
30+
test("Server Side Render with env", async ({ page }) => {
31+
await page.goto("/ssr");
32+
let el = page.getByText("Env:");
33+
expect(await el.textContent()).toEqual("Env: foo");
34+
});

packages/tests-e2e/tests/pagesRouter/ssr.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,9 @@ test("Server Side Render", async ({ page }) => {
2626
await wait(250);
2727
}
2828
});
29+
30+
test("Server Side Render with env", async ({ page }) => {
31+
await page.goto("/ssr/");
32+
let el = page.getByText("Env:");
33+
expect(await el.textContent()).toEqual("Env: bar");
34+
});

0 commit comments

Comments
 (0)