@klnap/cache-invalidation 0.0.0-stage → 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +139 -0
- package/LICENSE +21 -0
- package/README.md +176 -2
- package/dist/consumers/next/compile-tag-template.d.ts +16 -0
- package/dist/consumers/next/compile-tag-template.js +83 -0
- package/dist/consumers/next/consumer-types.d.ts +98 -0
- package/dist/consumers/next/consumer-types.js +3 -0
- package/dist/consumers/next/define-cache.d.ts +4 -0
- package/dist/consumers/next/define-cache.js +281 -0
- package/dist/consumers/next/host-types.d.ts +44 -0
- package/dist/consumers/next/host-types.js +3 -0
- package/dist/consumers/next/index.d.ts +5 -0
- package/dist/consumers/next/index.js +2 -0
- package/dist/consumers/next/param-values.d.ts +8 -0
- package/dist/consumers/next/param-values.js +23 -0
- package/dist/consumers/next/profile.d.ts +2 -0
- package/dist/consumers/next/profile.js +18 -0
- package/dist/consumers/next/registry-builders.d.ts +3 -0
- package/dist/consumers/next/registry-builders.js +155 -0
- package/dist/consumers/next/revalidation-handler.d.ts +6 -0
- package/dist/consumers/next/revalidation-handler.js +2 -0
- package/dist/consumers/next/runtime.d.ts +5 -0
- package/dist/consumers/next/runtime.js +560 -0
- package/dist/core/cloudevents.d.ts +27 -0
- package/dist/core/cloudevents.js +172 -0
- package/dist/core/dispatch-outcome.d.ts +4 -0
- package/dist/core/dispatch-outcome.js +17 -0
- package/dist/core/errors.d.ts +16 -0
- package/dist/core/errors.js +28 -0
- package/dist/core/event-limits.d.ts +15 -0
- package/dist/core/event-limits.js +20 -0
- package/dist/core/event-selector.d.ts +9 -0
- package/dist/core/event-selector.js +5 -0
- package/dist/core/index.d.ts +10 -0
- package/dist/core/index.js +6 -0
- package/dist/core/resource-changed-schema.d.ts +35 -0
- package/dist/core/resource-changed-schema.js +12 -0
- package/dist/core/standard-schema.d.ts +2 -0
- package/dist/core/standard-schema.js +7 -0
- package/dist/core/types.d.ts +35 -0
- package/dist/core/types.js +1 -0
- package/dist/core/url-segment.d.ts +2 -0
- package/dist/core/url-segment.js +17 -0
- package/dist/dispatchers/http/index.d.ts +20 -0
- package/dist/dispatchers/http/index.js +274 -0
- package/dist/dispatchers/http/targets.d.ts +14 -0
- package/dist/dispatchers/http/targets.js +26 -0
- package/dist/dispatchers/index.d.ts +1 -0
- package/dist/dispatchers/index.js +1 -0
- package/dist/producers/payload/exclude-fields.d.ts +19 -0
- package/dist/producers/payload/exclude-fields.js +75 -0
- package/dist/producers/payload/index.d.ts +4 -0
- package/dist/producers/payload/index.js +3 -0
- package/dist/producers/payload/lifecycle/deferred.d.ts +55 -0
- package/dist/producers/payload/lifecycle/deferred.js +111 -0
- package/dist/producers/payload/lifecycle/dispatch.d.ts +18 -0
- package/dist/producers/payload/lifecycle/dispatch.js +21 -0
- package/dist/producers/payload/lifecycle/doc-content.d.ts +8 -0
- package/dist/producers/payload/lifecycle/doc-content.js +88 -0
- package/dist/producers/payload/lifecycle/draft-status.d.ts +6 -0
- package/dist/producers/payload/lifecycle/draft-status.js +15 -0
- package/dist/producers/payload/lifecycle/helpers.d.ts +13 -0
- package/dist/producers/payload/lifecycle/helpers.js +12 -0
- package/dist/producers/payload/lifecycle/identifier-field.d.ts +8 -0
- package/dist/producers/payload/lifecycle/identifier-field.js +48 -0
- package/dist/producers/payload/lifecycle/identifiers.d.ts +4 -0
- package/dist/producers/payload/lifecycle/identifiers.js +5 -0
- package/dist/producers/payload/lifecycle/locale-published.d.ts +7 -0
- package/dist/producers/payload/lifecycle/locale-published.js +19 -0
- package/dist/producers/payload/lifecycle/locale.d.ts +24 -0
- package/dist/producers/payload/lifecycle/locale.js +44 -0
- package/dist/producers/payload/lifecycle/log.d.ts +31 -0
- package/dist/producers/payload/lifecycle/log.js +67 -0
- package/dist/producers/payload/lifecycle/media-equality.d.ts +4 -0
- package/dist/producers/payload/lifecycle/media-equality.js +36 -0
- package/dist/producers/payload/lifecycle/operation.d.ts +2 -0
- package/dist/producers/payload/lifecycle/operation.js +7 -0
- package/dist/producers/payload/lifecycle/orderable-hooks.d.ts +21 -0
- package/dist/producers/payload/lifecycle/orderable-hooks.js +26 -0
- package/dist/producers/payload/lifecycle/orderable-neighbors.d.ts +21 -0
- package/dist/producers/payload/lifecycle/orderable-neighbors.js +151 -0
- package/dist/producers/payload/lifecycle/public-snapshots.d.ts +16 -0
- package/dist/producers/payload/lifecycle/public-snapshots.js +173 -0
- package/dist/producers/payload/lifecycle/revalidate.d.ts +16 -0
- package/dist/producers/payload/lifecycle/revalidate.js +162 -0
- package/dist/producers/payload/lifecycle/skip-rules.d.ts +19 -0
- package/dist/producers/payload/lifecycle/skip-rules.js +57 -0
- package/dist/producers/payload/lifecycle/types.d.ts +32 -0
- package/dist/producers/payload/lifecycle/types.js +1 -0
- package/dist/producers/payload/media/media-references.d.ts +31 -0
- package/dist/producers/payload/media/media-references.js +127 -0
- package/dist/producers/payload/media/resolve-media-affected-locales.d.ts +22 -0
- package/dist/producers/payload/media/resolve-media-affected-locales.js +70 -0
- package/dist/producers/payload/media/resolve-media-dependents.d.ts +39 -0
- package/dist/producers/payload/media/resolve-media-dependents.js +157 -0
- package/dist/producers/payload/media/resolve-media-slug.d.ts +7 -0
- package/dist/producers/payload/media/resolve-media-slug.js +17 -0
- package/dist/producers/payload/media/revalidate-media-dependents.d.ts +14 -0
- package/dist/producers/payload/media/revalidate-media-dependents.js +222 -0
- package/dist/producers/payload/plugin.d.ts +36 -0
- package/dist/producers/payload/plugin.js +235 -0
- package/dist/producers/payload/publish-all/context.d.ts +2 -0
- package/dist/producers/payload/publish-all/context.js +2 -0
- package/dist/producers/payload/publish-all/edited-locale.d.ts +5 -0
- package/dist/producers/payload/publish-all/edited-locale.js +5 -0
- package/dist/producers/payload/publish-all/index.d.ts +3 -0
- package/dist/producers/payload/publish-all/index.js +3 -0
- package/dist/producers/payload/publish-all/localization.d.ts +7 -0
- package/dist/producers/payload/publish-all/localization.js +1 -0
- package/dist/producers/payload/publish-all/mirror-publish-all-locales-to-req.d.ts +19 -0
- package/dist/producers/payload/publish-all/mirror-publish-all-locales-to-req.js +32 -0
- package/dist/producers/payload/publish-all/preserve-locales-on-publish-all.d.ts +2 -0
- package/dist/producers/payload/publish-all/preserve-locales-on-publish-all.js +145 -0
- package/dist/producers/payload/publish-all/read-localized-scalar.d.ts +2 -0
- package/dist/producers/payload/publish-all/read-localized-scalar.js +24 -0
- package/dist/producers/payload/publish-all/request-flags.d.ts +13 -0
- package/dist/producers/payload/publish-all/request-flags.js +9 -0
- package/dist/producers/payload/publish-all/restore-guard.d.ts +2 -0
- package/dist/producers/payload/publish-all/restore-guard.js +12 -0
- package/dist/producers/payload/publish-all/restore-locales-after-publish-all.d.ts +2 -0
- package/dist/producers/payload/publish-all/restore-locales-after-publish-all.js +85 -0
- package/dist/producers/payload/revalidation-custom.d.ts +37 -0
- package/dist/producers/payload/revalidation-custom.js +54 -0
- package/dist/producers/payload/schema.d.ts +3 -0
- package/dist/producers/payload/schema.js +23 -0
- package/docs/concepts/architecture.md +32 -0
- package/docs/concepts/cache-tags.md +97 -0
- package/docs/concepts/event-contract.md +38 -0
- package/docs/consumers/custom.md +19 -0
- package/docs/consumers/next.md +277 -0
- package/docs/dispatchers/custom.md +38 -0
- package/docs/dispatchers/http.md +66 -0
- package/docs/extending.md +26 -0
- package/docs/migration/0.3.0-to-0.4.0.md +67 -0
- package/docs/migration/0.4.0-to-0.5.0.md +60 -0
- package/docs/migration/0.5.x-to-0.0.0.md +143 -0
- package/docs/producers/custom.md +63 -0
- package/docs/producers/payload-media-dependencies.md +45 -0
- package/docs/producers/payload.md +140 -0
- package/docs/reliability.md +35 -0
- package/docs/security.md +34 -0
- package/docs/troubleshooting.md +14 -0
- package/package.json +120 -4
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# Custom dispatchers
|
|
2
|
+
|
|
3
|
+
A dispatcher delivers CloudEvents and returns a `DispatchOutcome`. Implement the public
|
|
4
|
+
`Dispatcher` contract when the built-in HTTP dispatcher does not fit your transport or delivery
|
|
5
|
+
policy:
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import type { CloudEvent, Dispatcher } from '@klnap/cache-invalidation'
|
|
9
|
+
|
|
10
|
+
declare function publish(events: readonly CloudEvent[]): Promise<void>
|
|
11
|
+
|
|
12
|
+
export const dispatcher: Dispatcher = {
|
|
13
|
+
async dispatch({ events }) {
|
|
14
|
+
await publish(events)
|
|
15
|
+
return { status: 'accepted', results: [] }
|
|
16
|
+
},
|
|
17
|
+
}
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Pass the dispatcher to the Payload producer:
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
import { cacheInvalidation } from '@klnap/cache-invalidation/producers/payload'
|
|
24
|
+
import { dispatcher } from './dispatcher.js'
|
|
25
|
+
|
|
26
|
+
export const cacheInvalidationPlugin = cacheInvalidation({
|
|
27
|
+
source: 'https://cms.example.com',
|
|
28
|
+
dispatcher,
|
|
29
|
+
})
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
The dispatcher receives validated `CloudEvent` values. Return a
|
|
33
|
+
`DispatchOutcome` that reflects what the transport actually knows; use `unknown` when it cannot
|
|
34
|
+
confirm delivery. The built-in HTTP dispatcher's retry, signing, filtering, and deadline behavior
|
|
35
|
+
does not apply to custom implementations.
|
|
36
|
+
|
|
37
|
+
Batching stays inside each dispatcher. See the [event and dispatcher contracts](../concepts/event-contract.md) and the
|
|
38
|
+
[HTTP dispatcher guide](./http.md) for the built-in implementation.
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# HTTP dispatcher
|
|
2
|
+
|
|
3
|
+
The HTTP dispatcher sends CloudEvents to named destinations. It signs every request with Standard
|
|
4
|
+
Webhooks.
|
|
5
|
+
|
|
6
|
+
```ts
|
|
7
|
+
import { http } from '@klnap/cache-invalidation/dispatchers/http'
|
|
8
|
+
|
|
9
|
+
const dispatcher = http({
|
|
10
|
+
targets: {
|
|
11
|
+
website: {
|
|
12
|
+
url: process.env.CACHE_INVALIDATION_URL!,
|
|
13
|
+
secret: process.env.REVALIDATION_SECRET!,
|
|
14
|
+
filter: { source: 'https://cms.example/payload' },
|
|
15
|
+
},
|
|
16
|
+
preview: {
|
|
17
|
+
url: process.env.PREVIEW_CACHE_INVALIDATION_URL!,
|
|
18
|
+
secret: process.env.PREVIEW_REVALIDATION_SECRET!,
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
timeoutMs: 15_000,
|
|
22
|
+
deadlineMs: 5_000,
|
|
23
|
+
retries: 2,
|
|
24
|
+
retryDelayMs: 200,
|
|
25
|
+
maxEventItems: 100,
|
|
26
|
+
maxBodyBytes: 256 * 1024,
|
|
27
|
+
})
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
The `targets` record names delivery destinations. Target filters run before chunking. A manual
|
|
31
|
+
`targets` selection narrows the configured set; it does not bypass filters. Without an explicit
|
|
32
|
+
selection, matching events fan out to every configured target.
|
|
33
|
+
|
|
34
|
+
## Request format
|
|
35
|
+
|
|
36
|
+
A single event uses `application/cloudevents+json`. A batch uses
|
|
37
|
+
`application/cloudevents-batch+json`. Every request has `webhook-id`, `webhook-timestamp`, and
|
|
38
|
+
`webhook-signature` headers. Retries reuse the body and delivery ID, and create a fresh timestamp
|
|
39
|
+
and signature.
|
|
40
|
+
|
|
41
|
+
## Retries and outcomes
|
|
42
|
+
|
|
43
|
+
- `2xx`: accepted.
|
|
44
|
+
- `429` and `5xx`: retryable.
|
|
45
|
+
- Other `4xx` and redirects: terminal.
|
|
46
|
+
- Network errors and timeouts: retryable.
|
|
47
|
+
|
|
48
|
+
`retries` counts extra attempts. Backoff uses a random delay up to twice the previous bound.
|
|
49
|
+
`deadlineMs` bounds all targets, chunks, and retries in one dispatch. Deliveries run with bounded
|
|
50
|
+
concurrency. The default `timeoutMs` is 15 seconds per attempt, while the total dispatch deadline
|
|
51
|
+
defaults to 5 seconds. The shorter total deadline can abort an attempt before its per-attempt timeout;
|
|
52
|
+
these are nested bounds, not independent time guarantees.
|
|
53
|
+
|
|
54
|
+
`maxEventItems` bounds events per request. `maxBodyBytes` bounds encoded request bytes. The
|
|
55
|
+
dispatcher splits batches to satisfy both limits and reports an individually oversized event as a
|
|
56
|
+
failed target result without sending it.
|
|
57
|
+
|
|
58
|
+
## Transport security
|
|
59
|
+
|
|
60
|
+
Targets must use HTTPS unless `allowInsecure: true` or the URL uses loopback (`localhost`,
|
|
61
|
+
`127.0.0.1`, or `[::1]`). Do not enable insecure transport for a non-loopback host in production.
|
|
62
|
+
Target URLs cannot contain user-info credentials. Secrets must use the Standard Webhooks `whsec_`
|
|
63
|
+
encoding.
|
|
64
|
+
|
|
65
|
+
There is no persistent queue. Delivery failure is reported to the producer and is not recovered
|
|
66
|
+
after process exit. See [reliability](../reliability.md).
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# Extending revalidation
|
|
2
|
+
|
|
3
|
+
The package exposes framework-independent CloudEvents contracts and public integration
|
|
4
|
+
boundaries. Built-in integrations cover Payload event production, HTTP delivery, and Next.js
|
|
5
|
+
cache invalidation. Other integrations remain application-owned.
|
|
6
|
+
|
|
7
|
+
## Extension points
|
|
8
|
+
|
|
9
|
+
- [Custom producers](producers/custom.md) emit arbitrary CloudEvents. Resource Change V1 is optional.
|
|
10
|
+
- [Custom dispatchers](dispatchers/custom.md) implement the public `Dispatcher` contract.
|
|
11
|
+
- [Custom consumers](consumers/custom.md) own transport security, validation, planning, and effects.
|
|
12
|
+
- [Next.js rules](consumers/next.md) map event types to cache operations and can validate custom event schemas.
|
|
13
|
+
|
|
14
|
+
## Application-owned indexing
|
|
15
|
+
|
|
16
|
+
For commerce or search, the application owns projection, indexing, completion, revisions, and
|
|
17
|
+
idempotency. Emit an event after the public projection is ready. Do not invalidate frontend data
|
|
18
|
+
when a source system only queued indexing work. See [custom schemas and rules](consumers/next.md#custom-schemas-and-programmable-rules).
|
|
19
|
+
|
|
20
|
+
Custom execution is awaited in the request, uses bounded cooperative cancellation, and is not a
|
|
21
|
+
durable job system. See [reliability](reliability.md).
|
|
22
|
+
|
|
23
|
+
## Payload orderable hooks
|
|
24
|
+
|
|
25
|
+
Use the exported `cacheInvalidationOrderableHooks` integration when its transaction callback contract
|
|
26
|
+
applies. Ordinary Payload hooks do not gain post-commit guarantees. See [Payload](producers/payload.md).
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# Migrate from `0.3.0`
|
|
2
|
+
|
|
3
|
+
Version 0.4.0 is a breaking protocol and configuration change. Do not run old and new producers against
|
|
4
|
+
the same endpoint during a rolling migration: the consumer accepts CloudEvents and Standard
|
|
5
|
+
Webhooks, not the `0.3.0` bearer-token envelope.
|
|
6
|
+
|
|
7
|
+
## HTTP destination
|
|
8
|
+
|
|
9
|
+
Replace the unnamed `target` or target array with a named `targets` record. Use a Standard Webhooks
|
|
10
|
+
secret (`whsec_` plus Base64-encoded key material) instead of a bearer token:
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
http({
|
|
14
|
+
targets: {
|
|
15
|
+
website: {
|
|
16
|
+
url: process.env.REVALIDATION_URL!,
|
|
17
|
+
secret: process.env.REVALIDATION_SECRET!,
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
})
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
By default, every event goes to every configured destination. Add a destination filter only when
|
|
24
|
+
that endpoint should receive a subset. Manual target selection narrows the configured targets and
|
|
25
|
+
does not bypass filters.
|
|
26
|
+
|
|
27
|
+
## Event envelope
|
|
28
|
+
|
|
29
|
+
Replace the package-specific `{ source, data }` envelope with CloudEvents 1.0. A single event uses
|
|
30
|
+
`application/cloudevents+json`; a batch uses `application/cloudevents-batch+json`. The old `source:
|
|
31
|
+
"payload"` value becomes an absolute CloudEvents source URI, and the old item data becomes the
|
|
32
|
+
typed CloudEvent `data`.
|
|
33
|
+
|
|
34
|
+
Update custom producers to emit `id`, `source`, `specversion`, and `type`. Keep one stable CloudEvent
|
|
35
|
+
ID when retrying the same event. Add an application schema that implements Standard Schema V1 for
|
|
36
|
+
each custom event source.
|
|
37
|
+
|
|
38
|
+
## Cache declarations and rules
|
|
39
|
+
|
|
40
|
+
Remove `defineRevalidationTags()` and the `@klnap/revalidation/consumers/next/tags` import. Move
|
|
41
|
+
the existing `tags` and `paths` declarations into the single `defineRevalidation()` call. Keep
|
|
42
|
+
`tags` and `paths` separate; do not convert them to delivery destinations.
|
|
43
|
+
|
|
44
|
+
Replace key-based event resolution with rules over CloudEvent source, type, operation, root resource,
|
|
45
|
+
and affected resources. Use object mappings for exact affected resource type/name matches. Use
|
|
46
|
+
`on()` selectors for source, operation, root-resource, or other compound conditions. All matching
|
|
47
|
+
rules execute; duplicate tag/path operations are removed.
|
|
48
|
+
|
|
49
|
+
The default cache mode remains immediate expiry. `stale` maps to Next.js's `"max"` profile. Literal
|
|
50
|
+
paths omit a type; dynamic route patterns default to `page`. Use `layout` where required.
|
|
51
|
+
|
|
52
|
+
## Custom dispatchers and consumers
|
|
53
|
+
|
|
54
|
+
Update custom dispatchers to accept CloudEvents and optional explicit named target selection.
|
|
55
|
+
Return one result per target chunk and support the `partial` aggregate status. A dispatcher must not
|
|
56
|
+
select destinations based on consumer invalidation rules.
|
|
57
|
+
|
|
58
|
+
Custom consumers must verify transport authentication before parsing, validate CloudEvents
|
|
59
|
+
envelopes, validate `data` with Standard Schema V1, and use the parsed output. Make cache effects
|
|
60
|
+
idempotent because retries can redeliver a request.
|
|
61
|
+
|
|
62
|
+
## Payload transaction behavior
|
|
63
|
+
|
|
64
|
+
Payload hooks still run before commit. The built-in plugin remains best-effort and fail-open; a
|
|
65
|
+
scheduled delay does not guarantee commit ordering. Preserve the orderable integration where the
|
|
66
|
+
host invokes its `onCommitted` or `onRolledBack` callback. Use an application-managed transactional
|
|
67
|
+
outbox when committed, durable delivery is required.
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# Migrate from `0.4.0`
|
|
2
|
+
|
|
3
|
+
The 0.5.0 candidate separates CloudEvents source identity from event-data validation. Update
|
|
4
|
+
producer and consumer configuration together.
|
|
5
|
+
|
|
6
|
+
## Payload producer
|
|
7
|
+
|
|
8
|
+
Set a stable absolute URI on every Payload plugin:
|
|
9
|
+
|
|
10
|
+
```ts
|
|
11
|
+
revalidation({
|
|
12
|
+
source: 'https://cms.example/payload',
|
|
13
|
+
dispatcher,
|
|
14
|
+
})
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
The former implicit `urn:klnap:revalidation:payload` source and `payloadSource` export are removed.
|
|
18
|
+
Keep the configured URI stable across retries and deployments that share one producer identity.
|
|
19
|
+
|
|
20
|
+
## Next.js consumer
|
|
21
|
+
|
|
22
|
+
Replace `mode: 'expire' | 'stale'` with native profiles. Use `profile: 'max'` for stale-while-
|
|
23
|
+
revalidate behavior, or `[tag, 'max']` to override one tag. The default remains immediate expiry
|
|
24
|
+
through `{ expire: 0 }`. Replace tag/path `{ value, mode/type }` descriptors with tuples:
|
|
25
|
+
`[tag, profile]` and `[path, 'page' | 'layout']`.
|
|
26
|
+
|
|
27
|
+
The built-in resource-change schema is registered automatically. Remove its `sources` entry and
|
|
28
|
+
select it by its event type:
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
defineRevalidation({
|
|
32
|
+
secret: process.env.REVALIDATION_SECRET!,
|
|
33
|
+
tags: { pages: '[locale]:pages' },
|
|
34
|
+
rules: ({ tags }) => ({
|
|
35
|
+
'payload.collection.pages': [tags.pages],
|
|
36
|
+
}),
|
|
37
|
+
})
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Custom schemas move from source-keyed `sources` to event-type-keyed `schemas`:
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
defineRevalidation({
|
|
44
|
+
secret: process.env.REVALIDATION_SECRET!,
|
|
45
|
+
schemas: { 'com.example.pricing.changed.v1': pricingSchema },
|
|
46
|
+
allowedSources: ['https://billing.example/events'],
|
|
47
|
+
rules: ({ on }) => [
|
|
48
|
+
on({ type: 'com.example.pricing.changed.v1' }, ({ data }) => resolvePricing(data)),
|
|
49
|
+
],
|
|
50
|
+
})
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
The built-in schema cannot be replaced. `allowedSources` is optional and matches source URIs exactly.
|
|
54
|
+
Use source selectors when rule behavior depends on producer identity; they no longer select schemas.
|
|
55
|
+
|
|
56
|
+
## Event processing
|
|
57
|
+
|
|
58
|
+
The consumer validates every event in a request before running any rule or side effect. Duplicate
|
|
59
|
+
`(source, id)` identities in one request execute once. Equal IDs from different sources execute
|
|
60
|
+
independently.
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
# Migrate from `@klnap/revalidation` 0.5.x to `@klnap/cache-invalidation`
|
|
2
|
+
|
|
3
|
+
This migration covers two separate changes: the package rename starts a new version history at `0.0.0`, while the redesigned API introduces breaking source changes. The first normal feature release of the new package is planned as `0.1.0`; do not interpret old package versions as releases of the renamed package. The CloudEvents wire contract introduced by the redesign remains stable through the rename.
|
|
4
|
+
|
|
5
|
+
## Rename the package
|
|
6
|
+
|
|
7
|
+
Replace the dependency:
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
bun remove @klnap/revalidation
|
|
11
|
+
bun add @klnap/cache-invalidation
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Or use `npm uninstall @klnap/revalidation && npm install @klnap/cache-invalidation`. Update package imports to `@klnap/cache-invalidation` and its existing role subpaths.
|
|
15
|
+
|
|
16
|
+
The following protocol values do not change because of the rename:
|
|
17
|
+
|
|
18
|
+
- CloudEvents types such as `payload.collection.articles`.
|
|
19
|
+
- Configured `source` URIs and generated `rev_` event IDs.
|
|
20
|
+
- Resource Change V1 `dataschema`: `urn:klnap:revalidation:resource-change:v1`.
|
|
21
|
+
- Standard Webhooks authentication and delivery semantics.
|
|
22
|
+
|
|
23
|
+
## Rename the Payload producer and local key
|
|
24
|
+
|
|
25
|
+
Replace `revalidation()` with `cacheInvalidation()` and `custom.revalidation` with `custom.cacheInvalidation`:
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import { cacheInvalidation } from '@klnap/cache-invalidation/producers/payload'
|
|
29
|
+
|
|
30
|
+
export default buildConfig({
|
|
31
|
+
plugins: [
|
|
32
|
+
cacheInvalidation({
|
|
33
|
+
source: 'https://cms.example.com',
|
|
34
|
+
collections: { articles: true },
|
|
35
|
+
dispatcher: {
|
|
36
|
+
targets: {
|
|
37
|
+
website: {
|
|
38
|
+
url: process.env.CACHE_INVALIDATION_URL!,
|
|
39
|
+
secret: process.env.CACHE_INVALIDATION_SECRET!,
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
}),
|
|
44
|
+
],
|
|
45
|
+
})
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Central `collections` and `globals` and local `custom.cacheInvalidation` use the same options. Local `false` disables a central entry, local `true` enables it, and local options override supplied fields while inheriting others. `identifier` extraction options are removed; use snapshot values in typed cache templates. `projection` controls transmitted data; `filter` controls whether a public state change emits an event.
|
|
49
|
+
|
|
50
|
+
## Separate the cache definition from the handler
|
|
51
|
+
|
|
52
|
+
Replace the old `defineRevalidation()` result with a reusable `defineCache()` value. Replace `revalidationHandler()` with `cacheInvalidationHandler()`:
|
|
53
|
+
|
|
54
|
+
```ts
|
|
55
|
+
// src/cache.ts
|
|
56
|
+
import 'server-only'
|
|
57
|
+
import { defineCache } from '@klnap/cache-invalidation/consumers/next'
|
|
58
|
+
|
|
59
|
+
export const cache = defineCache({
|
|
60
|
+
tags: { article: 'article:[id]' },
|
|
61
|
+
rules: ({ tags }) => ({
|
|
62
|
+
'payload.collection.articles': [tags.article],
|
|
63
|
+
}),
|
|
64
|
+
})
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
// app/api/invalidate/route.ts
|
|
69
|
+
import { cacheInvalidationHandler } from '@klnap/cache-invalidation/consumers/next'
|
|
70
|
+
import { cache } from '@/cache'
|
|
71
|
+
|
|
72
|
+
export const POST = cacheInvalidationHandler({
|
|
73
|
+
config: cache,
|
|
74
|
+
secret: process.env.CACHE_INVALIDATION_SECRET!,
|
|
75
|
+
})
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Credentials, source policy, request limits, and logging belong to the handler boundary. `defineCache()` does not require a secret or create a route.
|
|
79
|
+
|
|
80
|
+
## Move declarations into nested trees
|
|
81
|
+
|
|
82
|
+
Nested keys organize helper autocomplete; they do not add prefixes or affect event matching:
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
tags: {
|
|
86
|
+
articles: {
|
|
87
|
+
list: 'articles:[locale]',
|
|
88
|
+
detail: 'article:[id]',
|
|
89
|
+
route: 'article-route:[locale]:[slug]',
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
paths: {
|
|
93
|
+
articles: {
|
|
94
|
+
detail: '/[locale]/articles/[slug]',
|
|
95
|
+
all: ['/[locale]/articles/[slug]', 'page'],
|
|
96
|
+
},
|
|
97
|
+
},
|
|
98
|
+
rules: ({ tags, paths }) => ({
|
|
99
|
+
payload: {
|
|
100
|
+
content: {
|
|
101
|
+
'payload.collection.articles': [tags.articles.detail, paths.articles.detail],
|
|
102
|
+
},
|
|
103
|
+
},
|
|
104
|
+
}),
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Only exact leaf event keys match. Group names are ignored for matching. Duplicate leaf types fail configuration. Use `rule({ tags, paths })` when a descriptor object needs to be distinguished from an organizational group.
|
|
108
|
+
|
|
109
|
+
## Configure native tag profiles
|
|
110
|
+
|
|
111
|
+
Remove the old top-level `profile`, per-rule `profile`, and individual `[tag, profile]` tuple forms. Set the default at `config.tags.profile`; use `[profile, [tagReferences...]]` for overrides:
|
|
112
|
+
|
|
113
|
+
```ts
|
|
114
|
+
defineCache({
|
|
115
|
+
config: { tags: { profile: { expire: 0 } } },
|
|
116
|
+
tags: {
|
|
117
|
+
articles: { detail: 'article:[id]', list: 'articles:[locale]' },
|
|
118
|
+
categories: { list: 'categories:[locale]' },
|
|
119
|
+
},
|
|
120
|
+
rules: ({ tags }) => ({
|
|
121
|
+
'payload.collection.articles': [
|
|
122
|
+
tags.articles.detail,
|
|
123
|
+
['max', [tags.articles.list, tags.categories.list]],
|
|
124
|
+
],
|
|
125
|
+
}),
|
|
126
|
+
})
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
The default remains `{ expire: 0 }`. Native profile names pass through unchanged. Paths use native route tuples and do not accept profiles.
|
|
130
|
+
|
|
131
|
+
## Keep the event contract changes
|
|
132
|
+
|
|
133
|
+
The previous redesign changed Payload events to exact types such as `payload.collection.articles`, with operation in event data. Resource Change V1 data uses independent `before` and `after` snapshots and optional `{ type, data }` affected records. It remains optional for custom producers. Review the [event contract](../concepts/event-contract.md), [Payload guide](../producers/payload.md), and [Next.js guide](../consumers/next.md).
|
|
134
|
+
|
|
135
|
+
The root no longer exports `resourceChangedEventType` or `resourceChangedSchema`. It exports `ResourceChangeV1`, `resourceChangeV1DataSchema`, and `resourceChangeV1DataSchemaUri`.
|
|
136
|
+
|
|
137
|
+
## New package release lifecycle
|
|
138
|
+
|
|
139
|
+
The first artifact is `@klnap/cache-invalidation@0.0.0`, submitted with npm Staged Publishing. npm may create its own public `0.0.0-stage` placeholder while the actual artifact remains private to the stage. A maintainer reviews the stage and approves it with npm two-factor authentication. Approval belongs to the maintainer; release automation only stages artifacts and must never call `npm publish` or `npm stage approve`.
|
|
140
|
+
|
|
141
|
+
After the initial `0.0.0` package is approved and visible in the registry, the first normal Changesets feature release is `0.1.0`. Later versions use Changesets. Configure npm Trusted Publishing for the new package, GitHub repository `klnap/cache-invalidation`, workflow `release.yml`, and environment `npm`, allowing `npm stage publish` only. Trust for the former package does not apply to this package. An E404 lookup does not prove that a package name is claimable.
|
|
142
|
+
|
|
143
|
+
The old package and its published versions remain unchanged. Do not recreate their history under the new name.
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# Custom producers
|
|
2
|
+
|
|
3
|
+
A producer detects a change and emits a CloudEvent. It owns domain semantics and does not need Payload or Next.js.
|
|
4
|
+
|
|
5
|
+
## Emit an event
|
|
6
|
+
|
|
7
|
+
Use a stable absolute `source`, a generated `rev_` ID, an exact event `type`, and application-owned data. Keep the same event ID when retrying the same delivery.
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
import type { CloudEvent, Dispatcher } from '@klnap/cache-invalidation'
|
|
11
|
+
|
|
12
|
+
declare const dispatcher: Dispatcher
|
|
13
|
+
|
|
14
|
+
const event: CloudEvent = {
|
|
15
|
+
specversion: '1.0',
|
|
16
|
+
id: `rev_${crypto.randomUUID()}`,
|
|
17
|
+
source: 'https://catalog.example.com',
|
|
18
|
+
type: 'com.example.inventory.updated',
|
|
19
|
+
time: new Date().toISOString(),
|
|
20
|
+
datacontenttype: 'application/json',
|
|
21
|
+
data: {
|
|
22
|
+
productId: 'sku-42',
|
|
23
|
+
revision: 17,
|
|
24
|
+
},
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
await dispatcher.dispatch({ events: [event] })
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
The dispatcher owns delivery. It does not interpret event data. See the [custom dispatcher contract](../dispatchers/custom.md).
|
|
31
|
+
|
|
32
|
+
## Emit Resource Change V1
|
|
33
|
+
|
|
34
|
+
Use Resource Change V1 only when before/after snapshots and affected resources suit the producer's domain. It is not required for custom event types.
|
|
35
|
+
|
|
36
|
+
```ts
|
|
37
|
+
import {
|
|
38
|
+
resourceChangeV1DataSchemaUri,
|
|
39
|
+
type CloudEvent,
|
|
40
|
+
type ResourceChangeV1,
|
|
41
|
+
} from '@klnap/cache-invalidation'
|
|
42
|
+
|
|
43
|
+
const data: ResourceChangeV1 = {
|
|
44
|
+
operation: 'update',
|
|
45
|
+
before: { id: 'sku-42', price: 12 },
|
|
46
|
+
after: { id: 'sku-42', price: 10 },
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const event: CloudEvent<ResourceChangeV1> = {
|
|
50
|
+
specversion: '1.0',
|
|
51
|
+
id: `rev_${crypto.randomUUID()}`,
|
|
52
|
+
source: 'https://catalog.example.com',
|
|
53
|
+
type: 'com.example.product.changed',
|
|
54
|
+
dataschema: resourceChangeV1DataSchemaUri,
|
|
55
|
+
data,
|
|
56
|
+
}
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
The event type remains producer-defined. `dataschema` identifies the optional shared data contract. The consumer validates it through its local trusted schema and never retrieves a remote schema.
|
|
60
|
+
|
|
61
|
+
For composable commerce, notify the frontend after the public projection it consumes is ready. If an application updates a search index first, its indexing layer owns that update and emits the completion event. This package does not provide an index adapter or workflow engine.
|
|
62
|
+
|
|
63
|
+
See the [event contract](../concepts/event-contract.md) and [Next.js consumer](../consumers/next.md).
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# Payload media dependencies
|
|
2
|
+
|
|
3
|
+
The Payload producer discovers supported collection and global fields that reference an upload. When media changes, it emits the media event with affected public representations so consumers can apply their normal declarative rules.
|
|
4
|
+
|
|
5
|
+
## Configure uploads
|
|
6
|
+
|
|
7
|
+
The plugin uses the only upload collection or an upload collection named `media`. If neither rule identifies one collection, pass the collection explicitly:
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
cacheInvalidation({
|
|
11
|
+
source: 'https://cms.example.com',
|
|
12
|
+
collections: { articles: true },
|
|
13
|
+
dispatcher: { targets: { website: target } },
|
|
14
|
+
media: mediaCollection,
|
|
15
|
+
})
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
An explicit configuration can set media collection exclusions:
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
media: { collection: mediaCollection, excludeFields: ['updatedAt'] }
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Dependent collections and globals must be enabled in central `collections` or `globals`, or through local `custom.cacheInvalidation`. Local `false` and `{ enabled: false }` disable discovery for that resource. Their projection controls the affected data sent to the consumer.
|
|
25
|
+
|
|
26
|
+
## Trigger behavior
|
|
27
|
+
|
|
28
|
+
- Media updates resolve current dependents after the write.
|
|
29
|
+
- Media deletes resolve dependents before the row is removed.
|
|
30
|
+
- Creates do not change existing media dependencies.
|
|
31
|
+
- The media event includes its own public before/after snapshots. Each affected resource contains its own event type and projected public data.
|
|
32
|
+
|
|
33
|
+
Payload media `alt` changes are localized. A localized change affects locales whose effective representation changes. Shared media fields can affect multiple locales. Configured exclusions are ignored during relevant-change comparison.
|
|
34
|
+
|
|
35
|
+
## Supported relationships
|
|
36
|
+
|
|
37
|
+
Discovery follows configured Payload field structures for single upload or relationship references and `hasMany` references, including supported group, row, collapsible, tabs, and array fields. It does not recursively traverse arbitrary relationships, rich text, JSON, joins, polymorphic references, or block fields. Check current resolver behavior before relying on a field shape outside those structures.
|
|
38
|
+
|
|
39
|
+
The relationship scan is bounded to supported configured references and the changed media ID. It does not build a universal dependency graph.
|
|
40
|
+
|
|
41
|
+
## Resolution failures
|
|
42
|
+
|
|
43
|
+
Each resolved dependent is read again through Payload's normal access control and field visibility before serialization. A failed safe read is logged and omitted; raw privileged documents are not used as fallback. If some discovery scopes fail, successfully resolved dependents can still be dispatched and failures are logged. Saves remain fail-open. Failed scopes are not retried.
|
|
44
|
+
|
|
45
|
+
Collection queries paginate through matching records. Concurrent writes can affect what the scan observes. This integration does not promise a transactionally consistent dependency snapshot or bounded total traversal time.
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
# Payload CMS producer
|
|
2
|
+
|
|
3
|
+
The Payload integration emits CloudEvents describing changes to public collection and global representations. It requires Payload `>=3.94.1 <4`.
|
|
4
|
+
|
|
5
|
+
## Configure the producer
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { cacheInvalidation } from '@klnap/cache-invalidation/producers/payload'
|
|
9
|
+
|
|
10
|
+
export default buildConfig({
|
|
11
|
+
plugins: [
|
|
12
|
+
cacheInvalidation({
|
|
13
|
+
source: 'https://cms.example.com',
|
|
14
|
+
collections: {
|
|
15
|
+
articles: true,
|
|
16
|
+
categories: true,
|
|
17
|
+
},
|
|
18
|
+
dispatcher: {
|
|
19
|
+
targets: {
|
|
20
|
+
website: {
|
|
21
|
+
url: process.env.CACHE_INVALIDATION_URL!,
|
|
22
|
+
secret: process.env.CACHE_INVALIDATION_SECRET!,
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
}),
|
|
27
|
+
],
|
|
28
|
+
})
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
`source` must be a stable absolute URI for this Payload deployment. Each HTTP target needs a destination URL and Standard Webhooks secret. The plugin also accepts an explicit `http()` dispatcher or an application-owned `Dispatcher`.
|
|
32
|
+
|
|
33
|
+
## Opt in centrally or locally
|
|
34
|
+
|
|
35
|
+
Collections and globals are opt-in. Central configuration uses `collections` and `globals`:
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
cacheInvalidation({
|
|
39
|
+
source: 'https://cms.example.com',
|
|
40
|
+
collections: {
|
|
41
|
+
articles: true,
|
|
42
|
+
products: { projection: ({ data }) => ({ id: data.id, sku: data.sku }) },
|
|
43
|
+
},
|
|
44
|
+
globals: { navigation: true },
|
|
45
|
+
dispatcher: { targets: { website: target } },
|
|
46
|
+
})
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
A collection or global can set `custom.cacheInvalidation` locally:
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
export const Articles: CollectionConfig = {
|
|
53
|
+
slug: 'articles',
|
|
54
|
+
custom: { cacheInvalidation: true },
|
|
55
|
+
fields: [],
|
|
56
|
+
}
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Local configuration overrides the central entry. Local `false` disables it. Local `true` enables it. A local options object overrides fields it supplies and inherits other central fields.
|
|
60
|
+
|
|
61
|
+
Supported options include:
|
|
62
|
+
|
|
63
|
+
- `enabled`: disable a configured resource.
|
|
64
|
+
- `hooks`: collection `afterChange` and `afterDelete`; globals support `afterChange`.
|
|
65
|
+
- `filter`: decide whether a public before/after change produces an event.
|
|
66
|
+
- `projection`: select fields to send after full public-state comparison.
|
|
67
|
+
- `excludeFields`: ignore fields when detecting relevant public changes.
|
|
68
|
+
- `orderField`: select the order field for orderable collections.
|
|
69
|
+
|
|
70
|
+
## Public snapshots and lifecycle
|
|
71
|
+
|
|
72
|
+
Payload events use exact event types such as `payload.collection.articles` and `payload.global.navigation`. Each event uses Resource Change V1 data:
|
|
73
|
+
|
|
74
|
+
```json
|
|
75
|
+
{
|
|
76
|
+
"operation": "update",
|
|
77
|
+
"before": {
|
|
78
|
+
"id": "42",
|
|
79
|
+
"locale": "pl",
|
|
80
|
+
"slug": "old-article",
|
|
81
|
+
"title": "Old title"
|
|
82
|
+
},
|
|
83
|
+
"after": {
|
|
84
|
+
"id": "42",
|
|
85
|
+
"locale": "pl",
|
|
86
|
+
"slug": "new-article",
|
|
87
|
+
"title": "New title"
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
The `dataschema` is `urn:klnap:revalidation:resource-change:v1`. Snapshots contain eligible public representation data; localized snapshots contain `locale` inside the snapshot. Relation reads default to depth zero. The producer uses Payload read access and field visibility rather than serializing privileged hook documents.
|
|
93
|
+
|
|
94
|
+
`before` is captured before the write. `after` is read from the published representation. `null` means that representation did not exist. A failed or unknown read is logged and prevents the event from being emitted; it is not converted to `null`.
|
|
95
|
+
|
|
96
|
+
For draft-enabled resources, draft creation and draft-only edits leave the public representation unchanged. First publication changes `null` to a public snapshot. Unpublishing changes a public snapshot to `null`. Deleting a public representation changes its snapshot to `null`. Autosave is skipped. A later publish of an already-public resource compares the old and new public state.
|
|
97
|
+
|
|
98
|
+
## Filter and projection
|
|
99
|
+
|
|
100
|
+
Comparison uses full public state before projection. A title change can emit an event even when the projection contains only an ID and slug.
|
|
101
|
+
|
|
102
|
+
```ts
|
|
103
|
+
custom: {
|
|
104
|
+
cacheInvalidation: {
|
|
105
|
+
filter: ({ before, after }) => before?.title !== after?.title,
|
|
106
|
+
projection: ({ data }) => ({
|
|
107
|
+
id: data.id,
|
|
108
|
+
locale: data.locale,
|
|
109
|
+
slug: data.slug,
|
|
110
|
+
categories: data.categories,
|
|
111
|
+
mpath: data.mpath,
|
|
112
|
+
}),
|
|
113
|
+
},
|
|
114
|
+
}
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
Filter and projection are independent. Use filter to decide whether to emit. Use projection to limit event data. Do not project secrets or fields the receiving application should not access.
|
|
118
|
+
|
|
119
|
+
## Localization and fallback
|
|
120
|
+
|
|
121
|
+
The producer compares each locale's effective published representation, including Payload's configured default-locale fallback when localization fallback is enabled. A shared-field change can affect several locales. A localized field change affects representations whose effective value changed. A fallback source change can therefore affect locales that inherit that value.
|
|
122
|
+
|
|
123
|
+
Stable-ID tags can group translations:
|
|
124
|
+
|
|
125
|
+
```ts
|
|
126
|
+
article: 'article:[id]'
|
|
127
|
+
articleRoute: 'article-route:[locale]:[slug]'
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
All locale variants can share `article:[id]` for article content and related metadata. Application caches must use that tag; the producer does not instrument cache reads.
|
|
131
|
+
|
|
132
|
+
## Media and orderable resources
|
|
133
|
+
|
|
134
|
+
Media changes keep the built-in relationship discovery behavior. The event carries the media resource's own snapshots and affected records for dependent collections or globals. Dependents activate declarative rules for their own event types. See [media dependencies](./payload-media-dependencies.md).
|
|
135
|
+
|
|
136
|
+
Orderable changes preserve the integration's neighbor detection and represent affected neighbors in the event. The package does not build a general relationship graph.
|
|
137
|
+
|
|
138
|
+
## Failure and transaction timing
|
|
139
|
+
|
|
140
|
+
Payload saves fail open when snapshot acquisition or HTTP delivery fails; the failure is logged and the save continues. Ordinary collection/global hooks run before database commit. A delivered event may race a later rollback or let a consumer repopulate stale data. The package does not guarantee post-commit delivery. See [reliability](../reliability.md).
|