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 env file not being copied in V3 #432

Merged
merged 3 commits into from
Jun 9, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
khuezy marked this conversation as resolved.
Show resolved Hide resolved
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");
});
Loading