Payload Adsign Plugin

Revalidate Hook

Invalidate frontend cache when content changes

Deprecated. Prefer autoMode and opt collections/globals in via custom.revalidate. This page applies when mode: 'legacyMode' is set.

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 | objectIncoming fan-out — docs that reference this one (denylist)
revalidateRelatedstring[]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

  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