Revalidate the site layout when a global is saved
buildGlobalRevalidateHook + ipalKit({ revalidatePath }) wires it into the
plugin's globals; menus, consent texts and analytics IDs no longer stay
stale for the whole ISR window.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
7c05a1dba9
commit
7967453f57
@@ -1,10 +1,12 @@
|
||||
import type { CollectionAfterChangeHook, CollectionAfterDeleteHook } from 'payload'
|
||||
import type { CollectionAfterChangeHook, CollectionAfterDeleteHook, GlobalAfterChangeHook } from 'payload'
|
||||
|
||||
import type { I18nConfig } from '../i18n/index.js'
|
||||
|
||||
import { buildLocalizedPath, getLocaleCodes } from '../i18n/index.js'
|
||||
|
||||
type RevalidateFn = (path: string) => void
|
||||
/** next/cache revalidatePath — the `type` argument is used for 'layout'. */
|
||||
export type RevalidatePathFn = (path: string, type?: 'layout' | 'page') => void
|
||||
|
||||
type BuildRevalidateHookArgs = {
|
||||
config: I18nConfig
|
||||
@@ -114,3 +116,23 @@ export function buildRevalidateHook({
|
||||
|
||||
return { afterChange, afterDelete }
|
||||
}
|
||||
|
||||
/**
|
||||
* afterChange for a GLOBAL (Site Settings, Cookie Settings, a menu…): a global
|
||||
* feeds the layout of every page — logo, menu, consent texts, analytics IDs —
|
||||
* so a save revalidates the whole site (`revalidatePath('/', 'layout')`).
|
||||
* Without it, every ISR page kept the old values until its revalidate window
|
||||
* ran out. Same rules as buildRevalidateHook: injected revalidatePath, no-op
|
||||
* outside a Next server or with `context: { disableRevalidate: true }`.
|
||||
*
|
||||
* import { revalidatePath } from 'next/cache'
|
||||
* hooks: { afterChange: [buildGlobalRevalidateHook({ revalidatePath })] }
|
||||
*
|
||||
* ipalKit({ revalidatePath }) attaches it to the plugin's own globals.
|
||||
*/
|
||||
export function buildGlobalRevalidateHook({ revalidatePath }: { revalidatePath: RevalidatePathFn }): GlobalAfterChangeHook {
|
||||
return ({ context, doc }) => {
|
||||
if (shouldRevalidate(context)) {revalidatePath('/', 'layout')}
|
||||
return doc
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,8 @@
|
||||
export { normalizeFilenameHook } from '../media/index.js'
|
||||
export { buildAutoFillMetaHook, validateFaviconField } from '../seo/index.js'
|
||||
export { buildCrossLocaleFieldSync } from './buildCrossLocaleFieldSync.js'
|
||||
export { buildRevalidateHook } from './buildRevalidateHook.js'
|
||||
export { buildGlobalRevalidateHook, buildRevalidateHook } from './buildRevalidateHook.js'
|
||||
export type { RevalidatePathFn } from './buildRevalidateHook.js'
|
||||
export { buildPreventDeleteSystemPage } from './preventDeleteSystemPage.js'
|
||||
export { setPublishedAtHook } from './setPublishedAt.js'
|
||||
|
||||
|
||||
Reference in New Issue
Block a user