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
@@ -42,6 +42,24 @@ brak `process.env.NEXT_RUNTIME`. Wcześniej Next rzucał `Invariant: static gene
|
|||||||
i seed padał. Celowe pominięcie (masowy import w aplikacji): `context: { disableRevalidate: true }`
|
i seed padał. Celowe pominięcie (masowy import w aplikacji): `context: { disableRevalidate: true }`
|
||||||
w wywołaniu Local API.
|
w wywołaniu Local API.
|
||||||
|
|
||||||
|
### Globale — buildGlobalRevalidateHook / `ipalKit({ revalidatePath })`
|
||||||
|
|
||||||
|
Global (Site Settings, Cookie Settings, menu projektu) zasila layout **każdej** strony — logo, menu,
|
||||||
|
teksty zgody, ID analityki. Bez rewalidacji strony ISR trzymały stare wartości do końca okna
|
||||||
|
`revalidate`. Zapis globala → `revalidatePath('/', 'layout')` (cała strona).
|
||||||
|
|
||||||
|
```ts
|
||||||
|
// payload.config.ts — globale pluginu (Site Settings, Site Integrations, Cookie Settings, Notifications)
|
||||||
|
import { revalidatePath } from 'next/cache'
|
||||||
|
ipalKit({ …, revalidatePath })
|
||||||
|
|
||||||
|
// własny global projektu (np. menu)
|
||||||
|
import { buildGlobalRevalidateHook } from '@intecion/ipal-kit'
|
||||||
|
hooks: { afterChange: [buildGlobalRevalidateHook({ revalidatePath })] }
|
||||||
|
```
|
||||||
|
|
||||||
|
Te same reguły co wyżej: bez `NEXT_RUNTIME` i z `context: { disableRevalidate: true }` nic nie robi.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## setPublishedAtHook — auto-data publikacji
|
## setPublishedAtHook — auto-data publikacji
|
||||||
|
|||||||
+1
-1
@@ -112,7 +112,7 @@ export type { ContentHelpers, EntryJsonLdContext, PageJsonLdOptions, SiteShell }
|
|||||||
export { createContentHelpers } from './modules/frontend/index.js'
|
export { createContentHelpers } from './modules/frontend/index.js'
|
||||||
// Healthcheck route (Coolify/Docker liveness with a real DB round-trip).
|
// Healthcheck route (Coolify/Docker liveness with a real DB round-trip).
|
||||||
export { createHealthRoute } from './modules/health/index.js'
|
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 type { I18nConfig, LocaleDefinition, LocalizedSlugs } from './modules/i18n/index.js'
|
||||||
export {
|
export {
|
||||||
buildLocalizedPath,
|
buildLocalizedPath,
|
||||||
|
|||||||
@@ -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 type { I18nConfig } from '../i18n/index.js'
|
||||||
|
|
||||||
import { buildLocalizedPath, getLocaleCodes } from '../i18n/index.js'
|
import { buildLocalizedPath, getLocaleCodes } from '../i18n/index.js'
|
||||||
|
|
||||||
type RevalidateFn = (path: string) => void
|
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 = {
|
type BuildRevalidateHookArgs = {
|
||||||
config: I18nConfig
|
config: I18nConfig
|
||||||
@@ -114,3 +116,23 @@ export function buildRevalidateHook({
|
|||||||
|
|
||||||
return { afterChange, afterDelete }
|
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 { normalizeFilenameHook } from '../media/index.js'
|
||||||
export { buildAutoFillMetaHook, validateFaviconField } from '../seo/index.js'
|
export { buildAutoFillMetaHook, validateFaviconField } from '../seo/index.js'
|
||||||
export { buildCrossLocaleFieldSync } from './buildCrossLocaleFieldSync.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 { buildPreventDeleteSystemPage } from './preventDeleteSystemPage.js'
|
||||||
export { setPublishedAtHook } from './setPublishedAt.js'
|
export { setPublishedAtHook } from './setPublishedAt.js'
|
||||||
|
|
||||||
|
|||||||
+10
-2
@@ -17,6 +17,7 @@ import { buildFormsPlugin } from './modules/forms/formsPluginConfig.js'
|
|||||||
import { buildLocalizationConfig, validateI18nConfig } from './modules/i18n/index.js'
|
import { buildLocalizationConfig, validateI18nConfig } from './modules/i18n/index.js'
|
||||||
import { buildSystemPagesFields } from './modules/pages/index.js'
|
import { buildSystemPagesFields } from './modules/pages/index.js'
|
||||||
import { buildSeoPlugin, injectAutoFillMeta, injectSeoTabs } from './modules/seo/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 { buildIndexNowHook } from './modules/seo/indexNow.js'
|
||||||
import { buildStatsCollection, buildStatsEndpoint } from './modules/stats/index.js'
|
import { buildStatsCollection, buildStatsEndpoint } from './modules/stats/index.js'
|
||||||
|
|
||||||
@@ -229,8 +230,7 @@ export const ipalKit = (options: IpalOptions): Plugin => {
|
|||||||
: []),
|
: []),
|
||||||
]
|
]
|
||||||
|
|
||||||
config.globals = [
|
const pluginGlobals = [
|
||||||
...(config.globals ?? []),
|
|
||||||
buildSiteSettings({
|
buildSiteSettings({
|
||||||
additionalFields: options.siteSettingsFields,
|
additionalFields: options.siteSettingsFields,
|
||||||
...(options.pages ? { constructionFields: buildConstructionFields(options.pages.slug) } : {}),
|
...(options.pages ? { constructionFields: buildConstructionFields(options.pages.slug) } : {}),
|
||||||
@@ -241,6 +241,14 @@ export const ipalKit = (options: IpalOptions): Plugin => {
|
|||||||
buildCookieSettings(),
|
buildCookieSettings(),
|
||||||
buildNotifications(),
|
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]
|
config.endpoints = [...(config.endpoints ?? []), testEmailEndpoint]
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ export type TotpPluginFactory = (options: {
|
|||||||
forceSetup: boolean
|
forceSetup: boolean
|
||||||
totp: { issuer: string }
|
totp: { issuer: string }
|
||||||
}) => Plugin
|
}) => Plugin
|
||||||
|
import type { RevalidatePathFn } from './modules/hooks/buildRevalidateHook.js'
|
||||||
import type { AccessOption } from './modules/access/types.js'
|
import type { AccessOption } from './modules/access/types.js'
|
||||||
import type { ContentOption } from './modules/content/types.js'
|
import type { ContentOption } from './modules/content/types.js'
|
||||||
import type { FormsOption } from './modules/forms/types.js'
|
import type { FormsOption } from './modules/forms/types.js'
|
||||||
@@ -88,6 +89,15 @@ export type IpalOptions = {
|
|||||||
*/
|
*/
|
||||||
pages?: PagesOption
|
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)
|
* SEO — adds meta fields to chosen collections (via @payloadcms/plugin-seo)
|
||||||
* and enables locale-aware metadata helpers.
|
* and enables locale-aware metadata helpers.
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
import type { Config, GlobalConfig } from 'payload'
|
||||||
|
|
||||||
|
import { afterEach, describe, expect, test, vi } from 'vitest'
|
||||||
|
|
||||||
|
import { buildGlobalRevalidateHook } from '../src/modules/hooks/buildRevalidateHook.js'
|
||||||
|
import { ipalKit } from '../src/plugin.js'
|
||||||
|
|
||||||
|
const i18n = { defaultLocale: 'pl', locales: [{ code: 'pl', label: 'Polski' }] }
|
||||||
|
const run = (g: GlobalConfig | undefined, context: object = {}) =>
|
||||||
|
g?.hooks?.afterChange?.forEach((h) => h({ context, doc: {}, global: g, previousDoc: {}, req: {} } as never))
|
||||||
|
|
||||||
|
describe('globals revalidate the site layout on save', () => {
|
||||||
|
afterEach(() => vi.unstubAllEnvs())
|
||||||
|
|
||||||
|
test('ipalKit({ revalidatePath }) wires every plugin global', async () => {
|
||||||
|
vi.stubEnv('NEXT_RUNTIME', 'nodejs')
|
||||||
|
const revalidatePath = vi.fn()
|
||||||
|
const config = await ipalKit({ i18n, revalidatePath, twoFactor: false })({ collections: [], secret: 'x' } as unknown as Config)
|
||||||
|
for (const slug of ['site-settings', 'site-integrations', 'cookie-settings', 'notifications']) {
|
||||||
|
revalidatePath.mockClear()
|
||||||
|
run(config.globals?.find((g) => g.slug === slug))
|
||||||
|
expect(revalidatePath, slug).toHaveBeenCalledWith('/', 'layout')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
test('outside a Next server, or with disableRevalidate, nothing happens', () => {
|
||||||
|
const revalidatePath = vi.fn()
|
||||||
|
const hook = buildGlobalRevalidateHook({ revalidatePath })
|
||||||
|
hook({ context: {}, doc: {} } as never)
|
||||||
|
vi.stubEnv('NEXT_RUNTIME', 'nodejs')
|
||||||
|
hook({ context: { disableRevalidate: true }, doc: {} } as never)
|
||||||
|
expect(revalidatePath).not.toHaveBeenCalled()
|
||||||
|
})
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user