diff --git a/front/src/pods/embalse/api/embalse.api.ts b/front/src/pods/embalse/api/embalse.api.ts index 8003a93..39fbfbe 100644 --- a/front/src/pods/embalse/api/embalse.api.ts +++ b/front/src/pods/embalse/api/embalse.api.ts @@ -3,39 +3,32 @@ import { unstable_cache } from "next/cache"; import type { ReservoirInfo } from "./embalse.api-model"; import { contentIslandClient } from "@/lib"; -const fetchReservoirInfoBySlug = async ( - slug: string -): Promise => { - const result = await contentIslandClient.getContent({ - language: "es", - "fields.slug": slug, - }); - - if (!result) { - throw new Error(`Empty reservoir info for slug: ${slug}`); - } +/** + * Cached version of getReservoirInfoBySlug. + * Revalidates every 60 seconds. + */ +export const getReservoirInfoBySlugCached = unstable_cache( + async (slug: string): Promise => { + try { + const result = await contentIslandClient.getContent({ + language: "es", + "fields.slug": slug, + }); - return result; -}; + if (!result) { + console.warn(`Empty reservoir info for slug: ${slug}`); + return null; + } -const fetchReservoirInfoBySlugCached = unstable_cache( - fetchReservoirInfoBySlug, + return result; + } catch (error) { + console.warn( + `Warning reservoir info for slug not available: ${slug}`, + error + ); + return null; + } + }, ["reservoir-by-slug"], { revalidate: 60 } ); - -/** - * Cached version of getReservoirInfoBySlug. - * Revalidates every 60 seconds. - * Only caches when data is available. - */ -export const getReservoirInfoBySlugCached = async ( - slug: string -): Promise => { - try { - return await fetchReservoirInfoBySlugCached(slug); - } catch (error) { - console.warn(`Warning reservoir info for slug not available: ${slug}`); - return null; - } -};