From a3e7998d111b13a7252a5b2aeef0207c7d63a4dd Mon Sep 17 00:00:00 2001 From: theoleuthardt Date: Fri, 28 Feb 2025 15:14:22 +0100 Subject: [PATCH] fix: use conditionally the right url for backend in nextjs frontend --- .env | 4 +--- backend/server.ts | 4 ++++ frontend/Dockerfile | 3 ++- frontend/next.config.ts | 7 ++++++- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/.env b/.env index f570331..7d9e540 100644 --- a/.env +++ b/.env @@ -2,6 +2,4 @@ NODE_ENV=development HOSTNAME=0.0.0.0 FRONTEND_PORT=3000 -BACKEND_PORT=4000 -NEXT_PUBLIC_BACKEND_URL=http://localhost:4000 -CORS_ALLOWED_ORIGIN=http://localhost:3000 \ No newline at end of file +BACKEND_PORT=4000 \ No newline at end of file diff --git a/backend/server.ts b/backend/server.ts index 1b7bd3e..fbdfc91 100644 --- a/backend/server.ts +++ b/backend/server.ts @@ -10,6 +10,10 @@ import { generateQRCode } from "./src/routes/generateqrcode.route"; import { wordCounter } from "./src/routes/wordcounter.route"; import { videoToAudio } from "./src/routes/videotoaudio.route"; import { removeBG } from "./src/routes/removebg.route"; +import dotenv from "dotenv"; +import path from "node:path"; + +dotenv.config({ path: path.resolve(__dirname, "../.env") }); const app = Fastify({ logger: true }); diff --git a/frontend/Dockerfile b/frontend/Dockerfile index 8c2aca8..6261969 100644 --- a/frontend/Dockerfile +++ b/frontend/Dockerfile @@ -24,6 +24,7 @@ WORKDIR /app COPY --from=deps /app/node_modules ./node_modules COPY . . +ENV NODE_ENV=production ARG NEXT_PUBLIC_BACKEND_URL ENV NEXT_PUBLIC_BACKEND_URL=$NEXT_PUBLIC_BACKEND_URL @@ -62,4 +63,4 @@ EXPOSE 3000 ENV PORT=3000 ENV HOSTNAME="0.0.0.0" -CMD echo "NEXT_PUBLIC_BACKEND_URL = $NEXT_PUBLIC_BACKEND_URL" && node server.js \ No newline at end of file +CMD node server.js \ No newline at end of file diff --git a/frontend/next.config.ts b/frontend/next.config.ts index 61ab14e..6030ff1 100644 --- a/frontend/next.config.ts +++ b/frontend/next.config.ts @@ -1,9 +1,14 @@ import type { NextConfig } from "next"; +const NEXT_PUBLIC_BACKEND_URL = + process.env.NODE_ENV === "production" + ? process.env.NEXT_PUBLIC_BACKEND_URL || "" + : "http://localhost:4000"; + const nextConfig: NextConfig = { output: "standalone", env: { - NEXT_PUBLIC_BACKEND_URL: process.env.NEXT_PUBLIC_BACKEND_URL, + NEXT_PUBLIC_BACKEND_URL: NEXT_PUBLIC_BACKEND_URL, }, };