Files
plant-disease-id/apps/web/src/lib/api/home.ts
2026-06-05 21:47:00 -04:00

45 lines
1.2 KiB
TypeScript

/**
* Homepage data — fetches featured plants from the Turso DB.
* Uses React's cache() to ensure one fetch per render pass.
* Backed by the async fetch for SSR but stays sync in exported interface
* via a module-level cache pattern.
*/
import { unstable_cache } from "next/cache";
// Re-export the type for convenience
export type { PlantCardData } from "@/components/PlantCard";
/**
* Get featured plants for the homepage.
* Cached via next/cache to avoid repeated DB calls.
*/
export const getFeaturedPlants = unstable_cache(
async () => {
const { getBrowsePlants } = await import("./browse");
const all = await getBrowsePlants();
const FEATURED_IDS = [
"tomato",
"basil",
"rose",
"monstera",
"snake-plant",
"pepper",
"apple",
"corn",
"wheat",
"strawberry",
"blueberry",
"lettuce",
];
const featured = all.filter((p) => FEATURED_IDS.includes(p.id));
if (featured.length < 6) {
const rest = all.filter((p) => !FEATURED_IDS.includes(p.id));
return [...featured, ...rest].slice(0, 12);
}
return featured.slice(0, 12);
},
["featured-plants"],
{ revalidate: 3600 },
);