@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,88 @@
|
|
|
1
|
+
/** Root-only metadata — not content. */
|
|
2
|
+
const TECHNICAL_KEYS = new Set(['id', 'createdAt', 'updatedAt', '_status', 'globalType']);
|
|
3
|
+
/**
|
|
4
|
+
* Nested noise that changes on republish without real edits.
|
|
5
|
+
* Lexical node `id`s regenerate every publish-all locale write (esp. last locale).
|
|
6
|
+
*/
|
|
7
|
+
const VOLATILE_NESTED_KEYS = new Set(['id']);
|
|
8
|
+
const deleteAtPath = (root, path) => {
|
|
9
|
+
const segments = path.split('.').filter(Boolean);
|
|
10
|
+
if (segments.length === 0)
|
|
11
|
+
return;
|
|
12
|
+
let current = root;
|
|
13
|
+
for (let i = 0; i < segments.length - 1; i++) {
|
|
14
|
+
const segment = segments[i];
|
|
15
|
+
if (!segment)
|
|
16
|
+
return;
|
|
17
|
+
const next = current[segment];
|
|
18
|
+
if (!next || typeof next !== 'object' || Array.isArray(next))
|
|
19
|
+
return;
|
|
20
|
+
current = next;
|
|
21
|
+
}
|
|
22
|
+
const leaf = segments[segments.length - 1];
|
|
23
|
+
if (leaf)
|
|
24
|
+
delete current[leaf];
|
|
25
|
+
};
|
|
26
|
+
/** Removes top-level or nested field paths before content comparison. */
|
|
27
|
+
export const omitFieldPaths = (doc, paths) => {
|
|
28
|
+
const clone = structuredClone(doc);
|
|
29
|
+
for (const path of paths) {
|
|
30
|
+
if (path.includes('.')) {
|
|
31
|
+
deleteAtPath(clone, path);
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
delete clone[path];
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return clone;
|
|
38
|
+
};
|
|
39
|
+
export const hasLocaleData = (doc) => {
|
|
40
|
+
if (!doc)
|
|
41
|
+
return false;
|
|
42
|
+
return Object.entries(doc).some(([key, value]) => {
|
|
43
|
+
if (TECHNICAL_KEYS.has(key))
|
|
44
|
+
return false;
|
|
45
|
+
if (value === null || value === undefined)
|
|
46
|
+
return false;
|
|
47
|
+
if (typeof value === 'string')
|
|
48
|
+
return value.trim().length > 0;
|
|
49
|
+
if (Array.isArray(value))
|
|
50
|
+
return value.length > 0;
|
|
51
|
+
if (typeof value === 'object')
|
|
52
|
+
return Object.keys(value).length > 0;
|
|
53
|
+
return true;
|
|
54
|
+
});
|
|
55
|
+
};
|
|
56
|
+
const sortKeys = (value) => {
|
|
57
|
+
if (Array.isArray(value))
|
|
58
|
+
return value.map(sortKeys);
|
|
59
|
+
if (value && typeof value === 'object') {
|
|
60
|
+
const record = value;
|
|
61
|
+
return Object.keys(record)
|
|
62
|
+
.sort()
|
|
63
|
+
.reduce((acc, key) => {
|
|
64
|
+
if (VOLATILE_NESTED_KEYS.has(key))
|
|
65
|
+
return acc;
|
|
66
|
+
acc[key] = sortKeys(record[key]);
|
|
67
|
+
return acc;
|
|
68
|
+
}, {});
|
|
69
|
+
}
|
|
70
|
+
return value;
|
|
71
|
+
};
|
|
72
|
+
const stripTechnical = (doc) => {
|
|
73
|
+
const out = {};
|
|
74
|
+
for (const [key, value] of Object.entries(doc)) {
|
|
75
|
+
if (TECHNICAL_KEYS.has(key))
|
|
76
|
+
continue;
|
|
77
|
+
out[key] = value;
|
|
78
|
+
}
|
|
79
|
+
return sortKeys(out);
|
|
80
|
+
};
|
|
81
|
+
const prepareForCompare = (doc, options) => {
|
|
82
|
+
const withoutExcluded = options?.excludeFields?.length
|
|
83
|
+
? omitFieldPaths(doc, options.excludeFields)
|
|
84
|
+
: doc;
|
|
85
|
+
return stripTechnical(withoutExcluded);
|
|
86
|
+
};
|
|
87
|
+
/** Content-only equality — ignores timestamps / _status / Lexical node ids / excludeFields. */
|
|
88
|
+
export const docsContentEqual = (a, b, options) => JSON.stringify(prepareForCompare(a, options)) === JSON.stringify(prepareForCompare(b, options));
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* True when doc has no published status (string draft, localizeStatus map
|
|
3
|
+
* without `published`, or missing `_status`). Used when Local API / admin
|
|
4
|
+
* autosave create sets `draft: true` without `req.query.draft`.
|
|
5
|
+
*/
|
|
6
|
+
export declare const isUnpublishedStatus: (status: unknown) => boolean;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* True when doc has no published status (string draft, localizeStatus map
|
|
3
|
+
* without `published`, or missing `_status`). Used when Local API / admin
|
|
4
|
+
* autosave create sets `draft: true` without `req.query.draft`.
|
|
5
|
+
*/
|
|
6
|
+
export const isUnpublishedStatus = (status) => {
|
|
7
|
+
if (status === 'published')
|
|
8
|
+
return false;
|
|
9
|
+
if (status === 'draft' || status == null)
|
|
10
|
+
return true;
|
|
11
|
+
if (typeof status === 'object') {
|
|
12
|
+
return !Object.values(status).includes('published');
|
|
13
|
+
}
|
|
14
|
+
return true;
|
|
15
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** Barrel — prefer importing from focused modules in new code. */
|
|
2
|
+
export { dispatchRevalidationChanges } from './deferred.js';
|
|
3
|
+
export { postRevalidationChanges } from './dispatch.js';
|
|
4
|
+
export { docsContentEqual, hasLocaleData } from './doc-content.js';
|
|
5
|
+
export { isUnpublishedStatus } from './draft-status.js';
|
|
6
|
+
export { readCollectionIdentity } from './identifiers.js';
|
|
7
|
+
export { getRequestLocale, isDraftSave, isPublishAllLocales, listLocales, resolveChangedLocales, toTypedLocale, } from './locale.js';
|
|
8
|
+
export { logRevalidateDispatch, mediaSubject, revalidationLog, revalidationLogError, } from './log.js';
|
|
9
|
+
export { MEDIA_LOCALIZED_KEYS, mediaSharedFieldsEqual } from './media-equality.js';
|
|
10
|
+
export { getOperation } from './operation.js';
|
|
11
|
+
export { clearPublicBefore, projectPayloadSnapshot, readPublishedRepresentation, takePublicBefore, } from './public-snapshots.js';
|
|
12
|
+
export { shouldSkipDraftRevalidation, shouldSkipUnchangedLocale, shouldSkipUnpublishedLocaleDoc, } from './skip-rules.js';
|
|
13
|
+
export type { RevalidateArgs, RevalidationChange } from './types.js';
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** Barrel — prefer importing from focused modules in new code. */
|
|
2
|
+
export { dispatchRevalidationChanges } from './deferred.js';
|
|
3
|
+
export { postRevalidationChanges } from './dispatch.js';
|
|
4
|
+
export { docsContentEqual, hasLocaleData } from './doc-content.js';
|
|
5
|
+
export { isUnpublishedStatus } from './draft-status.js';
|
|
6
|
+
export { readCollectionIdentity } from './identifiers.js';
|
|
7
|
+
export { getRequestLocale, isDraftSave, isPublishAllLocales, listLocales, resolveChangedLocales, toTypedLocale, } from './locale.js';
|
|
8
|
+
export { logRevalidateDispatch, mediaSubject, revalidationLog, revalidationLogError, } from './log.js';
|
|
9
|
+
export { MEDIA_LOCALIZED_KEYS, mediaSharedFieldsEqual } from './media-equality.js';
|
|
10
|
+
export { getOperation } from './operation.js';
|
|
11
|
+
export { clearPublicBefore, projectPayloadSnapshot, readPublishedRepresentation, takePublicBefore, } from './public-snapshots.js';
|
|
12
|
+
export { shouldSkipDraftRevalidation, shouldSkipUnchangedLocale, shouldSkipUnpublishedLocaleDoc, } from './skip-rules.js';
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { CollectionConfig, PayloadRequest } from 'payload';
|
|
2
|
+
export declare function readIdentifierAtPath(doc: Record<string, unknown>, path: string, locale: string, localizedField: boolean): string | undefined;
|
|
3
|
+
export declare function listPublishedLocalesForDocument({ req, collection, doc, }: {
|
|
4
|
+
req: PayloadRequest;
|
|
5
|
+
collection: CollectionConfig;
|
|
6
|
+
doc: Record<string, unknown>;
|
|
7
|
+
}): string[];
|
|
8
|
+
export declare const uniqueLocales: (locales: string[]) => string[];
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { listLocales } from './locale.js';
|
|
2
|
+
import { hasLocalizedDraftStatus, isLocalePublished } from './locale-published.js';
|
|
3
|
+
function readValueAtPath(doc, path) {
|
|
4
|
+
const segments = path.split('.').filter(Boolean);
|
|
5
|
+
let current = doc;
|
|
6
|
+
for (const segment of segments) {
|
|
7
|
+
if (!current || typeof current !== 'object' || Array.isArray(current))
|
|
8
|
+
return undefined;
|
|
9
|
+
current = current[segment];
|
|
10
|
+
}
|
|
11
|
+
return current;
|
|
12
|
+
}
|
|
13
|
+
const identifierValueToString = (value) => {
|
|
14
|
+
if (value == null)
|
|
15
|
+
return undefined;
|
|
16
|
+
if (typeof value === 'string') {
|
|
17
|
+
const t = value.trim();
|
|
18
|
+
return t.length > 0 ? t : undefined;
|
|
19
|
+
}
|
|
20
|
+
if (typeof value === 'number' || typeof value === 'boolean') {
|
|
21
|
+
return String(value);
|
|
22
|
+
}
|
|
23
|
+
return undefined;
|
|
24
|
+
};
|
|
25
|
+
export function readIdentifierAtPath(doc, path, locale, localizedField) {
|
|
26
|
+
if (!localizedField) {
|
|
27
|
+
return identifierValueToString(readValueAtPath(doc, path));
|
|
28
|
+
}
|
|
29
|
+
const segments = path.split('.').filter(Boolean);
|
|
30
|
+
if (segments.length === 0)
|
|
31
|
+
return undefined;
|
|
32
|
+
let current = doc;
|
|
33
|
+
for (const segment of segments) {
|
|
34
|
+
if (!current || typeof current !== 'object' || Array.isArray(current))
|
|
35
|
+
return undefined;
|
|
36
|
+
current = current[segment];
|
|
37
|
+
}
|
|
38
|
+
if (current && typeof current === 'object' && !Array.isArray(current)) {
|
|
39
|
+
return identifierValueToString(current[locale]);
|
|
40
|
+
}
|
|
41
|
+
return identifierValueToString(current);
|
|
42
|
+
}
|
|
43
|
+
export function listPublishedLocalesForDocument({ req, collection, doc, }) {
|
|
44
|
+
const localizeStatus = hasLocalizedDraftStatus(collection);
|
|
45
|
+
const status = doc._status;
|
|
46
|
+
return listLocales(req).filter((locale) => isLocalePublished(status, locale, localizeStatus));
|
|
47
|
+
}
|
|
48
|
+
export const uniqueLocales = (locales) => [...new Set(locales)];
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { CollectionConfig, GlobalConfig } from 'payload';
|
|
2
|
+
type DraftVersionsSchema = Pick<CollectionConfig, 'versions'> | Pick<GlobalConfig, 'versions'>;
|
|
3
|
+
export declare const hasLocalizedDraftStatus: (schema: DraftVersionsSchema) => boolean;
|
|
4
|
+
/** Per-locale publish check for locale-scoped reads (`findByID` / `findGlobal` with `locale`). */
|
|
5
|
+
export declare const isLocalePublished: (status: unknown, locale: string, _localizeStatus: boolean) => boolean;
|
|
6
|
+
export declare const isLocaleUnpublished: (status: unknown, locale: string, localizeStatus: boolean) => boolean;
|
|
7
|
+
export {};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export const hasLocalizedDraftStatus = (schema) => {
|
|
2
|
+
const versions = schema.versions;
|
|
3
|
+
if (!versions || typeof versions !== 'object' || !('drafts' in versions))
|
|
4
|
+
return false;
|
|
5
|
+
const drafts = versions.drafts;
|
|
6
|
+
return typeof drafts === 'object' && drafts !== null && drafts.localizeStatus === true;
|
|
7
|
+
};
|
|
8
|
+
/** Per-locale publish check for locale-scoped reads (`findByID` / `findGlobal` with `locale`). */
|
|
9
|
+
export const isLocalePublished = (status, locale, _localizeStatus) => {
|
|
10
|
+
if (typeof status === 'object' && status !== null) {
|
|
11
|
+
return status[locale] === 'published';
|
|
12
|
+
}
|
|
13
|
+
if (status === 'published') {
|
|
14
|
+
// Locale-scoped API returns a resolved string even when `localizeStatus` is enabled.
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
return false;
|
|
18
|
+
};
|
|
19
|
+
export const isLocaleUnpublished = (status, locale, localizeStatus) => !isLocalePublished(status, locale, localizeStatus);
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { PayloadRequest, TypedLocale } from 'payload';
|
|
2
|
+
import type { RevalidateArgs } from './types.js';
|
|
3
|
+
/** Wire + tag locale when `config.localization` is off (single-locale hosts). */
|
|
4
|
+
export declare const resolveDefaultLocaleCode: (req: PayloadRequest) => string;
|
|
5
|
+
export declare const listLocales: (req: PayloadRequest) => string[];
|
|
6
|
+
/** Bridge config locale codes → Payload `TypedLocale` (generated from localization config). */
|
|
7
|
+
export declare const toTypedLocale: (locale: string) => TypedLocale;
|
|
8
|
+
export { isPublishAllLocales } from '../publish-all/request-flags.js';
|
|
9
|
+
export declare const isDraftSave: (req: {
|
|
10
|
+
query?: Record<string, unknown>;
|
|
11
|
+
}) => boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Locale actually being edited/published (admin tab).
|
|
14
|
+
* Prefer query over req.locale — on publishAllLocales Payload leaves req.locale
|
|
15
|
+
* as the last locale it iterated (en↔de swap), while query.locale stays the tab.
|
|
16
|
+
*/
|
|
17
|
+
export declare const getRequestLocale: (req: {
|
|
18
|
+
locale?: string | null;
|
|
19
|
+
query?: Record<string, unknown>;
|
|
20
|
+
}) => string | undefined;
|
|
21
|
+
export declare const resolveChangedLocales: ({ args, locales, }: {
|
|
22
|
+
args: RevalidateArgs;
|
|
23
|
+
locales: string[];
|
|
24
|
+
}) => string[];
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { isPublishAllLocales } from '../publish-all/request-flags.js';
|
|
2
|
+
import { isRevalidationAllLocales } from './deferred.js';
|
|
3
|
+
import { getOperation } from './operation.js';
|
|
4
|
+
/** Wire + tag locale when `config.localization` is off (single-locale hosts). */
|
|
5
|
+
export const resolveDefaultLocaleCode = (req) => {
|
|
6
|
+
const localization = req.payload.config.localization;
|
|
7
|
+
if (localization && typeof localization === 'object' && 'defaultLocale' in localization) {
|
|
8
|
+
const code = localization.defaultLocale;
|
|
9
|
+
if (typeof code === 'string' && code.length > 0)
|
|
10
|
+
return code;
|
|
11
|
+
}
|
|
12
|
+
return 'en';
|
|
13
|
+
};
|
|
14
|
+
export const listLocales = (req) => {
|
|
15
|
+
const localization = req.payload.config.localization;
|
|
16
|
+
if (!localization)
|
|
17
|
+
return [resolveDefaultLocaleCode(req)];
|
|
18
|
+
return localization.locales.map((locale) => (typeof locale === 'string' ? locale : locale.code));
|
|
19
|
+
};
|
|
20
|
+
/** Bridge config locale codes → Payload `TypedLocale` (generated from localization config). */
|
|
21
|
+
export const toTypedLocale = (locale) => locale;
|
|
22
|
+
export { isPublishAllLocales } from '../publish-all/request-flags.js';
|
|
23
|
+
export const isDraftSave = (req) => req.query?.draft === 'true' || req.query?.draft === true;
|
|
24
|
+
const asLocaleCode = (value) => typeof value === 'string' && value.length > 0 && value !== 'all' ? value : undefined;
|
|
25
|
+
/**
|
|
26
|
+
* Locale actually being edited/published (admin tab).
|
|
27
|
+
* Prefer query over req.locale — on publishAllLocales Payload leaves req.locale
|
|
28
|
+
* as the last locale it iterated (en↔de swap), while query.locale stays the tab.
|
|
29
|
+
*/
|
|
30
|
+
export const getRequestLocale = (req) => asLocaleCode(req.query?.publishSpecificLocale) ??
|
|
31
|
+
asLocaleCode(req.query?.locale) ??
|
|
32
|
+
asLocaleCode(req.locale);
|
|
33
|
+
export const resolveChangedLocales = ({ args, locales, }) => {
|
|
34
|
+
if (getOperation(args) === 'delete')
|
|
35
|
+
return locales;
|
|
36
|
+
if (isPublishAllLocales(args.req))
|
|
37
|
+
return locales;
|
|
38
|
+
if (isRevalidationAllLocales(args.req))
|
|
39
|
+
return locales;
|
|
40
|
+
const locale = getRequestLocale(args.req);
|
|
41
|
+
if (locale)
|
|
42
|
+
return [locale];
|
|
43
|
+
return locales;
|
|
44
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { RevalidateArgs } from './types.js';
|
|
2
|
+
type RevalidationInfoLogger = {
|
|
3
|
+
info?: (message: string) => void;
|
|
4
|
+
};
|
|
5
|
+
type RevalidationWarnLogger = {
|
|
6
|
+
warn?: (message: string) => void;
|
|
7
|
+
};
|
|
8
|
+
type RevalidationErrorLogger = RevalidationInfoLogger & {
|
|
9
|
+
error: (message: string) => void;
|
|
10
|
+
};
|
|
11
|
+
export type LocaleSkipReason = 'draft' | 'empty' | 'missing';
|
|
12
|
+
/** Turns internal tokens like `de:draft` into compact log text. */
|
|
13
|
+
export declare const describeLocaleSkip: (token: string) => string;
|
|
14
|
+
/** Structured CMS logs: `dispatch|skip · subject · detail` */
|
|
15
|
+
export declare const revalidationLog: {
|
|
16
|
+
skip(logger: RevalidationInfoLogger, subject: string, detail: string): void;
|
|
17
|
+
dispatch(logger: RevalidationInfoLogger, subject: string, detail: string): void;
|
|
18
|
+
warn(logger: RevalidationWarnLogger, subject: string, detail: string): void;
|
|
19
|
+
};
|
|
20
|
+
export declare const revalidationLogError: (logger: RevalidationErrorLogger, detail: string, subject?: string) => void;
|
|
21
|
+
export declare const mediaSubject: (mediaId: string | number) => string;
|
|
22
|
+
export declare const revalidationSubject: (args: RevalidateArgs) => string;
|
|
23
|
+
export type FormatLogDetailInput = {
|
|
24
|
+
locales?: string[];
|
|
25
|
+
docId?: string | number;
|
|
26
|
+
skippedTokens?: string[];
|
|
27
|
+
reason?: string;
|
|
28
|
+
};
|
|
29
|
+
export declare const formatLogDetail: ({ locales, docId, skippedTokens, reason, }: FormatLogDetailInput) => string;
|
|
30
|
+
export declare const logRevalidateDispatch: (logger: RevalidationInfoLogger, target: string, locales: string[], skippedTokens?: string[], docId?: string | number) => void;
|
|
31
|
+
export {};
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
const PREFIX = '[revalidate]';
|
|
2
|
+
const SEP = ' · ';
|
|
3
|
+
const localeSkipPhrase = (locale, reason) => {
|
|
4
|
+
switch (reason) {
|
|
5
|
+
case 'draft':
|
|
6
|
+
return `${locale} draft`;
|
|
7
|
+
case 'empty':
|
|
8
|
+
return `${locale} empty`;
|
|
9
|
+
case 'missing':
|
|
10
|
+
return `${locale} missing`;
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
/** Turns internal tokens like `de:draft` into compact log text. */
|
|
14
|
+
export const describeLocaleSkip = (token) => {
|
|
15
|
+
const sep = token.indexOf(':');
|
|
16
|
+
if (sep === -1)
|
|
17
|
+
return token;
|
|
18
|
+
const locale = token.slice(0, sep);
|
|
19
|
+
const reason = token.slice(sep + 1);
|
|
20
|
+
if (reason === 'draft' || reason === 'empty' || reason === 'missing') {
|
|
21
|
+
return localeSkipPhrase(locale, reason);
|
|
22
|
+
}
|
|
23
|
+
return token;
|
|
24
|
+
};
|
|
25
|
+
const line = (action, subject, detail) => {
|
|
26
|
+
return detail
|
|
27
|
+
? `${PREFIX} ${action}${SEP}${subject}${SEP}${detail}`
|
|
28
|
+
: `${PREFIX} ${action}${SEP}${subject}`;
|
|
29
|
+
};
|
|
30
|
+
/** Structured CMS logs: `dispatch|skip · subject · detail` */
|
|
31
|
+
export const revalidationLog = {
|
|
32
|
+
skip(logger, subject, detail) {
|
|
33
|
+
logger.info?.(line('skip', subject, detail));
|
|
34
|
+
},
|
|
35
|
+
dispatch(logger, subject, detail) {
|
|
36
|
+
logger.info?.(line('dispatch', subject, detail));
|
|
37
|
+
},
|
|
38
|
+
warn(logger, subject, detail) {
|
|
39
|
+
logger.warn?.(line('warn', subject, detail));
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
export const revalidationLogError = (logger, detail, subject = 'dispatch') => {
|
|
43
|
+
logger.error(line('error', subject, detail));
|
|
44
|
+
};
|
|
45
|
+
export const mediaSubject = (mediaId) => `media:${mediaId}`;
|
|
46
|
+
export const revalidationSubject = (args) => {
|
|
47
|
+
const type = 'collection' in args ? 'collection' : 'global';
|
|
48
|
+
const slug = 'collection' in args ? args.collection.slug : args.global.slug;
|
|
49
|
+
return `${type}:${slug}`;
|
|
50
|
+
};
|
|
51
|
+
export const formatLogDetail = ({ locales, docId, skippedTokens, reason, }) => {
|
|
52
|
+
const parts = [];
|
|
53
|
+
if (locales?.length)
|
|
54
|
+
parts.push(locales.join(', '));
|
|
55
|
+
if (docId !== undefined && docId !== null)
|
|
56
|
+
parts.push(`id:${docId}`);
|
|
57
|
+
if (skippedTokens?.length) {
|
|
58
|
+
parts.push(`omit ${skippedTokens.map(describeLocaleSkip).join(', ')}`);
|
|
59
|
+
}
|
|
60
|
+
if (reason)
|
|
61
|
+
parts.push(reason);
|
|
62
|
+
return parts.join(SEP);
|
|
63
|
+
};
|
|
64
|
+
export const logRevalidateDispatch = (logger, target, locales, skippedTokens = [], docId) => {
|
|
65
|
+
const detail = formatLogDetail({ locales, docId, skippedTokens });
|
|
66
|
+
revalidationLog.dispatch(logger, target, detail);
|
|
67
|
+
};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { DocsContentEqualOptions } from './doc-content.js';
|
|
2
|
+
export declare const MEDIA_LOCALIZED_KEYS: Set<string>;
|
|
3
|
+
/** True when non-excluded media fields match (file/url/focal/…). */
|
|
4
|
+
export declare const mediaSharedFieldsEqual: (a: Record<string, unknown>, b: Record<string, unknown>, options?: DocsContentEqualOptions) => boolean;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { omitFieldPaths } from './doc-content.js';
|
|
2
|
+
const TECHNICAL_KEYS = new Set(['id', 'createdAt', 'updatedAt', '_status', 'globalType']);
|
|
3
|
+
const VOLATILE_NESTED_KEYS = new Set(['id']);
|
|
4
|
+
const sortKeys = (value) => {
|
|
5
|
+
if (Array.isArray(value))
|
|
6
|
+
return value.map(sortKeys);
|
|
7
|
+
if (value && typeof value === 'object') {
|
|
8
|
+
const record = value;
|
|
9
|
+
return Object.keys(record)
|
|
10
|
+
.sort()
|
|
11
|
+
.reduce((acc, key) => {
|
|
12
|
+
if (VOLATILE_NESTED_KEYS.has(key))
|
|
13
|
+
return acc;
|
|
14
|
+
acc[key] = sortKeys(record[key]);
|
|
15
|
+
return acc;
|
|
16
|
+
}, {});
|
|
17
|
+
}
|
|
18
|
+
return value;
|
|
19
|
+
};
|
|
20
|
+
const stripForMediaShared = (doc, excludeFields) => {
|
|
21
|
+
const withoutExcluded = excludeFields.length ? omitFieldPaths(doc, excludeFields) : doc;
|
|
22
|
+
const out = {};
|
|
23
|
+
for (const [key, value] of Object.entries(withoutExcluded)) {
|
|
24
|
+
if (TECHNICAL_KEYS.has(key))
|
|
25
|
+
continue;
|
|
26
|
+
out[key] = value;
|
|
27
|
+
}
|
|
28
|
+
return sortKeys(out);
|
|
29
|
+
};
|
|
30
|
+
export const MEDIA_LOCALIZED_KEYS = new Set(['alt']);
|
|
31
|
+
/** True when non-excluded media fields match (file/url/focal/…). */
|
|
32
|
+
export const mediaSharedFieldsEqual = (a, b, options) => {
|
|
33
|
+
const excludeFields = options?.excludeFields ?? [];
|
|
34
|
+
return (JSON.stringify(stripForMediaShared(a, excludeFields)) ===
|
|
35
|
+
JSON.stringify(stripForMediaShared(b, excludeFields)));
|
|
36
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
type OrderableRequest = {
|
|
2
|
+
context?: Record<string, unknown>;
|
|
3
|
+
};
|
|
4
|
+
/**
|
|
5
|
+
* Structurally compatible with `@klnap/payload-orderable` hooks. Neither package imports the other;
|
|
6
|
+
* the host passes this object to `orderable({ hooks })`.
|
|
7
|
+
*/
|
|
8
|
+
export type CacheInvalidationOrderableHooks = {
|
|
9
|
+
beforeWrite: (req: OrderableRequest, batch: {
|
|
10
|
+
orderScope: 'global' | 'locale';
|
|
11
|
+
}) => void;
|
|
12
|
+
onCommitted: (event: unknown, req: OrderableRequest) => Promise<void>;
|
|
13
|
+
onRolledBack: (error: unknown, req: OrderableRequest) => void;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Hooks for `orderable()` that keep reorder writes out of the transaction and dispatch one
|
|
17
|
+
* revalidation request after commit. A reorder is recorded as order-only, and a global reorder
|
|
18
|
+
* revalidates every locale. Rolled-back reorders are discarded.
|
|
19
|
+
*/
|
|
20
|
+
export declare const cacheInvalidationOrderableHooks: () => CacheInvalidationOrderableHooks;
|
|
21
|
+
export {};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { deferRevalidationUntilCommit, markRevalidationAllLocales, markRevalidationOrderOnly, } from './deferred.js';
|
|
2
|
+
/**
|
|
3
|
+
* Hooks for `orderable()` that keep reorder writes out of the transaction and dispatch one
|
|
4
|
+
* revalidation request after commit. A reorder is recorded as order-only, and a global reorder
|
|
5
|
+
* revalidates every locale. Rolled-back reorders are discarded.
|
|
6
|
+
*/
|
|
7
|
+
export const cacheInvalidationOrderableHooks = () => {
|
|
8
|
+
const deferrals = new WeakMap();
|
|
9
|
+
return {
|
|
10
|
+
beforeWrite: (req, { orderScope }) => {
|
|
11
|
+
if (orderScope === 'global')
|
|
12
|
+
markRevalidationAllLocales(req);
|
|
13
|
+
markRevalidationOrderOnly(req);
|
|
14
|
+
deferrals.set(req, deferRevalidationUntilCommit(req));
|
|
15
|
+
},
|
|
16
|
+
onCommitted: async (_event, req) => {
|
|
17
|
+
const deferral = deferrals.get(req);
|
|
18
|
+
deferrals.delete(req);
|
|
19
|
+
await deferral?.flush();
|
|
20
|
+
},
|
|
21
|
+
onRolledBack: (_error, req) => {
|
|
22
|
+
deferrals.get(req)?.discard();
|
|
23
|
+
deferrals.delete(req);
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { PayloadRequest } from 'payload';
|
|
2
|
+
import type { RevalidationChange } from './types.js';
|
|
3
|
+
type Doc = Record<string, unknown>;
|
|
4
|
+
/**
|
|
5
|
+
* Documents whose Previous/Next a change can affect, per locale, excluding the changed documents.
|
|
6
|
+
*
|
|
7
|
+
* - Moves to a new order key: exact diff of visible neighbours in that locale only.
|
|
8
|
+
* - Everything else (content, slug, create, delete, publish state at the same key): the documents
|
|
9
|
+
* directly before and after the document's current and previous keys, read in the same locale.
|
|
10
|
+
* Broader than strictly required but it covers link labels and slugs.
|
|
11
|
+
*/
|
|
12
|
+
export declare const resolveOrderableNeighborChanges: ({ req, collectionSlug, changes, orderField, draftsEnabled, previousOrderKey, toChange, }: {
|
|
13
|
+
req: PayloadRequest;
|
|
14
|
+
collectionSlug: string;
|
|
15
|
+
changes: RevalidationChange[];
|
|
16
|
+
orderField: string;
|
|
17
|
+
draftsEnabled: boolean;
|
|
18
|
+
previousOrderKey?: unknown;
|
|
19
|
+
toChange: (doc: Doc, locale: string) => RevalidationChange;
|
|
20
|
+
}) => Promise<RevalidationChange[]>;
|
|
21
|
+
export {};
|