From 5f1270a138920744a07ff189816000e2e1ba0af6 Mon Sep 17 00:00:00 2001 From: Owen Rees Date: Tue, 13 Jun 2023 09:11:16 +0200 Subject: [PATCH] condition env variables for host --- app/tournois/page.tsx | 18 +++++------------- next.config.js | 8 ++++++-- utils/getTournamentData.tsx | 10 +++++----- 3 files changed, 16 insertions(+), 20 deletions(-) diff --git a/app/tournois/page.tsx b/app/tournois/page.tsx index 5c779a9..71ecdfb 100644 --- a/app/tournois/page.tsx +++ b/app/tournois/page.tsx @@ -1,9 +1,11 @@ +import { Tournament } from "@/types"; + import dynamic from "next/dynamic"; import Layout from "@/components/Layout"; import TournamentTable from "@/components/TournamentTable"; +import getTournaments from "@/utils/getTournamentData"; -const server = process.env.SERVER; - +// TODO can these functions be put into a custom hook? /** * Imports the tournament map component, ensuring CSR only. * @remarks SSR is not supported by react-leaflet @@ -18,17 +20,7 @@ const TournamentMap = dynamic(() => import("@/components/TournamentMap"), { }); export default async function Tournaments() { - /** - * Retrieves tournament data from /api/tournaments/:country - * @remarks The result is cached for the revalidation period in seconds - */ - const res = await fetch(`${server}/api/tournaments/france`, { - next: { revalidate: 300 }, - }); - if (res.status !== 200) { - throw new Error("Failed to fetch tournament data"); - } - const tournamentData = await res.json(); + const tournamentData = await getTournaments("france"); return ( diff --git a/next.config.js b/next.config.js index 767719f..95db3ce 100644 --- a/next.config.js +++ b/next.config.js @@ -1,4 +1,8 @@ /** @type {import('next').NextConfig} */ -const nextConfig = {} +const nextConfig = { + env: { + SERVER: process.env.SERVER, + }, +}; -module.exports = nextConfig +module.exports = nextConfig; diff --git a/utils/getTournamentData.tsx b/utils/getTournamentData.tsx index 9c02ee3..ea2e73f 100644 --- a/utils/getTournamentData.tsx +++ b/utils/getTournamentData.tsx @@ -3,11 +3,11 @@ * @remarks The result is cached for the revalidation period in seconds */ export default async function getTournaments(country: string) { - const baseUrl = - process.env.NODE_ENV === "production" - ? `https://${process.env.NEXT_PUBLIC_VERCEL_URL}` - : "http://localhost:3000"; - const res = await fetch(`${baseUrl}/api/tournaments/${country}`, { + const server = process.env.NEXT_PUBLIC_VERCEL_URL + ? process.env.NEXT_PUBLIC_VERCEL_URL + : process.env.SERVER; + + const res = await fetch(`${server}/api/tournaments/${country}`, { next: { revalidate: 300 }, });