diff --git a/.changeset/pretty-spies-rush.md b/.changeset/pretty-spies-rush.md
new file mode 100644
index 00000000..9c62c1d0
--- /dev/null
+++ b/.changeset/pretty-spies-rush.md
@@ -0,0 +1,5 @@
+---
+"open-next": patch
+---
+
+Fix env file not being copied in V3
diff --git a/examples/app-pages-router/.env b/examples/app-pages-router/.env
new file mode 100644
index 00000000..5d8e1786
--- /dev/null
+++ b/examples/app-pages-router/.env
@@ -0,0 +1 @@
+SOME_ENV_VAR=foo
\ No newline at end of file
diff --git a/examples/app-pages-router/app/ssr/page.tsx b/examples/app-pages-router/app/ssr/page.tsx
index ed3c0b22..33063617 100644
--- a/examples/app-pages-router/app/ssr/page.tsx
+++ b/examples/app-pages-router/app/ssr/page.tsx
@@ -18,6 +18,7 @@ export default async function SSR() {
Time: {time}
{headerList.get("host")}
+
Env: {process.env.SOME_ENV_VAR}
);
}
diff --git a/examples/pages-router/.env.production b/examples/pages-router/.env.production
new file mode 100644
index 00000000..82e6f9e7
--- /dev/null
+++ b/examples/pages-router/.env.production
@@ -0,0 +1 @@
+SOME_PROD_VAR=bar
\ No newline at end of file
diff --git a/examples/pages-router/src/pages/ssr/index.tsx b/examples/pages-router/src/pages/ssr/index.tsx
index 659f2302..53236105 100644
--- a/examples/pages-router/src/pages/ssr/index.tsx
+++ b/examples/pages-router/src/pages/ssr/index.tsx
@@ -4,17 +4,20 @@ export async function getServerSideProps() {
return {
props: {
time: new Date().toISOString(),
+ envVar: process.env.SOME_PROD_VAR,
},
};
}
export default function Page({
time,
+ envVar,
}: InferGetServerSidePropsType) {
return (
<>
SSR
Time: {time}
+ Env: {envVar}
>
);
}
diff --git a/packages/open-next/src/build/createServerBundle.ts b/packages/open-next/src/build/createServerBundle.ts
index 506c98e8..5508d9dc 100644
--- a/packages/open-next/src/build/createServerBundle.ts
+++ b/packages/open-next/src/build/createServerBundle.ts
@@ -21,6 +21,7 @@ import { generateEdgeBundle } from "./edge/createEdgeBundle.js";
import type { BuildOptions } from "./helper.js";
import {
compareSemver,
+ copyEnvFile,
copyOpenNextConfig,
esbuildAsync,
traverseFiles,
@@ -176,6 +177,9 @@ async function generateBundle(
path.join(outputPath, packagePath),
);
+ //Copy env files
+ copyEnvFile(appBuildOutputPath, packagePath, outputPath);
+
// Copy all necessary traced files
copyTracedFiles(
appBuildOutputPath,
diff --git a/packages/open-next/src/build/helper.ts b/packages/open-next/src/build/helper.ts
index cbad33d7..2f41d355 100644
--- a/packages/open-next/src/build/helper.ts
+++ b/packages/open-next/src/build/helper.ts
@@ -254,3 +254,20 @@ export function copyOpenNextConfig(
path.join(outputPath, "open-next.config.mjs"),
);
}
+
+export function copyEnvFile(
+ appPath: string,
+ packagePath: string,
+ outputPath: string,
+) {
+ const baseAppPath = path.join(appPath, ".next/standalone", packagePath);
+ const baseOutputPath = path.join(outputPath, packagePath);
+ const envPath = path.join(baseAppPath, ".env");
+ if (fs.existsSync(envPath)) {
+ fs.copyFileSync(envPath, path.join(baseOutputPath, ".env"));
+ }
+ const envProdPath = path.join(baseAppPath, ".env.production");
+ if (fs.existsSync(envProdPath)) {
+ fs.copyFileSync(envProdPath, path.join(baseOutputPath, ".env.production"));
+ }
+}
diff --git a/packages/tests-e2e/tests/appPagesRouter/ssr.test.ts b/packages/tests-e2e/tests/appPagesRouter/ssr.test.ts
index 6acf5b9d..0b907b0f 100644
--- a/packages/tests-e2e/tests/appPagesRouter/ssr.test.ts
+++ b/packages/tests-e2e/tests/appPagesRouter/ssr.test.ts
@@ -26,3 +26,9 @@ test("Server Side Render", async ({ page }) => {
await wait(250);
}
});
+
+test("Server Side Render with env", async ({ page }) => {
+ await page.goto("/ssr");
+ let el = page.getByText("Env:");
+ expect(await el.textContent()).toEqual("Env: foo");
+});
diff --git a/packages/tests-e2e/tests/pagesRouter/ssr.test.ts b/packages/tests-e2e/tests/pagesRouter/ssr.test.ts
index 086bc4f9..c3258733 100644
--- a/packages/tests-e2e/tests/pagesRouter/ssr.test.ts
+++ b/packages/tests-e2e/tests/pagesRouter/ssr.test.ts
@@ -26,3 +26,9 @@ test("Server Side Render", async ({ page }) => {
await wait(250);
}
});
+
+test("Server Side Render with env", async ({ page }) => {
+ await page.goto("/ssr/");
+ let el = page.getByText("Env:");
+ expect(await el.textContent()).toEqual("Env: bar");
+});