Revalidate Hook
Invalidate frontend cache when content changes
Deprecated. Prefer
autoModeand opt collections/globals in viacustom.revalidate. This page applies whenmode: 'legacyMode'is set.
Usage
import { revalidateHook } from '@adsign/payload-adsign-plugin';
export const Pages: CollectionConfig = {
hooks: {
afterOperation: [
revalidateHook({
collection: 'pages',
cacheTagFields: ['slug'],
}),
],
},
};Options
| Option | Type | Description |
|---|---|---|
collection | string | Collection slug (required) |
cacheTagFields | string[] | Fields to use as cache tags |
collections | string[] | Additional collections to revalidate |
globals | string[] | Globals to revalidate |
documents | Document[] | Specific documents to revalidate |
revalidateRelationships | boolean | object | Incoming fan-out — docs that reference this one (denylist) |
revalidateRelated | string[] | Outgoing fan-out — own relationship/upload field names to follow |
Examples
Revalidate with cache tag fields:
revalidateHook({
collection: 'articles',
cacheTagFields: ['slug'],
collections: ['articles'],
})Revalidate related documents (useful for media):
revalidateHook({
collection: 'media',
revalidateRelationships: true,
})Scope the incoming fan-out (denylist — everything is included unless set to false):
revalidateHook({
collection: 'media',
revalidateRelationships: {
collections: { products: false }, // fan out to all referencing collections except products
globals: { menu: false }, // ...and all referencing globals except menu
},
})Follow the outgoing fan-out by naming own relationship fields:
revalidateHook({
collection: 'campaigns',
revalidateRelated: ['author'], // follow `author`, but not the costly `products` list
})In autoMode you don't pass revalidateRelated by hand — the plugin auto-derives
it from every own relationship/upload field. Scope it there with the
revalidateRelated denylist on custom.revalidate,
e.g. revalidateRelated: { products: false } or false to disable it entirely.
Setup
- Configure in Payload:
adsignPlugin({
siteName: 'My Site',
domain: 'example.com',
revalidation: {
url: process.env.REVALIDATE_URL,
secret: process.env.REVALIDATE_SECRET,
},
})- Create API route in your frontend Next.js app:
import { revalidateTag } from 'next/cache';
import { NextRequest, NextResponse } from 'next/server';
export async function POST(request: NextRequest): Promise<Response> {
const { secret, tag } = await request.json();
if (secret !== process.env.REVALIDATE_SECRET) {
return NextResponse.json({ message: 'Invalid secret' }, { status: 401 });
}
revalidateTag(tag, 'max');
return NextResponse.json({ revalidated: true, now: Date.now() });
}