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:
Radosław Smoliński
2026-10-22 17:09:00 +02:00
co-authored by Claude Opus 5.5
parent 7c05a1dba9
commit 7967453f57
7 changed files with 98 additions and 5 deletions
+1 -1
View File
@@ -112,7 +112,7 @@ export type { ContentHelpers, EntryJsonLdContext, PageJsonLdOptions, SiteShell }
export { createContentHelpers } from './modules/frontend/index.js'
// Healthcheck route (Coolify/Docker liveness with a real DB round-trip).
export { createHealthRoute } from './modules/health/index.js'
export { buildCrossLocaleFieldSync, buildPreventDeleteSystemPage, buildRevalidateHook, buildValidateUniqueRole, setPublishedAtHook, trackSlugHistoryHook } from './modules/hooks/index.js'
export { buildCrossLocaleFieldSync, buildGlobalRevalidateHook, buildPreventDeleteSystemPage, buildRevalidateHook, buildValidateUniqueRole, setPublishedAtHook, trackSlugHistoryHook } from './modules/hooks/index.js'
export type { I18nConfig, LocaleDefinition, LocalizedSlugs } from './modules/i18n/index.js'
export {
buildLocalizedPath,
+23 -1
View File
@@ -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
}
}
+2 -1
View File
@@ -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'
+10 -2
View File
@@ -17,6 +17,7 @@ import { buildFormsPlugin } from './modules/forms/formsPluginConfig.js'
import { buildLocalizationConfig, validateI18nConfig } from './modules/i18n/index.js'
import { buildSystemPagesFields } from './modules/pages/index.js'
import { buildSeoPlugin, injectAutoFillMeta, injectSeoTabs } from './modules/seo/index.js'
import { buildGlobalRevalidateHook } from './modules/hooks/buildRevalidateHook.js'
import { buildIndexNowHook } from './modules/seo/indexNow.js'
import { buildStatsCollection, buildStatsEndpoint } from './modules/stats/index.js'
@@ -229,8 +230,7 @@ export const ipalKit = (options: IpalOptions): Plugin => {
: []),
]
config.globals = [
...(config.globals ?? []),
const pluginGlobals = [
buildSiteSettings({
additionalFields: options.siteSettingsFields,
...(options.pages ? { constructionFields: buildConstructionFields(options.pages.slug) } : {}),
@@ -241,6 +241,14 @@ export const ipalKit = (options: IpalOptions): Plugin => {
buildCookieSettings(),
buildNotifications(),
]
// A global feeds every page's layout — revalidate the site on save.
const revalidateGlobal = options.revalidatePath ? buildGlobalRevalidateHook({ revalidatePath: options.revalidatePath }) : null
config.globals = [
...(config.globals ?? []),
...(revalidateGlobal
? pluginGlobals.map((g) => ({ ...g, hooks: { ...g.hooks, afterChange: [...(g.hooks?.afterChange ?? []), revalidateGlobal] } }))
: pluginGlobals),
]
config.endpoints = [...(config.endpoints ?? []), testEmailEndpoint]
+10
View File
@@ -12,6 +12,7 @@ export type TotpPluginFactory = (options: {
forceSetup: boolean
totp: { issuer: string }
}) => Plugin
import type { RevalidatePathFn } from './modules/hooks/buildRevalidateHook.js'
import type { AccessOption } from './modules/access/types.js'
import type { ContentOption } from './modules/content/types.js'
import type { FormsOption } from './modules/forms/types.js'
@@ -88,6 +89,15 @@ export type IpalOptions = {
*/
pages?: PagesOption
/**
* next/cache `revalidatePath`, INJECTED (the plugin never imports next/cache).
* When given, saving any of the plugin's globals (Site Settings, Site
* Integrations, Cookie Settings, Notifications) revalidates the whole site
* layout, so logo, menu, consent texts and analytics IDs change at once
* instead of after the ISR window. See buildGlobalRevalidateHook.
*/
revalidatePath?: RevalidatePathFn
/**
* SEO — adds meta fields to chosen collections (via @payloadcms/plugin-seo)
* and enables locale-aware metadata helpers.