import 'server-only' import type { Config } from '@/payload-types' import { getGlobalCacheTag } from '@/utilities/globals' import configPromise from '@payload-config' import { unstable_cache } from 'next/cache' import { getPayload } from 'payload' // Config-dependent cached global reads, kept separate from `globals.ts` (the // tag/revalidation contract global hooks import) so the module graph stays // acyclic — see the note in `documents.ts`. type Global = keyof Config['globals'] async function getGlobal(slug: Slug, depth?: number) { const payload = await getPayload({ config: configPromise }) const global = await payload.findGlobal({ slug, depth, }) return global } type CachedGlobalFactory = () => Promise const cachedGlobalFactories = new Map() export const getCachedGlobal = (slug: Slug, depth?: number) => { const tag = getGlobalCacheTag(slug) const cacheKey = `${tag}:${depth ?? 0}` const existing = cachedGlobalFactories.get(cacheKey) as CachedGlobalFactory | undefined if (existing) return existing const factory = unstable_cache(async () => getGlobal(slug, depth), [cacheKey], { tags: [tag], }) as CachedGlobalFactory cachedGlobalFactories.set(cacheKey, factory as CachedGlobalFactory) return factory }