Skip to content

Commit

Permalink
Fix env file not being copied in V3 (#432)
Browse files Browse the repository at this point in the history
* fix env file not copied to the server bundle

* add e2e test

* Create pretty-spies-rush.md
  • Loading branch information
conico974 committed Jun 9, 2024
1 parent 71b3347 commit 22e80d7
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/pretty-spies-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"open-next": patch
---

Fix env file not being copied in V3
1 change: 1 addition & 0 deletions examples/app-pages-router/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SOME_ENV_VAR=foo
1 change: 1 addition & 0 deletions examples/app-pages-router/app/ssr/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default async function SSR() {
<div>
<h1>Time: {time}</h1>
<div> {headerList.get("host")}</div>
<div>Env: {process.env.SOME_ENV_VAR}</div>
</div>
);
}
1 change: 1 addition & 0 deletions examples/pages-router/.env.production
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SOME_PROD_VAR=bar
3 changes: 3 additions & 0 deletions examples/pages-router/src/pages/ssr/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof getServerSideProps>) {
return (
<>
<h1>SSR</h1>
<div className="flex">Time: {time}</div>
<div>Env: {envVar}</div>
</>
);
}
4 changes: 4 additions & 0 deletions packages/open-next/src/build/createServerBundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { generateEdgeBundle } from "./edge/createEdgeBundle.js";
import type { BuildOptions } from "./helper.js";
import {
compareSemver,
copyEnvFile,
copyOpenNextConfig,
esbuildAsync,
traverseFiles,
Expand Down Expand Up @@ -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,
Expand Down
17 changes: 17 additions & 0 deletions packages/open-next/src/build/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}
6 changes: 6 additions & 0 deletions packages/tests-e2e/tests/appPagesRouter/ssr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
6 changes: 6 additions & 0 deletions packages/tests-e2e/tests/pagesRouter/ssr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});

0 comments on commit 22e80d7

Please sign in to comment.