@justanarthur/payload-www 0.3.5 → 1.2.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.
Files changed (63) hide show
  1. package/README.md +317 -307
  2. package/dist/access.d.ts +2 -2
  3. package/dist/access.js +3 -17
  4. package/dist/blocks.d.ts +7 -12
  5. package/dist/blocks.js +24 -43
  6. package/dist/cli.js +201 -0
  7. package/dist/collections.d.ts +33 -225
  8. package/dist/collections.js +185 -639
  9. package/dist/config.d.ts +19 -105
  10. package/dist/config.js +257 -855
  11. package/dist/fields.d.ts +4 -46
  12. package/dist/fields.js +177 -53
  13. package/dist/imagehash.js +0 -15
  14. package/dist/import-map-provider.d.ts +0 -16
  15. package/dist/import-map-provider.js +0 -15
  16. package/dist/metadata.d.ts +24 -89
  17. package/dist/metadata.js +123 -255
  18. package/dist/pages.d.ts +37 -319
  19. package/dist/pages.js +349 -964
  20. package/dist/render-pages.d.ts +70 -565
  21. package/dist/render-pages.js +464 -1477
  22. package/dist/sitemap.d.ts +10 -0
  23. package/dist/sitemap.js +337 -0
  24. package/dist/translator.js +0 -15
  25. package/dist/utils.d.ts +10 -12
  26. package/dist/utils.js +18 -41
  27. package/package.json +81 -178
  28. package/dist/components.d.ts +0 -6
  29. package/dist/components.js +0 -39
  30. package/dist/core-access.d.ts +0 -11
  31. package/dist/core-access.js +0 -34
  32. package/dist/core-blocks.d.ts +0 -24
  33. package/dist/core-blocks.js +0 -79
  34. package/dist/core-fields.d.ts +0 -73
  35. package/dist/core-fields.js +0 -167
  36. package/dist/core-utils.d.ts +0 -16
  37. package/dist/core-utils.js +0 -63
  38. package/dist/data-collections.d.ts +0 -229
  39. package/dist/data-collections.js +0 -676
  40. package/dist/data-seed.d.ts +0 -76
  41. package/dist/data-seed.js +0 -212
  42. package/dist/data-test.d.ts +0 -30
  43. package/dist/data-test.js +0 -1110
  44. package/dist/globals.d.ts +0 -69
  45. package/dist/globals.js +0 -251
  46. package/dist/hooks.d.ts +0 -108
  47. package/dist/hooks.js +0 -228
  48. package/dist/index.d.ts +0 -6
  49. package/dist/index.js +0 -39
  50. package/dist/render-components.d.ts +0 -42
  51. package/dist/render-components.js +0 -154
  52. package/dist/render-metadata.d.ts +0 -122
  53. package/dist/render-metadata.js +0 -390
  54. package/dist/render-utils.d.ts +0 -166
  55. package/dist/render-utils.js +0 -398
  56. package/dist/seed.d.ts +0 -76
  57. package/dist/seed.js +0 -212
  58. package/dist/server.d.ts +0 -980
  59. package/dist/server.js +0 -2231
  60. package/dist/test.d.ts +0 -30
  61. package/dist/test.js +0 -1110
  62. package/dist/with-www-config.d.ts +0 -108
  63. package/dist/with-www-config.js +0 -1006
