Declarative, type-safe cache invalidation across independent applications, using a framework-independent CloudEvents protocol and native Payload CMS, HTTP, and Next.js integrations.
Content, commerce, and indexing systems often change data cached by another application. This package lets producers describe those changes without knowing the consumer's cache keys or routes.
The producer knows what changed. The cache-owning application knows which cached data depends on it. A shared event protocol connects them without coupling either application to the other's framework or domain model.
Use Next.js native invalidation directly when the write and cache live in one application. Use this package when a separate service needs to notify the cache owner.
- Framework-independent CloudEvents 1.0 contracts and optional Resource Change V1 snapshots.
- Payload collection and global producers with localized public-state snapshots.
- HTTP delivery with Standard Webhooks signing, named destinations, bounded retries, and observable outcomes.
- Typed recursive cache tags and paths with exact event-type rules.
- Native Next.js profiles, schema-validated custom events, and programmable rules.
- Optional Payload and Next.js peer dependencies.
bun add @klnap/cache-invalidationnpm install @klnap/cache-invalidationInstall Payload or Next.js only in applications using those integrations. Importing the package root does not load either framework.
Define a tag and map the event type that changes it:
import 'server-only'
import { defineCache } from '@klnap/cache-invalidation/consumers/next'
export const cache = defineCache({
tags: {
article: 'article:[id]',
},
rules: ({ tags }) => ({
'payload.collection.articles': [tags.article],
}),
})Attach the tag where cached data is produced:
import { cacheTag } from 'next/cache'
import { cache } from '@/cache'
export async function getArticle(id: string) {
'use cache'
cacheTag(cache.tags.article({ id }))
return loadArticle(id)
}Next.js 16 requires cacheComponents: true in next.config.ts for use cache and cacheTag().
import { cacheInvalidation } from '@klnap/cache-invalidation/producers/payload'
export default buildConfig({
plugins: [
cacheInvalidation({
source: 'https://cms.example.com',
collections: { articles: true },
dispatcher: {
targets: {
website: {
url: process.env.CACHE_INVALIDATION_URL!,
secret: process.env.CACHE_INVALIDATION_SECRET!,
},
},
},
}),
],
})Resources are opt-in. Collection and global configuration can also be declared locally with custom.cacheInvalidation. See the Payload guide.
Bind the reusable definition to a signed endpoint:
// app/api/invalidate/route.ts
import { cacheInvalidationHandler } from '@klnap/cache-invalidation/consumers/next'
import { cache } from '@/cache'
export const POST = cacheInvalidationHandler({
config: cache,
secret: process.env.CACHE_INVALIDATION_SECRET!,
})Credentials and HTTP limits belong to the handler. defineCache() has no authentication or network side effects.
Use nested objects to organize tags, paths, and event rules. Their keys do not change rendered tag values or event matching:
defineCache({
tags: {
articles: {
list: 'articles:[locale]',
detail: 'article:[id]',
},
},
rules: ({ tags }) => ({
payload: {
content: {
'payload.collection.articles': [tags.articles.detail],
},
},
}),
})The only matched key is payload.collection.articles. See cache tags and paths and the Next.js guide for path patterns, profile groups, rule() descriptors, and programmable rules.
Custom producers can emit CloudEvents through the root contract. Custom dispatchers implement Dispatcher. Custom consumers can use the protocol parser and Standard Schema validator. See the custom producer, custom dispatcher, and custom consumer guides.
defineCache() separates config, tags, paths, and rules. Tag profiles use config.tags.profile, with { expire: 0 } as this library's default. Local rule groups use [profile, [tagReferences...]]; paths do not accept profiles.
Concrete path templates interpolate typed parameters. Native [routePattern, 'page' | 'layout'] declarations keep the route pattern literal. Rules match exact CloudEvents type strings.
cacheLife() configures a cache's lifetime. revalidateTag(tag, profile) requests on-demand invalidation; its native named profile values retain their Next.js meaning.
- Node.js
^22.23.5 || ^24.22.3 || >=26.13.1. - Next.js
>=16.4.5 <17for the Next.js consumer. - Payload
>=3.94.1 <4for the Payload producer.
Payload hooks ordinarily run before the database transaction commits. HTTP delivery is best effort: retries may duplicate events, and process failure can lose them. This package does not provide durable delivery, exactly-once processing, or atomic invalidation. See reliability and security.
The renamed package starts a separate version history at 0.0.0. The initial bootstrap uses npm Staged Publishing; normal Changesets releases publish directly from GitHub Actions through npm Trusted Publishing (OIDC), with provenance and automatic GitHub release metadata. See the migration and release guide.