@klnap/cache-invalidation 0.0.0-stage → 0.1.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 +145 -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,36 @@
|
|
|
1
|
+
import type { CollectionConfig, Plugin } from 'payload';
|
|
2
|
+
import type { Dispatcher } from '../../core/index.js';
|
|
3
|
+
import { type HttpDispatcherConfig } from '../../dispatchers/http/index.js';
|
|
4
|
+
import { type PayloadCacheInvalidationOptions } from './revalidation-custom.js';
|
|
5
|
+
export type DispatcherConfig = Dispatcher | HttpDispatcherConfig;
|
|
6
|
+
export type DispatcherSelection = DispatcherConfig | {
|
|
7
|
+
default: DispatcherConfig;
|
|
8
|
+
fallback?: DispatcherConfig;
|
|
9
|
+
};
|
|
10
|
+
export type MediaPluginConfig = CollectionConfig | {
|
|
11
|
+
collection: CollectionConfig;
|
|
12
|
+
excludeFields?: string[];
|
|
13
|
+
};
|
|
14
|
+
export type PluginSchemaExcludeConfig = boolean | PayloadCacheInvalidationOptions;
|
|
15
|
+
export type PayloadCacheInvalidationPluginConfig = {
|
|
16
|
+
dispatcher: DispatcherSelection;
|
|
17
|
+
/** Stable absolute CloudEvents source URI for this producer. */
|
|
18
|
+
source: string;
|
|
19
|
+
/**
|
|
20
|
+
* Upload collection for media-dependent hooks (optional).
|
|
21
|
+
* Omitted → auto: only upload collection, or slug `media` among uploads.
|
|
22
|
+
*/
|
|
23
|
+
media?: MediaPluginConfig;
|
|
24
|
+
enabled?: boolean;
|
|
25
|
+
/** Default exclude list when schema and per-slug plugin entries omit `excludeFields`. */
|
|
26
|
+
excludeFields?: string[];
|
|
27
|
+
/** Per collection slug. Local `custom.cacheInvalidation` overrides these options. */
|
|
28
|
+
collections?: Record<string, PluginSchemaExcludeConfig>;
|
|
29
|
+
/** Per global slug. Local `custom.cacheInvalidation` overrides these options. */
|
|
30
|
+
globals?: Record<string, PluginSchemaExcludeConfig>;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Payload plugin: `custom.cacheInvalidation` on collections/globals → events → dispatcher.
|
|
34
|
+
* Opt-in via `custom.cacheInvalidation` (like draftMode). Fail-open on dispatch.
|
|
35
|
+
*/
|
|
36
|
+
export declare function cacheInvalidation(config: PayloadCacheInvalidationPluginConfig): Plugin;
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
import { http } from '../../dispatchers/http/index.js';
|
|
2
|
+
import { normalizeExcludeFields } from './exclude-fields.js';
|
|
3
|
+
import { createCaptureCollectionPublicBeforeChange, createCaptureCollectionPublicBeforeDelete, createCaptureGlobalPublicBeforeChange, } from './lifecycle/public-snapshots.js';
|
|
4
|
+
import { createCollectionRevalidateAfterChange, createCollectionRevalidateAfterDelete, createGlobalRevalidateAfterChange, } from './lifecycle/revalidate.js';
|
|
5
|
+
import { resolveMediaSlug } from './media/resolve-media-slug.js';
|
|
6
|
+
import { createRevalidateMediaDependentsAfterChange, createRevalidateMediaDependentsBeforeDelete, } from './media/revalidate-media-dependents.js';
|
|
7
|
+
import { mirrorPublishAllLocalesFlagsToReq, preserveLocalesOnPublishAll, restoreLocalesAfterPublishAll, } from './publish-all/index.js';
|
|
8
|
+
import { normalizeCollectionRevalidation, normalizeGlobalRevalidation, normalizeResourceMap, } from './revalidation-custom.js';
|
|
9
|
+
const REVALIDATION_PLUGIN_APPLIED = Symbol('revalidation plugin applied');
|
|
10
|
+
const resolveDispatcherConfig = (selection) => {
|
|
11
|
+
if ('dispatch' in selection && typeof selection.dispatch === 'function')
|
|
12
|
+
return selection;
|
|
13
|
+
if ('targets' in selection && selection.targets && typeof selection.targets === 'object') {
|
|
14
|
+
return http(selection);
|
|
15
|
+
}
|
|
16
|
+
throw new TypeError('cacheInvalidation: dispatcher must implement Dispatcher or HTTP config contract');
|
|
17
|
+
};
|
|
18
|
+
const resolveDispatcher = (selection) => {
|
|
19
|
+
if ('default' in selection) {
|
|
20
|
+
const primary = resolveDispatcherConfig(selection.default);
|
|
21
|
+
const fallback = selection.fallback ? resolveDispatcherConfig(selection.fallback) : undefined;
|
|
22
|
+
if (!fallback)
|
|
23
|
+
return primary;
|
|
24
|
+
return {
|
|
25
|
+
async dispatch(input) {
|
|
26
|
+
try {
|
|
27
|
+
const first = await primary.dispatch(input);
|
|
28
|
+
if (first.status === 'accepted')
|
|
29
|
+
return first;
|
|
30
|
+
if (first.status === 'unknown' || first.status === 'rejected')
|
|
31
|
+
return first;
|
|
32
|
+
return fallback.dispatch(input);
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
return fallback.dispatch(input);
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
return resolveDispatcherConfig(selection);
|
|
41
|
+
};
|
|
42
|
+
const appendHook = (existing, hook) => {
|
|
43
|
+
if (!existing)
|
|
44
|
+
return [hook];
|
|
45
|
+
return Array.isArray(existing) ? [...existing, hook] : [existing, hook];
|
|
46
|
+
};
|
|
47
|
+
const prependHook = (existing, hook) => {
|
|
48
|
+
if (!existing)
|
|
49
|
+
return [hook];
|
|
50
|
+
return Array.isArray(existing) ? [hook, ...existing] : [hook, existing];
|
|
51
|
+
};
|
|
52
|
+
/** User hooks, then publish-all locale restore, then revalidation dispatch. */
|
|
53
|
+
const appendRevalidationAfterChange = (existing, restore, revalidate) => {
|
|
54
|
+
const base = !existing ? [] : Array.isArray(existing) ? [...existing] : [existing];
|
|
55
|
+
return [...base, restore, revalidate];
|
|
56
|
+
};
|
|
57
|
+
const resolveMediaPluginConfig = (media) => {
|
|
58
|
+
if (!media)
|
|
59
|
+
return { collection: undefined, excludeFields: undefined };
|
|
60
|
+
if ('collection' in media) {
|
|
61
|
+
return {
|
|
62
|
+
collection: media.collection,
|
|
63
|
+
excludeFields: media.excludeFields,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
return { collection: media, excludeFields: undefined };
|
|
67
|
+
};
|
|
68
|
+
const validateRevalidationExcludeFieldsOnConfig = (collections, globals, pluginExcludeFields, mediaPluginExcludeFields) => {
|
|
69
|
+
if (pluginExcludeFields?.length)
|
|
70
|
+
normalizeExcludeFields(pluginExcludeFields);
|
|
71
|
+
if (mediaPluginExcludeFields?.length)
|
|
72
|
+
normalizeExcludeFields(mediaPluginExcludeFields);
|
|
73
|
+
for (const options of [...Object.values(collections), ...Object.values(globals)]) {
|
|
74
|
+
if (options !== true && options.excludeFields)
|
|
75
|
+
normalizeExcludeFields(options.excludeFields);
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
/**
|
|
79
|
+
* Payload plugin: `custom.cacheInvalidation` on collections/globals → events → dispatcher.
|
|
80
|
+
* Opt-in via `custom.cacheInvalidation` (like draftMode). Fail-open on dispatch.
|
|
81
|
+
*/
|
|
82
|
+
export function cacheInvalidation(config) {
|
|
83
|
+
const enabled = config.enabled !== false;
|
|
84
|
+
const resolvedDispatcher = resolveDispatcher(config.dispatcher);
|
|
85
|
+
try {
|
|
86
|
+
const source = new URL(config.source);
|
|
87
|
+
if (!source.protocol)
|
|
88
|
+
throw new Error();
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
throw new TypeError('cacheInvalidation: source must be an absolute URI');
|
|
92
|
+
}
|
|
93
|
+
const dispatcher = resolvedDispatcher;
|
|
94
|
+
const pluginExcludeFields = config.excludeFields;
|
|
95
|
+
const { collection: mediaCollectionOverride, excludeFields: mediaPluginExcludeFields } = resolveMediaPluginConfig(config.media);
|
|
96
|
+
const hookOptions = {
|
|
97
|
+
source: config.source,
|
|
98
|
+
pluginExcludeFields,
|
|
99
|
+
};
|
|
100
|
+
const mediaHookOptions = {
|
|
101
|
+
source: config.source,
|
|
102
|
+
pluginExcludeFields,
|
|
103
|
+
pluginCollectionExcludeFields: undefined,
|
|
104
|
+
mediaPluginExcludeFields,
|
|
105
|
+
};
|
|
106
|
+
return (incoming) => {
|
|
107
|
+
if (!enabled)
|
|
108
|
+
return incoming;
|
|
109
|
+
if (incoming[REVALIDATION_PLUGIN_APPLIED]) {
|
|
110
|
+
throw new Error('cacheInvalidation: plugin is already applied to this Payload config');
|
|
111
|
+
}
|
|
112
|
+
const mediaSlug = resolveMediaSlug(incoming, mediaCollectionOverride);
|
|
113
|
+
const collectionOptions = normalizeResourceMap(config.collections, incoming.collections ?? []);
|
|
114
|
+
const globalOptions = normalizeResourceMap(config.globals, incoming.globals ?? []);
|
|
115
|
+
validateRevalidationExcludeFieldsOnConfig(collectionOptions, globalOptions, pluginExcludeFields, mediaPluginExcludeFields);
|
|
116
|
+
hookOptions.collections = collectionOptions;
|
|
117
|
+
hookOptions.globals = globalOptions;
|
|
118
|
+
const mirrorPublishAllLocalesToReq = mirrorPublishAllLocalesFlagsToReq();
|
|
119
|
+
const preserveLocalesBeforeChange = preserveLocalesOnPublishAll();
|
|
120
|
+
const restoreLocalesAfterChange = restoreLocalesAfterPublishAll();
|
|
121
|
+
const collectionAfterChange = createCollectionRevalidateAfterChange(dispatcher, hookOptions);
|
|
122
|
+
const collectionAfterDelete = createCollectionRevalidateAfterDelete(dispatcher, hookOptions);
|
|
123
|
+
const captureCollectionBeforeChange = createCaptureCollectionPublicBeforeChange();
|
|
124
|
+
const captureCollectionBeforeDelete = createCaptureCollectionPublicBeforeDelete();
|
|
125
|
+
const captureGlobalBeforeChange = createCaptureGlobalPublicBeforeChange();
|
|
126
|
+
const globalAfterChange = createGlobalRevalidateAfterChange(dispatcher, hookOptions);
|
|
127
|
+
const mediaRevalidationOptions = {
|
|
128
|
+
...mediaHookOptions,
|
|
129
|
+
pluginCollectionExcludeFields: Object.fromEntries(Object.entries(collectionOptions).flatMap(([slug, options]) => options !== true && options.excludeFields ? [[slug, options.excludeFields]] : [])),
|
|
130
|
+
collections: collectionOptions,
|
|
131
|
+
};
|
|
132
|
+
const mediaAfterChange = createRevalidateMediaDependentsAfterChange(dispatcher, mediaRevalidationOptions);
|
|
133
|
+
const mediaBeforeDelete = createRevalidateMediaDependentsBeforeDelete(dispatcher, config.source, mediaRevalidationOptions);
|
|
134
|
+
const collections = (incoming.collections ?? []).map((collection) => {
|
|
135
|
+
if (mediaSlug && collection.slug === mediaSlug) {
|
|
136
|
+
return {
|
|
137
|
+
...collection,
|
|
138
|
+
hooks: {
|
|
139
|
+
...collection.hooks,
|
|
140
|
+
beforeChange: [
|
|
141
|
+
captureCollectionBeforeChange,
|
|
142
|
+
...(collection.hooks?.beforeChange
|
|
143
|
+
? Array.isArray(collection.hooks.beforeChange)
|
|
144
|
+
? collection.hooks.beforeChange
|
|
145
|
+
: [collection.hooks.beforeChange]
|
|
146
|
+
: []),
|
|
147
|
+
],
|
|
148
|
+
afterChange: appendHook(collection.hooks?.afterChange, mediaAfterChange),
|
|
149
|
+
beforeDelete: [
|
|
150
|
+
captureCollectionBeforeDelete,
|
|
151
|
+
...(collection.hooks?.beforeDelete
|
|
152
|
+
? Array.isArray(collection.hooks.beforeDelete)
|
|
153
|
+
? collection.hooks.beforeDelete
|
|
154
|
+
: [collection.hooks.beforeDelete]
|
|
155
|
+
: []),
|
|
156
|
+
mediaBeforeDelete,
|
|
157
|
+
],
|
|
158
|
+
},
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
const meta = collectionOptions[collection.slug];
|
|
162
|
+
if (!meta)
|
|
163
|
+
return collection;
|
|
164
|
+
const opts = normalizeCollectionRevalidation(meta);
|
|
165
|
+
let next = {
|
|
166
|
+
...collection,
|
|
167
|
+
hooks: {
|
|
168
|
+
...collection.hooks,
|
|
169
|
+
beforeOperation: prependHook(collection.hooks?.beforeOperation, mirrorPublishAllLocalesToReq),
|
|
170
|
+
beforeChange: [
|
|
171
|
+
captureCollectionBeforeChange,
|
|
172
|
+
...prependHook(collection.hooks?.beforeChange, preserveLocalesBeforeChange),
|
|
173
|
+
],
|
|
174
|
+
},
|
|
175
|
+
};
|
|
176
|
+
if (opts.hooks.includes('afterChange')) {
|
|
177
|
+
next = {
|
|
178
|
+
...next,
|
|
179
|
+
hooks: {
|
|
180
|
+
...next.hooks,
|
|
181
|
+
afterChange: appendRevalidationAfterChange(next.hooks?.afterChange, restoreLocalesAfterChange, collectionAfterChange),
|
|
182
|
+
},
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
if (opts.hooks.includes('afterDelete')) {
|
|
186
|
+
next = {
|
|
187
|
+
...next,
|
|
188
|
+
hooks: {
|
|
189
|
+
...next.hooks,
|
|
190
|
+
beforeDelete: [
|
|
191
|
+
captureCollectionBeforeDelete,
|
|
192
|
+
...(next.hooks?.beforeDelete
|
|
193
|
+
? Array.isArray(next.hooks.beforeDelete)
|
|
194
|
+
? next.hooks.beforeDelete
|
|
195
|
+
: [next.hooks.beforeDelete]
|
|
196
|
+
: []),
|
|
197
|
+
],
|
|
198
|
+
afterDelete: appendHook(next.hooks?.afterDelete, collectionAfterDelete),
|
|
199
|
+
},
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
return next;
|
|
203
|
+
});
|
|
204
|
+
const globals = (incoming.globals ?? []).map((global) => {
|
|
205
|
+
const meta = globalOptions[global.slug];
|
|
206
|
+
if (!meta)
|
|
207
|
+
return global;
|
|
208
|
+
const opts = normalizeGlobalRevalidation(meta);
|
|
209
|
+
if (!opts.hooks.includes('afterChange'))
|
|
210
|
+
return global;
|
|
211
|
+
return {
|
|
212
|
+
...global,
|
|
213
|
+
hooks: {
|
|
214
|
+
...global.hooks,
|
|
215
|
+
beforeOperation: prependHook(global.hooks?.beforeOperation, mirrorPublishAllLocalesToReq),
|
|
216
|
+
beforeChange: [
|
|
217
|
+
captureGlobalBeforeChange,
|
|
218
|
+
...prependHook(global.hooks?.beforeChange, preserveLocalesBeforeChange),
|
|
219
|
+
],
|
|
220
|
+
afterChange: appendRevalidationAfterChange(global.hooks?.afterChange, restoreLocalesAfterChange, globalAfterChange),
|
|
221
|
+
},
|
|
222
|
+
};
|
|
223
|
+
});
|
|
224
|
+
const nextConfig = {
|
|
225
|
+
...incoming,
|
|
226
|
+
collections,
|
|
227
|
+
globals,
|
|
228
|
+
};
|
|
229
|
+
Object.defineProperty(nextConfig, REVALIDATION_PLUGIN_APPLIED, {
|
|
230
|
+
value: true,
|
|
231
|
+
enumerable: true,
|
|
232
|
+
});
|
|
233
|
+
return nextConfig;
|
|
234
|
+
};
|
|
235
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/** Active admin locale tab (publish-all leaves `req.locale` on the last iterated locale). */
|
|
2
|
+
const asLocaleCode = (value) => typeof value === 'string' && value.length > 0 && value !== 'all' ? value : undefined;
|
|
3
|
+
export const getAdminEditedLocale = (req) => asLocaleCode(req.query?.publishSpecificLocale) ??
|
|
4
|
+
asLocaleCode(req.query?.locale) ??
|
|
5
|
+
asLocaleCode(req.locale);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const localeCodes = (localization) => localization.locales.map((locale) => (typeof locale === 'string' ? locale : locale.code));
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { PayloadRequest } from 'payload';
|
|
2
|
+
type PublicationFlags = {
|
|
3
|
+
publishAllLocales?: boolean;
|
|
4
|
+
unpublishAllLocales?: boolean | string;
|
|
5
|
+
publishSpecificLocale?: string;
|
|
6
|
+
locale?: string | null;
|
|
7
|
+
};
|
|
8
|
+
type MirrorArgs = {
|
|
9
|
+
args?: PublicationFlags & {
|
|
10
|
+
req?: PayloadRequest;
|
|
11
|
+
};
|
|
12
|
+
req: PayloadRequest;
|
|
13
|
+
};
|
|
14
|
+
export declare const mirrorPublishAllLocalesFlagsToReq: () => <TArgs extends NonNullable<MirrorArgs['args']>>(input: MirrorArgs & {
|
|
15
|
+
args?: TArgs;
|
|
16
|
+
}) => (PublicationFlags & {
|
|
17
|
+
req?: PayloadRequest;
|
|
18
|
+
} & TArgs) | undefined;
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const asLocaleCode = (value) => typeof value === 'string' && value.length > 0 && value !== 'all' ? value : undefined;
|
|
2
|
+
const applyPublicationFlagsToReq = (req, flags) => {
|
|
3
|
+
const query = { ...(req.query ?? {}) };
|
|
4
|
+
if (flags.publishAllLocales) {
|
|
5
|
+
query.publishAllLocales = 'true';
|
|
6
|
+
const locale = asLocaleCode(flags.locale) ?? asLocaleCode(req.locale);
|
|
7
|
+
if (locale && !asLocaleCode(query.locale) && !asLocaleCode(query.publishSpecificLocale)) {
|
|
8
|
+
query.locale = locale;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
const unpublish = flags.unpublishAllLocales;
|
|
12
|
+
if (unpublish === true || unpublish === 'true') {
|
|
13
|
+
query.unpublishAllLocales = 'true';
|
|
14
|
+
}
|
|
15
|
+
if (typeof flags.publishSpecificLocale === 'string' && flags.publishSpecificLocale.length > 0) {
|
|
16
|
+
query.publishSpecificLocale = flags.publishSpecificLocale;
|
|
17
|
+
}
|
|
18
|
+
req.query = query;
|
|
19
|
+
};
|
|
20
|
+
export const mirrorPublishAllLocalesFlagsToReq = () => {
|
|
21
|
+
const hook = (input) => {
|
|
22
|
+
const { args, req } = input;
|
|
23
|
+
if (!args)
|
|
24
|
+
return;
|
|
25
|
+
applyPublicationFlagsToReq(req, args);
|
|
26
|
+
if (args.req && args.req !== req) {
|
|
27
|
+
applyPublicationFlagsToReq(args.req, args);
|
|
28
|
+
}
|
|
29
|
+
return input.args;
|
|
30
|
+
};
|
|
31
|
+
return hook;
|
|
32
|
+
};
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { PRESERVE_LOCALES_EDITED_PATCH_CONTEXT_KEY, PRESERVE_LOCALES_SNAPSHOTS_CONTEXT_KEY, } from './context.js';
|
|
2
|
+
import { getAdminEditedLocale } from './edited-locale.js';
|
|
3
|
+
import { localeCodes } from './localization.js';
|
|
4
|
+
import { readLocalizedScalar } from './read-localized-scalar.js';
|
|
5
|
+
import { isPublishAllLocales } from './request-flags.js';
|
|
6
|
+
import { isLocaleRestoreReq } from './restore-guard.js';
|
|
7
|
+
const asLocaleCode = (value) => typeof value === 'string' && value.length > 0 && value !== 'all' ? value : undefined;
|
|
8
|
+
const readRequestLocale = (req) => {
|
|
9
|
+
const fromReq = asLocaleCode(req.locale);
|
|
10
|
+
if (fromReq)
|
|
11
|
+
return fromReq;
|
|
12
|
+
return asLocaleCode(req.query?.publishSpecificLocale) ?? asLocaleCode(req.query?.locale);
|
|
13
|
+
};
|
|
14
|
+
const isLocaleMap = (value, localeCodesList) => {
|
|
15
|
+
if (!value || typeof value !== 'object' || Array.isArray(value))
|
|
16
|
+
return false;
|
|
17
|
+
const record = value;
|
|
18
|
+
const keys = Object.keys(record);
|
|
19
|
+
if (!keys.length)
|
|
20
|
+
return false;
|
|
21
|
+
return keys.every((key) => localeCodesList.includes(key));
|
|
22
|
+
};
|
|
23
|
+
const mergeScalarsFromLocaleDoc = ({ incoming, localeDoc, locale, localeCodesList, }) => {
|
|
24
|
+
const out = { ...incoming };
|
|
25
|
+
for (const [key, value] of Object.entries(out)) {
|
|
26
|
+
const source = localeDoc[key];
|
|
27
|
+
if (isLocaleMap(value, localeCodesList)) {
|
|
28
|
+
if (typeof source === 'string' || source === null) {
|
|
29
|
+
const merged = { ...value };
|
|
30
|
+
if (typeof source === 'string') {
|
|
31
|
+
merged[locale] = source;
|
|
32
|
+
}
|
|
33
|
+
out[key] = merged;
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
if (source && typeof source === 'object' && !Array.isArray(source)) {
|
|
37
|
+
const merged = { ...value };
|
|
38
|
+
const localeEntry = merged[locale];
|
|
39
|
+
if (localeEntry && typeof localeEntry === 'object' && !Array.isArray(localeEntry)) {
|
|
40
|
+
merged[locale] = mergeScalarsFromLocaleDoc({
|
|
41
|
+
incoming: localeEntry,
|
|
42
|
+
localeDoc: source,
|
|
43
|
+
locale,
|
|
44
|
+
localeCodesList,
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
out[key] = merged;
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
if (value &&
|
|
52
|
+
typeof value === 'object' &&
|
|
53
|
+
!Array.isArray(value) &&
|
|
54
|
+
!isLocaleMap(value, localeCodesList) &&
|
|
55
|
+
source &&
|
|
56
|
+
typeof source === 'object' &&
|
|
57
|
+
!Array.isArray(source)) {
|
|
58
|
+
out[key] = mergeScalarsFromLocaleDoc({
|
|
59
|
+
incoming: value,
|
|
60
|
+
localeDoc: source,
|
|
61
|
+
locale,
|
|
62
|
+
localeCodesList,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return out;
|
|
67
|
+
};
|
|
68
|
+
const fetchPublishedLocaleDoc = async (args, locale) => {
|
|
69
|
+
const { req } = args;
|
|
70
|
+
if ('collection' in args) {
|
|
71
|
+
return (await req.payload.findByID({
|
|
72
|
+
collection: args.collection.slug,
|
|
73
|
+
id: args.originalDoc?.id,
|
|
74
|
+
locale: locale,
|
|
75
|
+
fallbackLocale: false,
|
|
76
|
+
depth: 0,
|
|
77
|
+
draft: false,
|
|
78
|
+
overrideAccess: true,
|
|
79
|
+
req,
|
|
80
|
+
}));
|
|
81
|
+
}
|
|
82
|
+
return (await req.payload.findGlobal({
|
|
83
|
+
slug: args.global.slug,
|
|
84
|
+
locale: locale,
|
|
85
|
+
fallbackLocale: false,
|
|
86
|
+
depth: 0,
|
|
87
|
+
draft: false,
|
|
88
|
+
overrideAccess: true,
|
|
89
|
+
req,
|
|
90
|
+
}));
|
|
91
|
+
};
|
|
92
|
+
export const preserveLocalesOnPublishAll = () => {
|
|
93
|
+
const hook = async (args) => {
|
|
94
|
+
const { data, req } = args;
|
|
95
|
+
if (!isPublishAllLocales(req))
|
|
96
|
+
return data;
|
|
97
|
+
if (isLocaleRestoreReq(req))
|
|
98
|
+
return data;
|
|
99
|
+
const localization = req.payload.config.localization;
|
|
100
|
+
if (!localization)
|
|
101
|
+
return data;
|
|
102
|
+
const editedLocale = getAdminEditedLocale(req);
|
|
103
|
+
if (!editedLocale)
|
|
104
|
+
return data;
|
|
105
|
+
const codes = localeCodes(localization);
|
|
106
|
+
const requestLocale = readRequestLocale(req);
|
|
107
|
+
if (requestLocale && requestLocale !== editedLocale) {
|
|
108
|
+
const published = await fetchPublishedLocaleDoc(args, requestLocale);
|
|
109
|
+
return {
|
|
110
|
+
...published,
|
|
111
|
+
_status: data._status ?? published._status,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
let merged = { ...data };
|
|
115
|
+
if (typeof merged.title === 'string') {
|
|
116
|
+
const snapshots = {};
|
|
117
|
+
for (const code of codes) {
|
|
118
|
+
if (code === editedLocale)
|
|
119
|
+
continue;
|
|
120
|
+
const published = await fetchPublishedLocaleDoc(args, code);
|
|
121
|
+
const title = readLocalizedScalar(published, 'title', code);
|
|
122
|
+
snapshots[code] = title ? { title } : published;
|
|
123
|
+
}
|
|
124
|
+
req.context = {
|
|
125
|
+
...(req.context ?? {}),
|
|
126
|
+
[PRESERVE_LOCALES_SNAPSHOTS_CONTEXT_KEY]: snapshots,
|
|
127
|
+
[PRESERVE_LOCALES_EDITED_PATCH_CONTEXT_KEY]: { title: merged.title },
|
|
128
|
+
};
|
|
129
|
+
return merged;
|
|
130
|
+
}
|
|
131
|
+
for (const locale of codes) {
|
|
132
|
+
if (locale === editedLocale)
|
|
133
|
+
continue;
|
|
134
|
+
const localeDoc = await fetchPublishedLocaleDoc(args, locale);
|
|
135
|
+
merged = mergeScalarsFromLocaleDoc({
|
|
136
|
+
incoming: merged,
|
|
137
|
+
localeDoc,
|
|
138
|
+
locale,
|
|
139
|
+
localeCodesList: codes,
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
return merged;
|
|
143
|
+
};
|
|
144
|
+
return hook;
|
|
145
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/** Read a localized scalar from a per-locale Payload document (handles prior corruption). */
|
|
2
|
+
export const readLocalizedScalar = (doc, field, locale) => {
|
|
3
|
+
const value = doc[field];
|
|
4
|
+
if (typeof value === 'string') {
|
|
5
|
+
if (value.startsWith('{')) {
|
|
6
|
+
try {
|
|
7
|
+
const parsed = JSON.parse(value);
|
|
8
|
+
const fromParsed = parsed[locale];
|
|
9
|
+
if (typeof fromParsed === 'string')
|
|
10
|
+
return fromParsed;
|
|
11
|
+
}
|
|
12
|
+
catch {
|
|
13
|
+
// not JSON — use as-is
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
return value;
|
|
17
|
+
}
|
|
18
|
+
if (value && typeof value === 'object' && !Array.isArray(value)) {
|
|
19
|
+
const entry = value[locale];
|
|
20
|
+
if (typeof entry === 'string')
|
|
21
|
+
return entry;
|
|
22
|
+
}
|
|
23
|
+
return undefined;
|
|
24
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** Payload Admin / Local API query flags for publish-all detection. */
|
|
2
|
+
type PublishAllLocalesRequest = {
|
|
3
|
+
query?: Record<string, unknown>;
|
|
4
|
+
context?: Record<string, unknown> | null;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Publish-all detection for hook guards.
|
|
8
|
+
*
|
|
9
|
+
* - Admin / REST: `req.query.publishAllLocales`
|
|
10
|
+
* - Local API: set `req.context.publishAllLocales` when passing `publishAllLocales: true`
|
|
11
|
+
*/
|
|
12
|
+
export declare const isPublishAllLocales: (req: PublishAllLocalesRequest) => boolean;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** Payload Admin / Local API query flags for publish-all detection. */
|
|
2
|
+
const isTruthyFlag = (value) => value === 'true' || value === true;
|
|
3
|
+
/**
|
|
4
|
+
* Publish-all detection for hook guards.
|
|
5
|
+
*
|
|
6
|
+
* - Admin / REST: `req.query.publishAllLocales`
|
|
7
|
+
* - Local API: set `req.context.publishAllLocales` when passing `publishAllLocales: true`
|
|
8
|
+
*/
|
|
9
|
+
export const isPublishAllLocales = (req) => isTruthyFlag(req.query?.publishAllLocales) || isTruthyFlag(req.context?.publishAllLocales);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tracks request objects created internally for locale restoration writes.
|
|
3
|
+
*
|
|
4
|
+
* Using a WeakSet (rather than a user-controllable `req.context` flag) prevents
|
|
5
|
+
* external or unrelated callers from suppressing required revalidation by
|
|
6
|
+
* setting `context.preserveLocalesRestore`.
|
|
7
|
+
*/
|
|
8
|
+
const restoreReqs = new WeakSet();
|
|
9
|
+
export const markLocaleRestoreReq = (req) => {
|
|
10
|
+
restoreReqs.add(req);
|
|
11
|
+
};
|
|
12
|
+
export const isLocaleRestoreReq = (req) => typeof req === 'object' && req !== null && restoreReqs.has(req);
|