package/dist/globals.d.ts DELETED
@@ -1,69 +0,0 @@
1
- import { Field, GlobalConfig } from "payload";
2
- type CreateHeaderGlobalOptions = {
3
- /**
4
- * Optional override for the `custom.path` Payload uses to resolve
5
- * the header's render module. Default: the lib's `HEADER_RENDER_PATH`.
6
- */
7
- renderPath?: string;
8
- /**
9
- * Collection slugs the nav's link fields can reference. Required
10
- * by Payload 3.85 — empty `relationTo` arrays are rejected.
11
- * Defaults to `['pages']` (the lib's Pages collection). Pass a
12
- * different list when your host has additional linkable collections.
13
- */
14
- linkRelationTo?: string[];
15
- /**
16
- * Extra fields appended to each `navColumn` link — e.g. a
17
- * `description` text field or a `navHover` group for a mega-menu.
18
- * Lets hosts enrich the column-link schema without redefining the
19
- * whole nav.
20
- */
21
- navColumnLinkFields?: Field[];
22
- /**
23
- * Extra fields appended to each top-level `navItem` link.
24
- */
25
- navItemLinkFields?: Field[];
26
- };
27
- /**
28
- * Build the Header global. The lib's default shape: a `nav` blocks
29
- * field with two block types — `navColumn` (a titled group of links)
30
- * and `navItem` (a single link).
31
- */
32
- declare const createHeaderGlobal: (options?: CreateHeaderGlobalOptions) => GlobalConfig;
33
- import { Field as Field2, GlobalConfig as GlobalConfig2 } from "payload";
34
- type CreateFooterGlobalOptions = {
35
- /**
36
- * Optional override for the `custom.path` Payload uses to resolve
37
- * the footer's render module. Default: the lib's `FOOTER_RENDER_PATH`.
38
- */
39
- renderPath?: string;
40
- /**
41
- * Collection slugs the nav's link fields can reference. Required
42
- * by Payload 3.85 — empty `relationTo` arrays are rejected.
43
- * Defaults to `['pages']` (the lib's Pages collection). Pass a
44
- * different list when your host has additional linkable collections.
45
- */
46
- linkRelationTo?: string[];
47
- /**
48
- * Extra fields appended to each `navColumn` link — e.g. a
49
- * `description` text field. Lets hosts enrich the column-link
50
- * schema without redefining the whole nav.
51
- */
52
- navColumnLinkFields?: Field2[];
53
- /**
54
- * Extra fields appended to each top-level `navItem` link.
55
- */
56
- navItemLinkFields?: Field2[];
57
- };
58
- /**
59
- * Build the Footer global. The lib's default shape mirrors the
60
- * Header: a `nav` blocks field with `navColumn` (a titled group of
61
- * links) and `navItem` (a single link) blocks.
62
- */
63
- declare const createFooterGlobal: (options?: CreateFooterGlobalOptions) => GlobalConfig2;
64
- import { GlobalConfig as GlobalConfig_fuin17 } from "payload";
65
- declare const _default: {
66
- createHeaderGlobal: (options?: CreateHeaderGlobalOptions) => GlobalConfig_fuin17;
67
- createFooterGlobal: (options?: CreateFooterGlobalOptions) => GlobalConfig_fuin17;
68
- };
69
- export { _default as default, createHeaderGlobal, createFooterGlobal, CreateHeaderGlobalOptions, CreateFooterGlobalOptions };
package/dist/globals.js DELETED
@@ -1,251 +0,0 @@
1
-
2
- var __defProp = Object.defineProperty;
3
- var __returnValue = (v) => v;
4
- function __exportSetter(name, newValue) {
5
- this[name] = __returnValue.bind(null, newValue);
6
- }
7
- var __export = (target, all) => {
8
- for (var name in all)
9
- __defProp(target, name, {
10
- get: all[name],
11
- enumerable: true,
12
- configurable: true,
13
- set: __exportSetter.bind(all, name)
14
- });
15
- };
16
- var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
17
-
18
- // src/config/constants.ts
19
- var PAGES_RENDER_PATH = "@justanarthur/payload-www/render-pages#PagesPage";
20
- var POSTS_RENDER_PATH = "@justanarthur/payload-www/render-pages#PostsPage";
21
- var HEADER_RENDER_PATH = "@justanarthur/payload-www/render-pages#HeaderPage";
22
- var FOOTER_RENDER_PATH = "@justanarthur/payload-www/render-pages#FooterPage";
23
- var LIVE_PREVIEW_LISTENER_PATH = "@justanarthur/payload-www/render-components#LivePreviewListener";
24
- var PAGES_SLUG = "pages";
25
- var POSTS_SLUG = "posts";
26
- var STATIC_PAGES_SLUG = "staticPages";
27
-
28
- // src/core/fields/link.ts
29
- var appearanceOptions = {
30
- default: { label: "Default", value: "default" },
31
- outline: { label: "Outline", value: "outline" }
32
- };
33
- var link = (options = {}) => {
34
- const {
35
- appearances,
36
- disableLabel = false,
37
- relationTo = [PAGES_SLUG],
38
- localized = false,
39
- overrides = {},
40
- extraFields = []
41
- } = options;
42
- const result = {
43
- name: "link",
44
- type: "group",
45
- admin: { hideGutter: true },
46
- fields: [
47
- {
48
- type: "row",
49
- fields: [
50
- {
51
- name: "type",
52
- type: "radio",
53
- admin: { layout: "horizontal", width: "50%" },
54
- defaultValue: "reference",
55
- options: [
56
- { label: "Internal link", value: "reference" },
57
- { label: "Custom URL", value: "custom" }
58
- ]
59
- },
60
- {
61
- name: "newTab",
62
- type: "checkbox",
63
- admin: { style: { alignSelf: "flex-end" }, width: "50%" },
64
- label: "Open in new tab"
65
- }
66
- ]
67
- }
68
- ]
69
- };
70
- const linkTypes = [
71
- {
72
- name: "reference",
73
- type: "relationship",
74
- admin: { condition: (_, siblingData) => siblingData?.type === "reference" },
75
- label: "Document to link to",
76
- relationTo,
77
- required: true,
78
- localized
79
- },
80
- {
81
- name: "url",
82
- type: "text",
83
- admin: { condition: (_, siblingData) => siblingData?.type === "custom" },
84
- label: "Custom URL",
85
- required: true,
86
- localized
87
- }
88
- ];
89
- if (!disableLabel) {
90
- result.fields.push({
91
- type: "row",
92
- fields: [
93
- ...linkTypes,
94
- {
95
- name: "label",
96
- type: "text",
97
- admin: { width: "50%" },
98
- label: "Label",
99
- required: true,
100
- localized
101
- }
102
- ]
103
- });
104
- } else {
105
- result.fields = [...result.fields, ...linkTypes];
106
- }
107
- if (appearances !== false) {
108
- const opts = appearances ? appearances.map((a) => appearanceOptions[a]) : [appearanceOptions.default, appearanceOptions.outline];
109
- result.fields.push({
110
- name: "appearance",
111
- type: "select",
112
- admin: { description: "Choose how the link should be rendered." },
113
- defaultValue: "default",
114
- options: opts
115
- });
116
- }
117
- if (extraFields.length) {
118
- result.fields.push(...extraFields);
119
- }
120
- return { ...result, ...overrides };
121
- };
122
-
123
- // src/render/hooks/_shared.ts
124
- var cachePromise = null;
125
- function nextCacheImport() {
126
- return cachePromise ??= import("next/cache");
127
- }
128
- function shouldSkipRevalidate(context) {
129
- const skip = Boolean(context?.disableRevalidate);
130
- if (skip)
131
- console.log("[WWW] render/hooks:_shared:shouldSkipRevalidate -> skip");
132
- return skip;
133
- }
134
- async function safeRevalidatePath(payload, path) {
135
- console.log("[WWW] render/hooks:_shared:safeRevalidatePath path=", path);
136
- try {
137
- const { revalidatePath } = await nextCacheImport();
138
- revalidatePath(path);
139
- } catch (error) {
140
- console.error("[WWW] render/hooks:_shared:safeRevalidatePath failed path=", path, "err=", String(error));
141
- payload.logger.error(`revalidatePath("${path}") failed: ${String(error)}`);
142
- }
143
- }
144
- async function safeRevalidateTag(payload, tag, profile = "max") {
145
- console.log("[WWW] render/hooks:_shared:safeRevalidateTag tag=", tag, "profile=", profile);
146
- try {
147
- const { revalidateTag } = await nextCacheImport();
148
- revalidateTag(tag, profile);
149
- } catch (error) {
150
- console.error("[WWW] render/hooks:_shared:safeRevalidateTag failed tag=", tag, "err=", String(error));
151
- payload.logger.error(`revalidateTag("${tag}") failed: ${String(error)}`);
152
- }
153
- }
154
-
155
- // src/render/hooks/revalidateGlobal.ts
156
- function createRevalidateGlobalHook(slug) {
157
- console.log("[WWW] render/hooks:createRevalidateGlobalHook slug=", slug);
158
- return async ({ doc, req: { payload, context, locale } }) => {
159
- if (shouldSkipRevalidate(context))
160
- return doc;
161
- const tags = [`global_${slug}`, `global_${slug}_${locale}`];
162
- console.log("[WWW] render/hooks:revalidateGlobal slug=", slug, "locale=", locale, "tags=", JSON.stringify(tags));
163
- payload.logger.info?.(`Revalidating global: ${tags.join(", ")}`);
164
- for (const tag of tags) {
165
- await safeRevalidateTag(payload, tag);
166
- }
167
- return doc;
168
- };
169
- }
170
-
171
- // src/data/collections/globals/Header/config.ts
172
- var createHeaderGlobal = (options = {}) => {
173
- const {
174
- renderPath = HEADER_RENDER_PATH,
175
- linkRelationTo = [PAGES_SLUG],
176
- navColumnLinkFields = [],
177
- navItemLinkFields = []
178
- } = options;
179
- const navColumnBlock = {
180
- slug: "navColumn",
181
- fields: [
182
- { name: "title", type: "text", required: true, localized: true },
183
- { name: "links", type: "array", fields: [link({ appearances: false, localized: true, relationTo: linkRelationTo, extraFields: navColumnLinkFields })] }
184
- ]
185
- };
186
- const navItemBlock = {
187
- slug: "navItem",
188
- fields: [link({ appearances: false, localized: true, relationTo: linkRelationTo, extraFields: navItemLinkFields })]
189
- };
190
- return {
191
- slug: "header",
192
- custom: { path: renderPath },
193
- access: { read: () => true },
194
- fields: [
195
- {
196
- name: "nav",
197
- type: "blocks",
198
- required: true,
199
- blocks: [navColumnBlock, navItemBlock]
200
- }
201
- ],
202
- hooks: {
203
- afterChange: [createRevalidateGlobalHook("header")]
204
- }
205
- };
206
- };
207
-
208
- // src/data/collections/globals/Footer/config.ts
209
- var createFooterGlobal = (options = {}) => {
210
- const {
211
- renderPath = FOOTER_RENDER_PATH,
212
- linkRelationTo = [PAGES_SLUG],
213
- navColumnLinkFields = [],
214
- navItemLinkFields = []
215
- } = options;
216
- const navColumnBlock = {
217
- slug: "navColumn",
218
- fields: [
219
- { name: "title", type: "text", required: true, localized: true },
220
- { name: "links", type: "array", fields: [link({ appearances: false, localized: true, relationTo: linkRelationTo, extraFields: navColumnLinkFields })] }
221
- ]
222
- };
223
- const navItemBlock = {
224
- slug: "navItem",
225
- fields: [link({ appearances: false, localized: true, relationTo: linkRelationTo, extraFields: navItemLinkFields })]
226
- };
227
- return {
228
- slug: "footer",
229
- custom: { path: renderPath },
230
- access: { read: () => true },
231
- fields: [
232
- {
233
- name: "nav",
234
- type: "blocks",
235
- required: true,
236
- blocks: [navColumnBlock, navItemBlock]
237
- }
238
- ],
239
- hooks: {
240
- afterChange: [createRevalidateGlobalHook("footer")]
241
- }
242
- };
243
- };
244
-
245
- // src/exports/globals.ts
246
- var globals_default = { createHeaderGlobal, createFooterGlobal };
247
- export {
248
- globals_default as default,
249
- createHeaderGlobal,
250
- createFooterGlobal
251
- };
package/dist/hooks.d.ts DELETED
@@ -1,108 +0,0 @@
1
- import { CollectionAfterChangeHook, CollectionAfterDeleteHook } from "payload";
2
- type LocalePrefixMode = "always" | "as-needed" | "never";
3
- type CreateRevalidateCollectionHookOptions = {
4
- /**
5
- * The collection's slug. Used in the per-collection tag fired
6
- * alongside path invalidation: `collection_<slug>_<id>`.
7
- */
8
- collectionSlug: string;
9
- /**
10
- * URL path prefix for this collection's rendered URLs. Pages use
11
- * `''` (mounted at root); Posts use `'/posts'`. Leading slash,
12
- * no trailing slash — same convention as
13
- * `createSitemapFile.urlPrefixes`.
14
- */
15
- urlPathPrefix?: string;
16
- /**
17
- * Sitemap tag the collection's revalidation hook invalidates on
18
- * every save. Defaults to `${collectionSlug}-sitemap`. Pass
19
- * `false` to opt out (collections without a sitemap handler).
20
- */
21
- sitemapTag?: string | false;
22
- /**
23
- * Locale prefix mode — mirrors next-intl's `localePrefix`.
24
- * Defaults to `'always'` (preserves legacy behavior).
25
- */
26
- localePrefix?: LocalePrefixMode;
27
- /**
28
- * Default locale for `localePrefix: 'as-needed'`. Falls back to
29
- * `req.payload.config.localization.defaultLocale` at request time
30
- * when omitted.
31
- */
32
- defaultLocale?: string;
33
- /**
34
- * Path invalidation strategy. Default: `'url'` — fans out
35
- * `revalidatePath` for every locale × every slug. Set to
36
- * `'tag-only'` for collections without a URL (e.g. `staticPages`
37
- * — addressed by a `kind` discriminator, not a slug). Tag-only
38
- * mode still fires the per-collection tag (via `sitemapTag`) and
39
- * the per-id tag (`collection_<slug>_<id>`).
40
- */
41
- pathMode?: "url" | "tag-only";
42
- };
43
- /**
44
- * Canonical factory for **all** collection revalidation hooks —
45
- * Pages, Posts, and any host-defined collection.
46
- *
47
- * Behavior:
48
- *
49
- * - Fires `revalidatePath` for **every** locale the host has
50
- * declared (not just the request locale). The path is
51
- * `${prefixFor(locale)}${urlPathPrefix}/${slug}` (or
52
- * `${prefixFor(locale)}${urlPathPrefix}` for the home page with
53
- * empty slug).
54
- * - Fires `revalidateTag(sitemapTag, 'max')` on every change /
55
- * delete so the sitemap handler sees fresh data.
56
- * - Fires `revalidateTag('collection_<slug>_<id>', 'max')` so
57
- * hosts using `unstable_cache` keyed by id stay fresh.
58
- * - Handles slug renames while published (fires both old and new
59
- * paths across all locales).
60
- * - Respects `req.context.disableRevalidate` for seed / test
61
- * scripts.
62
- *
63
- * Use `createRevalidateCollectionHook({ ... })` for any
64
- * collection. For Pages specifically, the lib still exposes
65
- * `createRevalidatePageHooks()` as a deprecated alias that
66
- * delegates to this factory with `collectionSlug: 'pages'` and
67
- * `urlPathPrefix: ''`.
68
- */
69
- declare function createRevalidateCollectionHook(options: CreateRevalidateCollectionHookOptions): {
70
- afterChange: CollectionAfterChangeHook;
71
- afterDelete: CollectionAfterDeleteHook;
72
- };
73
- type CreateRevalidatePageHooksOptions = Omit<CreateRevalidateCollectionHookOptions, "collectionSlug" | "urlPathPrefix">;
74
- /**
75
- * @deprecated Use `createRevalidateCollectionHook({ collectionSlug: 'pages', urlPathPrefix: '' })` instead.
76
- * Kept so hosts that imported this name directly don't break — the
77
- * alias delegates to the canonical factory with the Pages preset.
78
- */
79
- declare const createRevalidatePageHooks: (opts?: CreateRevalidatePageHooksOptions) => {
80
- afterChange: CollectionAfterChangeHook;
81
- afterDelete: CollectionAfterDeleteHook;
82
- };
83
- import { GlobalAfterChangeHook } from "payload";
84
- /**
85
- * Build a tag-based revalidation hook for a global. Fires two tags
86
- * after a save:
87
- *
88
- * - `global_${slug}` — invalidates the lib's `getCachedGlobal` cache
89
- * and any host caches that key by slug alone.
90
- * - `global_${slug}_${locale}` — invalidates per-locale cache slots
91
- * for hosts that key by `[slug, locale]`.
92
- *
93
- * The hook fires both so the lib's single-slot cache and the
94
- * per-locale pattern both stay fresh regardless of which the host
95
- * uses.
96
- */
97
- declare function createRevalidateGlobalHook(slug: string): GlobalAfterChangeHook;
98
- import { CollectionAfterChangeHook as CollectionAfterChangeHook_qdlahd } from "payload";
99
- import { CollectionAfterDeleteHook as CollectionAfterDeleteHook_jscwy4 } from "payload";
100
- declare const _default: {
101
- createRevalidateCollectionHook: typeof createRevalidateCollectionHook;
102
- createRevalidatePageHooks: (opts?: CreateRevalidatePageHooksOptions) => {
103
- afterChange: CollectionAfterChangeHook_qdlahd;
104
- afterDelete: CollectionAfterDeleteHook_jscwy4;
105
- };
106
- createRevalidateGlobalHook: typeof createRevalidateGlobalHook;
107
- };
108
- export { _default as default, createRevalidatePageHooks, createRevalidateGlobalHook, createRevalidateCollectionHook, CreateRevalidatePageHooksOptions, CreateRevalidateCollectionHookOptions };
package/dist/hooks.js DELETED
@@ -1,228 +0,0 @@
1
-
2
- var __defProp = Object.defineProperty;
3
- var __returnValue = (v) => v;
4
- function __exportSetter(name, newValue) {
5
- this[name] = __returnValue.bind(null, newValue);
6
- }
7
- var __export = (target, all) => {
8
- for (var name in all)
9
- __defProp(target, name, {
10
- get: all[name],
11
- enumerable: true,
12
- configurable: true,
13
- set: __exportSetter.bind(all, name)
14
- });
15
- };
16
- var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
17
-
18
- // src/render/_locale.ts
19
- function prefixFor(locale, defaultLocale, mode) {
20
- let result;
21
- if (mode === "never")
22
- result = "";
23
- else if (mode === "as-needed" && locale === defaultLocale)
24
- result = "";
25
- else
26
- result = `/${locale}`;
27
- console.log("[WWW] render/_locale:prefixFor locale=", locale, "default=", defaultLocale, "mode=", mode, "->", JSON.stringify(result));
28
- return result;
29
- }
30
- function resolveLocale(req) {
31
- if (!req || typeof req !== "object") {
32
- console.log('[WWW] render/_locale:resolveLocale -> "" (no req)');
33
- return "";
34
- }
35
- const r = req;
36
- if (typeof r.locale === "string" && r.locale.length > 0) {
37
- console.log("[WWW] render/_locale:resolveLocale ->", r.locale, "(from req.locale)");
38
- return r.locale;
39
- }
40
- const fallback = r.payload?.config?.localization?.defaultLocale;
41
- if (typeof fallback === "string" && fallback.length > 0) {
42
- console.log("[WWW] render/_locale:resolveLocale ->", fallback, "(from config.localization.defaultLocale)");
43
- return fallback;
44
- }
45
- console.log('[WWW] render/_locale:resolveLocale -> "" (no locale anywhere)');
46
- return "";
47
- }
48
- function allLocales(req) {
49
- if (!req || typeof req !== "object") {
50
- console.log("[WWW] render/_locale:allLocales -> [] (no req)");
51
- return [];
52
- }
53
- const r = req;
54
- const list = r.payload?.config?.localization?.locales;
55
- if (!Array.isArray(list) || list.length === 0) {
56
- console.log("[WWW] render/_locale:allLocales -> [] (no locales declared)");
57
- return [];
58
- }
59
- const out = [];
60
- for (const entry of list) {
61
- if (typeof entry === "string" && entry.length > 0) {
62
- out.push(entry);
63
- } else if (entry && typeof entry === "object" && "code" in entry) {
64
- const code = entry.code;
65
- if (typeof code === "string" && code.length > 0)
66
- out.push(code);
67
- }
68
- }
69
- console.log("[WWW] render/_locale:allLocales ->", JSON.stringify(out));
70
- return out;
71
- }
72
-
73
- // src/render/hooks/_shared.ts
74
- var cachePromise = null;
75
- function nextCacheImport() {
76
- return cachePromise ??= import("next/cache");
77
- }
78
- function shouldSkipRevalidate(context) {
79
- const skip = Boolean(context?.disableRevalidate);
80
- if (skip)
81
- console.log("[WWW] render/hooks:_shared:shouldSkipRevalidate -> skip");
82
- return skip;
83
- }
84
- async function safeRevalidatePath(payload, path) {
85
- console.log("[WWW] render/hooks:_shared:safeRevalidatePath path=", path);
86
- try {
87
- const { revalidatePath } = await nextCacheImport();
88
- revalidatePath(path);
89
- } catch (error) {
90
- console.error("[WWW] render/hooks:_shared:safeRevalidatePath failed path=", path, "err=", String(error));
91
- payload.logger.error(`revalidatePath("${path}") failed: ${String(error)}`);
92
- }
93
- }
94
- async function safeRevalidateTag(payload, tag, profile = "max") {
95
- console.log("[WWW] render/hooks:_shared:safeRevalidateTag tag=", tag, "profile=", profile);
96
- try {
97
- const { revalidateTag } = await nextCacheImport();
98
- revalidateTag(tag, profile);
99
- } catch (error) {
100
- console.error("[WWW] render/hooks:_shared:safeRevalidateTag failed tag=", tag, "err=", String(error));
101
- payload.logger.error(`revalidateTag("${tag}") failed: ${String(error)}`);
102
- }
103
- }
104
-
105
- // src/render/hooks/revalidateCollection.ts
106
- function createRevalidateCollectionHook(options) {
107
- const {
108
- collectionSlug,
109
- urlPathPrefix = "",
110
- sitemapTag,
111
- localePrefix: modeOption,
112
- defaultLocale: defaultLocaleOption,
113
- pathMode = "url"
114
- } = options;
115
- const resolvedSitemapTag = sitemapTag === false ? false : sitemapTag ?? `${collectionSlug}-sitemap`;
116
- console.log("[WWW] render/hooks:createRevalidateCollectionHook collectionSlug=", collectionSlug, "urlPathPrefix=", urlPathPrefix, "sitemapTag=", resolvedSitemapTag, "pathMode=", pathMode);
117
- const resolveDefaults = (req) => {
118
- const mode = modeOption ?? "always";
119
- const defaultLocale = defaultLocaleOption ?? req?.payload?.config?.localization?.defaultLocale ?? "";
120
- return { mode, defaultLocale };
121
- };
122
- const localesToFanOut = (req) => {
123
- const all = allLocales(req);
124
- if (all.length > 0)
125
- return all;
126
- const single = resolveLocale(req);
127
- return single ? [single] : [];
128
- };
129
- const collectionPath = (slug, locale, mode, defaultLocale) => {
130
- const prefix = prefixFor(locale, defaultLocale, mode);
131
- if (!slug)
132
- return `${prefix}${urlPathPrefix}` || "/";
133
- return `${prefix}${urlPathPrefix}/${slug}`;
134
- };
135
- const fanOutPaths = async (payload, req, slug, label) => {
136
- const { mode, defaultLocale } = resolveDefaults(req);
137
- const locales = localesToFanOut(req);
138
- for (const locale of locales) {
139
- const path = collectionPath(slug, locale, mode, defaultLocale);
140
- payload.logger.info?.(`${label} ${path}`);
141
- await safeRevalidatePath(payload, path);
142
- }
143
- };
144
- const fireCollectionTags = async (payload, docId, req) => {
145
- console.log("[WWW] render/hooks:fireCollectionTags collectionSlug=", collectionSlug, "docId=", docId);
146
- if (typeof docId === "string" || typeof docId === "number") {
147
- await safeRevalidateTag(payload, `collection_${collectionSlug}_${docId}`);
148
- }
149
- if (resolvedSitemapTag !== false) {
150
- await safeRevalidateTag(payload, resolvedSitemapTag);
151
- }
152
- };
153
- const afterChange = async ({ doc, previousDoc, req }) => {
154
- if (shouldSkipRevalidate(req.context))
155
- return doc;
156
- const { payload } = req;
157
- const typed = doc;
158
- const prev = previousDoc;
159
- console.log("[WWW] render/hooks:afterChange collectionSlug=", collectionSlug, "id=", typed.id, "slug=", typed.slug, "prevSlug=", prev?.slug);
160
- const isPublished = typed._status === "published";
161
- const wasPublished = prev?._status === "published";
162
- const prevSlugIsString = typeof prev?.slug === "string";
163
- const slugChanged = prevSlugIsString && prev?.slug !== typed.slug;
164
- if (isPublished) {
165
- if (pathMode !== "tag-only") {
166
- await fanOutPaths(payload, req, typed.slug, `Revalidating ${collectionSlug} at path:`);
167
- }
168
- await fireCollectionTags(payload, typed.id, req);
169
- }
170
- if (wasPublished && (!isPublished || slugChanged)) {
171
- if (pathMode !== "tag-only") {
172
- await fanOutPaths(payload, req, prev?.slug, `Revalidating old ${collectionSlug} at path:`);
173
- }
174
- await fireCollectionTags(payload, typed.id, req);
175
- }
176
- return doc;
177
- };
178
- const afterDelete = async ({ doc, req }) => {
179
- if (shouldSkipRevalidate(req.context))
180
- return doc ?? null;
181
- const { payload } = req;
182
- const typed = doc;
183
- console.log("[WWW] render/hooks:afterDelete collectionSlug=", collectionSlug, "id=", typed?.id, "slug=", typed?.slug);
184
- if (pathMode !== "tag-only") {
185
- await fanOutPaths(payload, req, typed?.slug, `Revalidating deleted ${collectionSlug} at path:`);
186
- }
187
- await fireCollectionTags(payload, typed?.id, req);
188
- return doc ?? null;
189
- };
190
- return { afterChange, afterDelete };
191
- }
192
- var createRevalidatePageHooks = (opts = {}) => {
193
- console.log("[WWW] render/hooks:createRevalidatePageHooks (deprecated alias) opts=", JSON.stringify(opts));
194
- return createRevalidateCollectionHook({
195
- collectionSlug: "pages",
196
- urlPathPrefix: "",
197
- ...opts
198
- });
199
- };
200
-
201
- // src/render/hooks/revalidateGlobal.ts
202
- function createRevalidateGlobalHook(slug) {
203
- console.log("[WWW] render/hooks:createRevalidateGlobalHook slug=", slug);
204
- return async ({ doc, req: { payload, context, locale } }) => {
205
- if (shouldSkipRevalidate(context))
206
- return doc;
207
- const tags = [`global_${slug}`, `global_${slug}_${locale}`];
208
- console.log("[WWW] render/hooks:revalidateGlobal slug=", slug, "locale=", locale, "tags=", JSON.stringify(tags));
209
- payload.logger.info?.(`Revalidating global: ${tags.join(", ")}`);
210
- for (const tag of tags) {
211
- await safeRevalidateTag(payload, tag);
212
- }
213
- return doc;
214
- };
215
- }
216
-
217
- // src/exports/hooks.ts
218
- var hooks_default = {
219
- createRevalidateCollectionHook,
220
- createRevalidatePageHooks,
221
- createRevalidateGlobalHook
222
- };
223
- export {
224
- hooks_default as default,
225
- createRevalidatePageHooks,
226
- createRevalidateGlobalHook,
227
- createRevalidateCollectionHook
228
- };
package/dist/index.d.ts DELETED
@@ -1,6 +0,0 @@
1
- import { FC } from "react";
2
- type LivePreviewListenerProps = {
3
- serverURL?: string;
4
- };
5
- declare const LivePreviewListener: FC<LivePreviewListenerProps>;
6
- export { LivePreviewListener as default, LivePreviewListenerProps, LivePreviewListener };