Skip to content

Commit 0b06f80

Browse files
authored
feat: remove NEXT_PUBLIC env vars from docker build args (#13)
* feat: add next public envs to runtime * Commit env vars
1 parent 37d2ebc commit 0b06f80

File tree

19 files changed

+55
-33
lines changed

19 files changed

+55
-33
lines changed

.dockerignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
.env
22

3+
docker-compose.override.yml
4+
35
Dockerfile
46
./**/*/Dockerfile
57

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,6 @@ dist/
4343

4444
# turbo
4545
.turbo
46+
47+
# docker
48+
docker-compose.override.yml

apps/web/Dockerfile

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,6 @@ ENV CI=true
4242

4343
WORKDIR /app
4444

45-
ARG NEXT_PUBLIC_KAN_ENV=${NEXT_PUBLIC_KAN_ENV}
46-
ARG NEXT_PUBLIC_UMAMI_ID=${NEXT_PUBLIC_UMAMI_ID}
47-
ARG NEXT_PUBLIC_BASE_URL=${NEXT_PUBLIC_BASE_URL}
48-
ARG NEXT_PUBLIC_STORAGE_URL=${NEXT_PUBLIC_STORAGE_URL}
49-
ARG NEXT_PUBLIC_STORAGE_DOMAIN=${NEXT_PUBLIC_STORAGE_DOMAIN}
50-
ARG NEXT_PUBLIC_AVATAR_BUCKET_NAME=${NEXT_PUBLIC_AVATAR_BUCKET_NAME}
51-
5245
# Copy lockfile and package.json's of isolated subworkspace
5346
COPY --from=pruner /app/out/pnpm-lock.yaml ./pnpm-lock.yaml
5447
COPY --from=pruner /app/out/pnpm-workspace.yaml ./pnpm-workspace.yaml

apps/web/next.config.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import { fileURLToPath } from "url";
22
import createJiti from "jiti";
3+
import { env } from "next-runtime-env";
4+
import { configureRuntimeEnv } from "next-runtime-env/build/configure.js";
35

46
// Import env files to validate at build time. Use jiti so we can load .ts files in here.
57
createJiti(fileURLToPath(import.meta.url))("./src/env");
68

9+
configureRuntimeEnv();
10+
711
/** @type {import("next").NextConfig} */
812
const config = {
913
reactStrictMode: true,
@@ -22,7 +26,7 @@ const config = {
2226
typescript: { ignoreBuildErrors: true },
2327

2428
images: {
25-
domains: [process.env.NEXT_PUBLIC_STORAGE_DOMAIN ?? ""],
29+
domains: [env("NEXT_PUBLIC_STORAGE_DOMAIN") ?? ""],
2630
},
2731
experimental: {
2832
instrumentationHook: true,

apps/web/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"jwt-decode": "^4.0.0",
3636
"next": "^14.2.15",
3737
"next-logger": "^5.0.1",
38+
"next-runtime-env": "^1.7.2",
3839
"nextjs-cors": "^2.2.0",
3940
"pino": "^9.6.0",
4041
"react": "catalog:react18",

apps/web/public/__ENV.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
window.__ENV = {
2+
NEXT_PUBLIC_UMAMI_ID: "aaed1f55-25e2-4223-b918-e429c390be35",
3+
NEXT_PUBLIC_KAN_ENV: "cloud",
4+
NEXT_PUBLIC_BASE_URL: "http://localhost:3000",
5+
NEXT_PUBLIC_AVATAR_BUCKET_NAME: "avatars",
6+
NEXT_PUBLIC_STORAGE_DOMAIN: "storage.kanbn.com",
7+
NEXT_PUBLIC_STORAGE_URL: "https://storage.kanbn.com",
8+
};

apps/web/src/pages/_app.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import "~/styles/globals.css";
22

33
import type { AppType } from "next/app";
44
import { Plus_Jakarta_Sans } from "next/font/google";
5+
import { env } from "next-runtime-env";
56

6-
import { env } from "~/env";
77
import { ModalProvider } from "~/providers/modal";
88
import { PopupProvider } from "~/providers/popup";
99
import { ThemeProvider } from "~/providers/theme";
@@ -31,13 +31,14 @@ const MyApp: AppType = ({ Component, pageProps }) => {
3131
position: relative;
3232
}
3333
`}</style>
34-
{env.NEXT_PUBLIC_UMAMI_ID && (
34+
{env("NEXT_PUBLIC_UMAMI_ID") && (
3535
<script
3636
defer
3737
src="https://cloud.umami.is/script.js"
38-
data-website-id={env.NEXT_PUBLIC_UMAMI_ID}
38+
data-website-id={env("NEXT_PUBLIC_UMAMI_ID")}
3939
/>
4040
)}
41+
<script src="/__ENV.js" />
4142
<main className="font-sans">
4243
<ThemeProvider>
4344
<ModalProvider>

apps/web/src/pages/api/stripe/create_billing_session.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { NextApiRequest, NextApiResponse } from "next";
2+
import { env } from "next-runtime-env";
23

34
import { createNextApiContext } from "@kan/api/trpc";
45
import { createStripeClient } from "@kan/stripe";
@@ -22,7 +23,7 @@ export default async function handler(
2223

2324
const session = await stripe.billingPortal.sessions.create({
2425
customer: user.stripeCustomerId,
25-
return_url: `${process.env.NEXT_PUBLIC_BASE_URL}/settings`,
26+
return_url: `${env("NEXT_PUBLIC_BASE_URL")}/settings`,
2627
});
2728

2829
return res.status(200).json({ url: session.url });

apps/web/src/pages/api/stripe/create_checkout_session.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { NextApiRequest, NextApiResponse } from "next";
2+
import { env } from "next-runtime-env";
23
import { z } from "zod";
34

45
import { createNextApiContext } from "@kan/api/trpc";
@@ -73,8 +74,8 @@ export default async function handler(
7374
quantity: 1,
7475
},
7576
],
76-
success_url: `${process.env.NEXT_PUBLIC_BASE_URL}${successUrl}`,
77-
cancel_url: `${process.env.NEXT_PUBLIC_BASE_URL}${cancelUrl}`,
77+
success_url: `${env("NEXT_PUBLIC_BASE_URL")}${successUrl}`,
78+
cancel_url: `${env("NEXT_PUBLIC_BASE_URL")}${cancelUrl}`,
7879
customer: user.stripeCustomerId ?? undefined,
7980
metadata: {
8081
workspaceSlug: slug,

apps/web/src/pages/api/upload/image.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import type { NextApiRequest, NextApiResponse } from "next";
22
import { PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
33
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
4-
4+
import { env } from "next-runtime-env";
55
import { createNextApiContext } from "@kan/api/trpc";
66

7-
import { env } from "~/env";
87

98
const allowedContentTypes = ["image/jpeg", "image/png"];
109

@@ -55,7 +54,7 @@ export default async function handler(
5554
// @ts-ignore
5655
client,
5756
new PutObjectCommand({
58-
Bucket: process.env.NEXT_PUBLIC_AVATAR_BUCKET_NAME ?? "",
57+
Bucket: env("NEXT_PUBLIC_AVATAR_BUCKET_NAME") ?? "",
5958
Key: filename,
6059
}),
6160
);

0 commit comments

Comments
 (0)