teqo/src/utilities/globals.ts
Francisco Ignácio Fontoura Solla 0460b08429 Add allowlisted global cache revalidation for Onda 0 F1.
Extend POST /api/revalidate with posts and global_privacy-policy tags so
direct-DB Onda 0 writes can bust /privacidade without an admin edit.
2026-07-19 22:21:16 -03:00

40 lines
1.3 KiB
TypeScript

import type { Config } from 'src/payload-types'
import configPromise from '@payload-config'
import { revalidateTag, unstable_cache } from 'next/cache'
import { getPayload } from 'payload'
type Global = keyof Config['globals']
export async function getGlobal<Slug extends Global>(slug: Slug, depth?: number) {
const payload = await getPayload({ config: configPromise })
const global = await payload.findGlobal({
slug,
depth,
})
return global
}
export const getGlobalCacheTag = (slug: string) => `global_${slug}`
type CachedGlobalFactory<Slug extends Global = Global> = () => Promise<Config['globals'][Slug]>
const cachedGlobalFactories = new Map<string, CachedGlobalFactory>()
export const getCachedGlobal = <Slug extends Global>(slug: Slug, depth?: number) => {
const tag = getGlobalCacheTag(slug)
const cacheKey = `${tag}:${depth ?? 0}`
const existing = cachedGlobalFactories.get(cacheKey) as CachedGlobalFactory<Slug> | undefined
if (existing) return existing
const factory = unstable_cache(async () => getGlobal(slug, depth), [cacheKey], {
tags: [tag],
}) as CachedGlobalFactory<Slug>
cachedGlobalFactories.set(cacheKey, factory as CachedGlobalFactory)
return factory
}
export const revalidateGlobal = <Slug extends Global>(slug: Slug) =>
revalidateTag(getGlobalCacheTag(slug))