teqo/src/utilities/municipalityRevalidation.ts
Francisco Ignácio Fontoura Solla 77023db99d B34+: scale and DRY after municipality chip lists.
Memoize access reads per req, cache portfolio index, trim chip revalidates, and shrink RSC payload via catalog-backed labels.
2026-07-28 19:49:29 -03:00

40 lines
1.3 KiB
TypeScript

import { revalidatePath } from 'next/cache'
/**
* Cache tag for `unstable_cache` entries derived from the município catalog's
* system fields (name / slug / region), which are `readOnly` in admin and
* writable only by `canSetCampaignSystemField` — i.e. by a migration or seed.
* The runbook mirrors `election-tse`: after a migration that adds or renames a
* município, `POST /api/revalidate?tag=municipality-catalog` (allowlist in
* revalidateRequest.ts).
*/
export const MUNICIPALITY_CATALOG_CACHE_TAG = 'municipality-catalog'
type MunicipalityListRevalidateScope = 'list' | 'detail' | 'both'
const municipalitySlugPathPattern = /^[a-z0-9]+(?:-[a-z0-9]+)*$/
type RevalidateMunicipalityListPathsOptions = {
slug?: string
scope?: MunicipalityListRevalidateScope
}
export const revalidateMunicipalityListPaths = ({
slug,
scope = 'both',
}: RevalidateMunicipalityListPathsOptions = {}) => {
const detailSlug = slug && municipalitySlugPathPattern.test(slug) ? slug : undefined
if (scope === 'list' || scope === 'both') {
revalidatePath('/campanha/municipios', 'page')
}
if (scope === 'detail' || scope === 'both') {
if (detailSlug) {
revalidatePath(`/campanha/municipios/${detailSlug}`, 'page')
return
}
revalidatePath('/campanha/municipios/[slug]', 'page')
}
}