From 1fe7beba6fe67947071d0be6308b3d93cf2f5016 Mon Sep 17 00:00:00 2001 From: RaviAnand Mohabir Date: Wed, 28 Aug 2024 11:29:02 +0200 Subject: [PATCH] feat: :sparkles: add cache and cache revalidation to getHome --- src/api/home.ts | 13 +++++++++---- src/globals/Home.ts | 8 ++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/api/home.ts b/src/api/home.ts index 8c0d718..12cede1 100644 --- a/src/api/home.ts +++ b/src/api/home.ts @@ -1,7 +1,12 @@ import type { Options } from "node_modules/payload/dist/globals/operations/local/findOne"; import { getPayload } from "@/utils/payload"; +import { unstable_cache } from "next/cache"; -export const getHome = async (opts: Omit, "slug"> = {}) => { - const payload = await getPayload(); - return await payload.findGlobal({ slug: "home", ...opts }); -}; +export const getHome = unstable_cache( + async (opts: Omit, "slug"> = {}) => { + const payload = await getPayload(); + return await payload.findGlobal({ slug: "home", ...opts }); + }, + [], + { revalidate: 60 * 60, tags: ["home"] }, +); diff --git a/src/globals/Home.ts b/src/globals/Home.ts index b7f1c0c..93a4fb5 100644 --- a/src/globals/Home.ts +++ b/src/globals/Home.ts @@ -1,4 +1,5 @@ import type { GlobalConfig } from "payload"; +import { revalidateTag } from "next/cache"; export const Home: GlobalConfig = { slug: "home", @@ -23,4 +24,11 @@ export const Home: GlobalConfig = { required: true, }, ], + hooks: { + afterChange: [ + async () => { + revalidateTag("home"); + }, + ], + }, };