Payload Adsign Plugin

Revalidate Hook

Invalidate frontend cache when content changes

Usage

collections/Pages/index.ts
import { revalidateHook } from '@adsign/payload-adsign-plugin';

export const Pages: CollectionConfig = {
  hooks: {
    afterOperation: [
      revalidateHook({
        collection: 'pages',
        cacheTagFields: ['slug'],
      }),
    ],
  },
};

Options

OptionTypeDescription
collectionstringCollection slug (required)
cacheTagFieldsstring[]Fields to use as cache tags
collectionsstring[]Additional collections to revalidate
globalsstring[]Globals to revalidate
documentsDocument[]Specific documents to revalidate
revalidateRelationshipsboolean | objectRevalidate related documents

Examples

Revalidate with cache tag fields:

revalidateHook({
  collection: 'articles',
  cacheTagFields: ['slug'],
  collections: ['articles'],
})

Revalidate related documents (useful for media):

revalidateHook({
  collection: 'media',
  revalidateRelationships: true,
})

Revalidate specific collections:

revalidateHook({
  collection: 'media',
  revalidateRelationships: {
    collections: {
      articles: true,
      pages: true,
    },
  },
})

Setup

  1. Configure in Payload:
payload.config.ts
adsignPlugin({
  siteName: 'My Site',
  domain: 'example.com',
  revalidation: {
    url: process.env.REVALIDATE_URL,
    secret: process.env.REVALIDATE_SECRET,
  },
})
  1. Create API route in your frontend Next.js app:
app/api/revalidate/route.ts
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() });
}

On this page