diff --git a/front/src/app/page.tsx b/front/src/app/page.tsx index 230461d..41f812b 100644 --- a/front/src/app/page.tsx +++ b/front/src/app/page.tsx @@ -1,7 +1,7 @@ import { EmbalseSearch } from "@/pods/embalse-search"; import { getEmbalsesCollection } from "@/pods/embalse-search/api"; -export const revalidate = 300; +export const dynamic = "force-dynamic"; const RootPage = async () => { const embalses = await getEmbalsesCollection(); diff --git a/front/src/pods/embalse-search/api/embalse.api.ts b/front/src/pods/embalse-search/api/embalse.api.ts index 9f1cabe..4420e9e 100644 --- a/front/src/pods/embalse-search/api/embalse.api.ts +++ b/front/src/pods/embalse-search/api/embalse.api.ts @@ -1,6 +1,24 @@ +import "server-only"; +import { unstable_cache } from "next/cache"; import { getEmbalsesFromDb } from "../embalse-search.repository"; import { Embalse } from "./api.model"; +const getCachedEmbalses = unstable_cache( + async (): Promise => { + const embalses = await getEmbalsesFromDb(); + if (embalses.length === 0) { + throw new Error("Empty embalses - skip cache"); + } + return embalses; + }, + ["embalses-collection"], + { revalidate: 300 } +); + export const getEmbalsesCollection = async (): Promise => { - return getEmbalsesFromDb(); + try { + return await getCachedEmbalses(); + } catch { + return []; + } };