@foundrykit/seo-plugin 0.0.0-stage → 0.3.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/README.md +270 -2
- package/dist/components/JsonLdEditor.d.ts +2 -0
- package/dist/components/JsonLdEditor.js +1184 -0
- package/dist/components/JsonLdEditor.js.map +1 -0
- package/dist/components/JsonLdTemplateRowLabel.d.ts +2 -0
- package/dist/components/JsonLdTemplateRowLabel.js +12 -0
- package/dist/components/JsonLdTemplateRowLabel.js.map +1 -0
- package/dist/components/MetaDescriptionComponent.d.ts +8 -0
- package/dist/components/MetaDescriptionComponent.js +101 -0
- package/dist/components/MetaDescriptionComponent.js.map +1 -0
- package/dist/components/MetaTitleComponent.d.ts +9 -0
- package/dist/components/MetaTitleComponent.js +193 -0
- package/dist/components/MetaTitleComponent.js.map +1 -0
- package/dist/components/OverviewComponent.d.ts +8 -0
- package/dist/components/OverviewComponent.js +64 -0
- package/dist/components/OverviewComponent.js.map +1 -0
- package/dist/components/PreviewComponent.d.ts +8 -0
- package/dist/components/PreviewComponent.js +51 -0
- package/dist/components/PreviewComponent.js.map +1 -0
- package/dist/exports/client.d.ts +6 -0
- package/dist/exports/client.js +8 -0
- package/dist/exports/client.js.map +1 -0
- package/dist/exports/next.d.ts +2 -0
- package/dist/exports/next.js +3 -0
- package/dist/exports/next.js.map +1 -0
- package/dist/exports/rsc.d.ts +1 -0
- package/dist/exports/rsc.js +5 -0
- package/dist/exports/rsc.js.map +1 -0
- package/dist/fields.d.ts +67 -0
- package/dist/fields.js +220 -0
- package/dist/fields.js.map +1 -0
- package/dist/global.d.ts +6 -0
- package/dist/global.js +93 -0
- package/dist/global.js.map +1 -0
- package/dist/helpers.d.ts +44 -0
- package/dist/helpers.js +23 -0
- package/dist/helpers.js.map +1 -0
- package/dist/helpers.test.d.ts +1 -0
- package/dist/helpers.test.js +234 -0
- package/dist/helpers.test.js.map +1 -0
- package/dist/hooks/autoAlternates.d.ts +18 -0
- package/dist/hooks/autoAlternates.js +63 -0
- package/dist/hooks/autoAlternates.js.map +1 -0
- package/dist/hooks/autoAlternates.test.d.ts +1 -0
- package/dist/hooks/autoAlternates.test.js +225 -0
- package/dist/hooks/autoAlternates.test.js.map +1 -0
- package/dist/hooks/generateMeta.d.ts +17 -0
- package/dist/hooks/generateMeta.js +58 -0
- package/dist/hooks/generateMeta.js.map +1 -0
- package/dist/hooks/generateMeta.test.d.ts +1 -0
- package/dist/hooks/generateMeta.test.js +132 -0
- package/dist/hooks/generateMeta.test.js.map +1 -0
- package/dist/hooks/ogImageGenerator.d.ts +19 -0
- package/dist/hooks/ogImageGenerator.js +96 -0
- package/dist/hooks/ogImageGenerator.js.map +1 -0
- package/dist/index.d.ts +87 -0
- package/dist/index.js +269 -0
- package/dist/index.js.map +1 -0
- package/dist/jsonld.d.ts +83 -0
- package/dist/jsonld.js +179 -0
- package/dist/jsonld.js.map +1 -0
- package/dist/jsonld.test.d.ts +1 -0
- package/dist/jsonld.test.js +214 -0
- package/dist/jsonld.test.js.map +1 -0
- package/dist/metadata.d.ts +53 -0
- package/dist/metadata.js +114 -0
- package/dist/metadata.js.map +1 -0
- package/dist/metadata.test.d.ts +1 -0
- package/dist/metadata.test.js +286 -0
- package/dist/metadata.test.js.map +1 -0
- package/package.json +100 -3
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { resolveMeta } from './helpers.js';
|
|
3
|
+
describe('resolveMeta', ()=>{
|
|
4
|
+
describe('title resolution', ()=>{
|
|
5
|
+
it('uses meta.title when set', ()=>{
|
|
6
|
+
const result = resolveMeta({
|
|
7
|
+
title: 'Doc title',
|
|
8
|
+
meta: {
|
|
9
|
+
title: 'Meta title'
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
expect(result.title).toBe('Meta title');
|
|
13
|
+
});
|
|
14
|
+
it('falls back to global defaultTitle when meta.title is empty', ()=>{
|
|
15
|
+
const result = resolveMeta({
|
|
16
|
+
title: 'Doc title',
|
|
17
|
+
meta: {}
|
|
18
|
+
}, {
|
|
19
|
+
globals: {
|
|
20
|
+
defaultTitle: 'Global title'
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
expect(result.title).toBe('Global title');
|
|
24
|
+
});
|
|
25
|
+
it('falls back to doc.title when meta and global are empty', ()=>{
|
|
26
|
+
const result = resolveMeta({
|
|
27
|
+
title: 'Doc title',
|
|
28
|
+
meta: {}
|
|
29
|
+
});
|
|
30
|
+
expect(result.title).toBe('Doc title');
|
|
31
|
+
});
|
|
32
|
+
it('returns empty string when all title sources are empty', ()=>{
|
|
33
|
+
const result = resolveMeta({});
|
|
34
|
+
expect(result.title).toBe('');
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
describe('description resolution', ()=>{
|
|
38
|
+
it('uses meta.description when set', ()=>{
|
|
39
|
+
const result = resolveMeta({
|
|
40
|
+
meta: {
|
|
41
|
+
description: 'Meta desc'
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
expect(result.description).toBe('Meta desc');
|
|
45
|
+
});
|
|
46
|
+
it('falls back to global defaultDescription', ()=>{
|
|
47
|
+
const result = resolveMeta({
|
|
48
|
+
meta: {}
|
|
49
|
+
}, {
|
|
50
|
+
globals: {
|
|
51
|
+
defaultDescription: 'Global desc'
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
expect(result.description).toBe('Global desc');
|
|
55
|
+
});
|
|
56
|
+
it('falls back to doc.description', ()=>{
|
|
57
|
+
const result = resolveMeta({
|
|
58
|
+
description: 'Doc desc',
|
|
59
|
+
meta: {}
|
|
60
|
+
});
|
|
61
|
+
expect(result.description).toBe('Doc desc');
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
describe('image resolution', ()=>{
|
|
65
|
+
it('extracts url from a populated image object', ()=>{
|
|
66
|
+
const result = resolveMeta({
|
|
67
|
+
meta: {
|
|
68
|
+
image: {
|
|
69
|
+
url: 'https://example.com/img.jpg'
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
expect(result.image).toBe('https://example.com/img.jpg');
|
|
74
|
+
});
|
|
75
|
+
it('uses a plain string image directly', ()=>{
|
|
76
|
+
const result = resolveMeta({
|
|
77
|
+
meta: {
|
|
78
|
+
image: 'https://example.com/img.jpg'
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
expect(result.image).toBe('https://example.com/img.jpg');
|
|
82
|
+
});
|
|
83
|
+
it('falls back to global defaultImage object', ()=>{
|
|
84
|
+
const result = resolveMeta({
|
|
85
|
+
meta: {}
|
|
86
|
+
}, {
|
|
87
|
+
globals: {
|
|
88
|
+
defaultImage: {
|
|
89
|
+
url: 'https://example.com/default.jpg'
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
expect(result.image).toBe('https://example.com/default.jpg');
|
|
94
|
+
});
|
|
95
|
+
it('falls back to global defaultImage string', ()=>{
|
|
96
|
+
const result = resolveMeta({
|
|
97
|
+
meta: {}
|
|
98
|
+
}, {
|
|
99
|
+
globals: {
|
|
100
|
+
defaultImage: 'https://example.com/default.jpg'
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
expect(result.image).toBe('https://example.com/default.jpg');
|
|
104
|
+
});
|
|
105
|
+
it('returns null when no image is set anywhere', ()=>{
|
|
106
|
+
const result = resolveMeta({
|
|
107
|
+
meta: {}
|
|
108
|
+
});
|
|
109
|
+
expect(result.image).toBeNull();
|
|
110
|
+
});
|
|
111
|
+
it('returns null when image object has no url', ()=>{
|
|
112
|
+
const result = resolveMeta({
|
|
113
|
+
meta: {
|
|
114
|
+
image: {}
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
expect(result.image).toBeNull();
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
describe('alternates', ()=>{
|
|
121
|
+
it('returns manual alternates from the document', ()=>{
|
|
122
|
+
const alternates = [
|
|
123
|
+
{
|
|
124
|
+
locale: 'en',
|
|
125
|
+
url: 'https://example.com/en'
|
|
126
|
+
},
|
|
127
|
+
{
|
|
128
|
+
locale: 'fr',
|
|
129
|
+
url: 'https://example.com/fr'
|
|
130
|
+
}
|
|
131
|
+
];
|
|
132
|
+
const result = resolveMeta({
|
|
133
|
+
meta: {
|
|
134
|
+
alternates
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
expect(result.alternates).toEqual(alternates);
|
|
138
|
+
});
|
|
139
|
+
it('returns empty array when no alternates are set', ()=>{
|
|
140
|
+
const result = resolveMeta({
|
|
141
|
+
meta: {}
|
|
142
|
+
});
|
|
143
|
+
expect(result.alternates).toEqual([]);
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
describe('jsonLd', ()=>{
|
|
147
|
+
it('returns jsonLd from meta', ()=>{
|
|
148
|
+
const jsonLd = {
|
|
149
|
+
'@context': 'https://schema.org',
|
|
150
|
+
'@type': 'Article',
|
|
151
|
+
headline: 'Test'
|
|
152
|
+
};
|
|
153
|
+
const result = resolveMeta({
|
|
154
|
+
meta: {
|
|
155
|
+
jsonLd
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
expect(result.jsonLd).toEqual(jsonLd);
|
|
159
|
+
});
|
|
160
|
+
it('returns null when no jsonLd set', ()=>{
|
|
161
|
+
const result = resolveMeta({
|
|
162
|
+
meta: {}
|
|
163
|
+
});
|
|
164
|
+
expect(result.jsonLd).toBeNull();
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
describe('noindex / nofollow resolution', ()=>{
|
|
168
|
+
it('noindex defaults to false when not set', ()=>{
|
|
169
|
+
const result = resolveMeta({
|
|
170
|
+
meta: {}
|
|
171
|
+
});
|
|
172
|
+
expect(result.noindex).toBe(false);
|
|
173
|
+
});
|
|
174
|
+
it('noindex is true when meta.noindex is true', ()=>{
|
|
175
|
+
const result = resolveMeta({
|
|
176
|
+
meta: {
|
|
177
|
+
noindex: true
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
expect(result.noindex).toBe(true);
|
|
181
|
+
});
|
|
182
|
+
it('noindex is false when meta.noindex is explicitly false', ()=>{
|
|
183
|
+
const result = resolveMeta({
|
|
184
|
+
meta: {
|
|
185
|
+
noindex: false
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
expect(result.noindex).toBe(false);
|
|
189
|
+
});
|
|
190
|
+
it('nofollow defaults to false when not set', ()=>{
|
|
191
|
+
const result = resolveMeta({
|
|
192
|
+
meta: {}
|
|
193
|
+
});
|
|
194
|
+
expect(result.nofollow).toBe(false);
|
|
195
|
+
});
|
|
196
|
+
it('nofollow is true when meta.nofollow is true', ()=>{
|
|
197
|
+
const result = resolveMeta({
|
|
198
|
+
meta: {
|
|
199
|
+
nofollow: true
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
expect(result.nofollow).toBe(true);
|
|
203
|
+
});
|
|
204
|
+
it('there is no global fallback for noindex (per-document only)', ()=>{
|
|
205
|
+
// Passing a globals object does not affect noindex resolution
|
|
206
|
+
const result = resolveMeta({
|
|
207
|
+
meta: {}
|
|
208
|
+
}, {
|
|
209
|
+
globals: {
|
|
210
|
+
defaultTitle: 'Global'
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
expect(result.noindex).toBe(false);
|
|
214
|
+
});
|
|
215
|
+
});
|
|
216
|
+
describe('url resolution', ()=>{
|
|
217
|
+
it('returns empty string when meta.url is not set', ()=>{
|
|
218
|
+
const result = resolveMeta({
|
|
219
|
+
meta: {}
|
|
220
|
+
});
|
|
221
|
+
expect(result.url).toBe('');
|
|
222
|
+
});
|
|
223
|
+
it('passes meta.url through', ()=>{
|
|
224
|
+
const result = resolveMeta({
|
|
225
|
+
meta: {
|
|
226
|
+
url: 'https://example.com/page'
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
expect(result.url).toBe('https://example.com/page');
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
//# sourceMappingURL=helpers.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/helpers.test.ts"],"sourcesContent":["import { describe, expect, it } from 'vitest';\n\nimport { resolveMeta } from './helpers.js';\n\ndescribe('resolveMeta', () => {\n describe('title resolution', () => {\n it('uses meta.title when set', () => {\n const result = resolveMeta({\n title: 'Doc title',\n meta: { title: 'Meta title' },\n });\n expect(result.title).toBe('Meta title');\n });\n\n it('falls back to global defaultTitle when meta.title is empty', () => {\n const result = resolveMeta(\n { title: 'Doc title', meta: {} },\n { globals: { defaultTitle: 'Global title' } }\n );\n expect(result.title).toBe('Global title');\n });\n\n it('falls back to doc.title when meta and global are empty', () => {\n const result = resolveMeta({ title: 'Doc title', meta: {} });\n expect(result.title).toBe('Doc title');\n });\n\n it('returns empty string when all title sources are empty', () => {\n const result = resolveMeta({});\n expect(result.title).toBe('');\n });\n });\n\n describe('description resolution', () => {\n it('uses meta.description when set', () => {\n const result = resolveMeta({ meta: { description: 'Meta desc' } });\n expect(result.description).toBe('Meta desc');\n });\n\n it('falls back to global defaultDescription', () => {\n const result = resolveMeta(\n { meta: {} },\n { globals: { defaultDescription: 'Global desc' } }\n );\n expect(result.description).toBe('Global desc');\n });\n\n it('falls back to doc.description', () => {\n const result = resolveMeta({ description: 'Doc desc', meta: {} });\n expect(result.description).toBe('Doc desc');\n });\n });\n\n describe('image resolution', () => {\n it('extracts url from a populated image object', () => {\n const result = resolveMeta({\n meta: { image: { url: 'https://example.com/img.jpg' } },\n });\n expect(result.image).toBe('https://example.com/img.jpg');\n });\n\n it('uses a plain string image directly', () => {\n const result = resolveMeta({\n meta: { image: 'https://example.com/img.jpg' },\n });\n expect(result.image).toBe('https://example.com/img.jpg');\n });\n\n it('falls back to global defaultImage object', () => {\n const result = resolveMeta(\n { meta: {} },\n {\n globals: { defaultImage: { url: 'https://example.com/default.jpg' } },\n }\n );\n expect(result.image).toBe('https://example.com/default.jpg');\n });\n\n it('falls back to global defaultImage string', () => {\n const result = resolveMeta(\n { meta: {} },\n { globals: { defaultImage: 'https://example.com/default.jpg' } }\n );\n expect(result.image).toBe('https://example.com/default.jpg');\n });\n\n it('returns null when no image is set anywhere', () => {\n const result = resolveMeta({ meta: {} });\n expect(result.image).toBeNull();\n });\n\n it('returns null when image object has no url', () => {\n const result = resolveMeta({ meta: { image: {} } });\n expect(result.image).toBeNull();\n });\n });\n\n describe('alternates', () => {\n it('returns manual alternates from the document', () => {\n const alternates = [\n { locale: 'en', url: 'https://example.com/en' },\n { locale: 'fr', url: 'https://example.com/fr' },\n ];\n const result = resolveMeta({ meta: { alternates } });\n expect(result.alternates).toEqual(alternates);\n });\n\n it('returns empty array when no alternates are set', () => {\n const result = resolveMeta({ meta: {} });\n expect(result.alternates).toEqual([]);\n });\n });\n\n describe('jsonLd', () => {\n it('returns jsonLd from meta', () => {\n const jsonLd = {\n '@context': 'https://schema.org',\n '@type': 'Article',\n headline: 'Test',\n };\n const result = resolveMeta({ meta: { jsonLd } });\n expect(result.jsonLd).toEqual(jsonLd);\n });\n\n it('returns null when no jsonLd set', () => {\n const result = resolveMeta({ meta: {} });\n expect(result.jsonLd).toBeNull();\n });\n });\n\n describe('noindex / nofollow resolution', () => {\n it('noindex defaults to false when not set', () => {\n const result = resolveMeta({ meta: {} });\n expect(result.noindex).toBe(false);\n });\n\n it('noindex is true when meta.noindex is true', () => {\n const result = resolveMeta({ meta: { noindex: true } });\n expect(result.noindex).toBe(true);\n });\n\n it('noindex is false when meta.noindex is explicitly false', () => {\n const result = resolveMeta({ meta: { noindex: false } });\n expect(result.noindex).toBe(false);\n });\n\n it('nofollow defaults to false when not set', () => {\n const result = resolveMeta({ meta: {} });\n expect(result.nofollow).toBe(false);\n });\n\n it('nofollow is true when meta.nofollow is true', () => {\n const result = resolveMeta({ meta: { nofollow: true } });\n expect(result.nofollow).toBe(true);\n });\n\n it('there is no global fallback for noindex (per-document only)', () => {\n // Passing a globals object does not affect noindex resolution\n const result = resolveMeta(\n { meta: {} },\n { globals: { defaultTitle: 'Global' } }\n );\n expect(result.noindex).toBe(false);\n });\n });\n\n describe('url resolution', () => {\n it('returns empty string when meta.url is not set', () => {\n const result = resolveMeta({ meta: {} });\n expect(result.url).toBe('');\n });\n\n it('passes meta.url through', () => {\n const result = resolveMeta({ meta: { url: 'https://example.com/page' } });\n expect(result.url).toBe('https://example.com/page');\n });\n });\n});\n"],"names":["describe","expect","it","resolveMeta","result","title","meta","toBe","globals","defaultTitle","description","defaultDescription","image","url","defaultImage","toBeNull","alternates","locale","toEqual","jsonLd","headline","noindex","nofollow"],"mappings":"AAAA,SAASA,QAAQ,EAAEC,MAAM,EAAEC,EAAE,QAAQ,SAAS;AAE9C,SAASC,WAAW,QAAQ,eAAe;AAE3CH,SAAS,eAAe;IACtBA,SAAS,oBAAoB;QAC3BE,GAAG,4BAA4B;YAC7B,MAAME,SAASD,YAAY;gBACzBE,OAAO;gBACPC,MAAM;oBAAED,OAAO;gBAAa;YAC9B;YACAJ,OAAOG,OAAOC,KAAK,EAAEE,IAAI,CAAC;QAC5B;QAEAL,GAAG,8DAA8D;YAC/D,MAAME,SAASD,YACb;gBAAEE,OAAO;gBAAaC,MAAM,CAAC;YAAE,GAC/B;gBAAEE,SAAS;oBAAEC,cAAc;gBAAe;YAAE;YAE9CR,OAAOG,OAAOC,KAAK,EAAEE,IAAI,CAAC;QAC5B;QAEAL,GAAG,0DAA0D;YAC3D,MAAME,SAASD,YAAY;gBAAEE,OAAO;gBAAaC,MAAM,CAAC;YAAE;YAC1DL,OAAOG,OAAOC,KAAK,EAAEE,IAAI,CAAC;QAC5B;QAEAL,GAAG,yDAAyD;YAC1D,MAAME,SAASD,YAAY,CAAC;YAC5BF,OAAOG,OAAOC,KAAK,EAAEE,IAAI,CAAC;QAC5B;IACF;IAEAP,SAAS,0BAA0B;QACjCE,GAAG,kCAAkC;YACnC,MAAME,SAASD,YAAY;gBAAEG,MAAM;oBAAEI,aAAa;gBAAY;YAAE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BAAEG,MAAM;oBAAEe,SAAS;gBAAM;YAAE;YACtDpB,OAAOG,OAAOiB,OAAO,EAAEd,IAAI,CAAC;QAC9B;QAEAL,GAAG,2CAA2C;YAC5C,MAAME,SAASD,YAAY;gBAAEG,MAAM,CAAC;YAAE;YACtCL,OAAOG,OAAOkB,QAAQ,EAAEf,IAAI,CAAC;QAC/B;QAEAL,GAAG,+CAA+C;YAChD,MAAME,SAASD,YAAY;gBAAEG,MAAM;oBAAEgB,UAAU;gBAAK;YAAE;YACtDrB,OAAOG,OAAOkB,QAAQ,EAAEf,IAAI,CAAC;QAC/B;QAEAL,GAAG,+DAA+D;YAChE,8DAA8D;YAC9D,MAAME,SAASD,YACb;gBAAEG,MAAM,CAAC;YAAE,GACX;gBAAEE,SAAS;oBAAEC,cAAc;gBAAS;YAAE;YAExCR,OAAOG,OAAOiB,OAAO,EAAEd,IAAI,CAAC;QAC9B;IACF;IAEAP,SAAS,kBAAkB;QACzBE,GAAG,iDAAiD;YAClD,MAAME,SAASD,YAAY;gBAAEG,MAAM,CAAC;YAAE;YACtCL,OAAOG,OAAOS,GAAG,EAAEN,IAAI,CAAC;QAC1B;QAEAL,GAAG,2BAA2B;YAC5B,MAAME,SAASD,YAAY;gBAAEG,MAAM;oBAAEO,KAAK;gBAA2B;YAAE;YACvEZ,OAAOG,OAAOS,GAAG,EAAEN,IAAI,CAAC;QAC1B;IACF;AACF"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { CollectionAfterReadHook } from 'payload';
|
|
2
|
+
import type { GenerateAlternateURLArgs } from '../index.js';
|
|
3
|
+
type BuildAutoAlternatesHookArgs = {
|
|
4
|
+
collectionSlug: string;
|
|
5
|
+
locales: string[];
|
|
6
|
+
generateAlternateURL?: (args: GenerateAlternateURLArgs) => string | null | Promise<string | null>;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* afterRead hook — auto-generates hreflang alternates when:
|
|
10
|
+
* 1. The global-seo document has autoGenerateAlternates = true
|
|
11
|
+
* 2. The document has no manually entered alternates
|
|
12
|
+
* 3. A generateAlternateURL function is configured on the plugin
|
|
13
|
+
*
|
|
14
|
+
* Manual alternates always win — if the editor has added even one entry
|
|
15
|
+
* the auto-generator is skipped entirely for that document.
|
|
16
|
+
*/
|
|
17
|
+
export declare const buildAutoAlternatesHook: ({ collectionSlug, locales, generateAlternateURL, }: BuildAutoAlternatesHookArgs) => CollectionAfterReadHook;
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* afterRead hook — auto-generates hreflang alternates when:
|
|
3
|
+
* 1. The global-seo document has autoGenerateAlternates = true
|
|
4
|
+
* 2. The document has no manually entered alternates
|
|
5
|
+
* 3. A generateAlternateURL function is configured on the plugin
|
|
6
|
+
*
|
|
7
|
+
* Manual alternates always win — if the editor has added even one entry
|
|
8
|
+
* the auto-generator is skipped entirely for that document.
|
|
9
|
+
*/ export const buildAutoAlternatesHook = ({ collectionSlug, locales, generateAlternateURL })=>{
|
|
10
|
+
return async ({ doc, req })=>{
|
|
11
|
+
// Nothing to do if no generator or no locales configured
|
|
12
|
+
if (!generateAlternateURL || locales.length === 0) {
|
|
13
|
+
return doc;
|
|
14
|
+
}
|
|
15
|
+
// Skip if the doc already has manual alternates
|
|
16
|
+
const existingAlternates = doc?.meta?.alternates ?? [];
|
|
17
|
+
if (existingAlternates.length > 0) {
|
|
18
|
+
return doc;
|
|
19
|
+
}
|
|
20
|
+
// Check the global-seo toggle
|
|
21
|
+
let autoGenerate = false;
|
|
22
|
+
try {
|
|
23
|
+
const globalSeo = await req.payload.findGlobal({
|
|
24
|
+
slug: 'global-seo',
|
|
25
|
+
depth: 0
|
|
26
|
+
});
|
|
27
|
+
autoGenerate = Boolean(globalSeo.autoGenerateAlternates);
|
|
28
|
+
} catch {
|
|
29
|
+
// global-seo may not exist yet (first boot), skip silently
|
|
30
|
+
return doc;
|
|
31
|
+
}
|
|
32
|
+
if (!autoGenerate) {
|
|
33
|
+
return doc;
|
|
34
|
+
}
|
|
35
|
+
// Build alternates for each configured locale
|
|
36
|
+
const generated = [];
|
|
37
|
+
for (const locale of locales){
|
|
38
|
+
const url = await generateAlternateURL({
|
|
39
|
+
doc,
|
|
40
|
+
locale,
|
|
41
|
+
collectionSlug
|
|
42
|
+
});
|
|
43
|
+
if (url) {
|
|
44
|
+
generated.push({
|
|
45
|
+
locale,
|
|
46
|
+
url
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
if (generated.length === 0) {
|
|
51
|
+
return doc;
|
|
52
|
+
}
|
|
53
|
+
return {
|
|
54
|
+
...doc,
|
|
55
|
+
meta: {
|
|
56
|
+
...doc.meta ?? {},
|
|
57
|
+
alternates: generated
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
//# sourceMappingURL=autoAlternates.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/hooks/autoAlternates.ts"],"sourcesContent":["import type { CollectionAfterReadHook } from 'payload';\nimport type { Alternate } from '../helpers.js';\nimport type { GenerateAlternateURLArgs } from '../index.js';\n\ntype BuildAutoAlternatesHookArgs = {\n collectionSlug: string;\n locales: string[];\n generateAlternateURL?: (\n args: GenerateAlternateURLArgs\n ) => string | null | Promise<string | null>;\n};\n\n/**\n * afterRead hook — auto-generates hreflang alternates when:\n * 1. The global-seo document has autoGenerateAlternates = true\n * 2. The document has no manually entered alternates\n * 3. A generateAlternateURL function is configured on the plugin\n *\n * Manual alternates always win — if the editor has added even one entry\n * the auto-generator is skipped entirely for that document.\n */\nexport const buildAutoAlternatesHook = ({\n collectionSlug,\n locales,\n generateAlternateURL,\n}: BuildAutoAlternatesHookArgs): CollectionAfterReadHook => {\n return async ({ doc, req }) => {\n // Nothing to do if no generator or no locales configured\n if (!generateAlternateURL || locales.length === 0) {\n return doc;\n }\n\n // Skip if the doc already has manual alternates\n const existingAlternates: Alternate[] = doc?.meta?.alternates ?? [];\n if (existingAlternates.length > 0) {\n return doc;\n }\n\n // Check the global-seo toggle\n let autoGenerate = false;\n try {\n const globalSeo = await req.payload.findGlobal({\n slug: 'global-seo',\n depth: 0,\n });\n autoGenerate = Boolean(\n (globalSeo as Record<string, unknown>).autoGenerateAlternates\n );\n } catch {\n // global-seo may not exist yet (first boot), skip silently\n return doc;\n }\n\n if (!autoGenerate) {\n return doc;\n }\n\n // Build alternates for each configured locale\n const generated: Alternate[] = [];\n for (const locale of locales) {\n const url = await generateAlternateURL({ doc, locale, collectionSlug });\n if (url) {\n generated.push({ locale, url });\n }\n }\n\n if (generated.length === 0) {\n return doc;\n }\n\n return {\n ...doc,\n meta: {\n ...(doc.meta ?? {}),\n alternates: generated,\n },\n };\n };\n};\n"],"names":["buildAutoAlternatesHook","collectionSlug","locales","generateAlternateURL","doc","req","length","existingAlternates","meta","alternates","autoGenerate","globalSeo","payload","findGlobal","slug","depth","Boolean","autoGenerateAlternates","generated","locale","url","push"],"mappings":"AAYA;;;;;;;;CAQC,GACD,OAAO,MAAMA,0BAA0B,CAAC,EACtCC,cAAc,EACdC,OAAO,EACPC,oBAAoB,EACQ;IAC5B,OAAO,OAAO,EAAEC,GAAG,EAAEC,GAAG,EAAE;QACxB,yDAAyD;QACzD,IAAI,CAACF,wBAAwBD,QAAQI,MAAM,KAAK,GAAG;YACjD,OAAOF;QACT;QAEA,gDAAgD;QAChD,MAAMG,qBAAkCH,KAAKI,MAAMC,cAAc,EAAE;QACnE,IAAIF,mBAAmBD,MAAM,GAAG,GAAG;YACjC,OAAOF;QACT;QAEA,8BAA8B;QAC9B,IAAIM,eAAe;QACnB,IAAI;YACF,MAAMC,YAAY,MAAMN,IAAIO,OAAO,CAACC,UAAU,CAAC;gBAC7CC,MAAM;gBACNC,OAAO;YACT;YACAL,eAAeM,QACb,AAACL,UAAsCM,sBAAsB;QAEjE,EAAE,OAAM;YACN,2DAA2D;YAC3D,OAAOb;QACT;QAEA,IAAI,CAACM,cAAc;YACjB,OAAON;QACT;QAEA,8CAA8C;QAC9C,MAAMc,YAAyB,EAAE;QACjC,KAAK,MAAMC,UAAUjB,QAAS;YAC5B,MAAMkB,MAAM,MAAMjB,qBAAqB;gBAAEC;gBAAKe;gBAAQlB;YAAe;YACrE,IAAImB,KAAK;gBACPF,UAAUG,IAAI,CAAC;oBAAEF;oBAAQC;gBAAI;YAC/B;QACF;QAEA,IAAIF,UAAUZ,MAAM,KAAK,GAAG;YAC1B,OAAOF;QACT;QAEA,OAAO;YACL,GAAGA,GAAG;YACNI,MAAM;gBACJ,GAAIJ,IAAII,IAAI,IAAI,CAAC,CAAC;gBAClBC,YAAYS;YACd;QACF;IACF;AACF,EAAE"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import { buildAutoAlternatesHook } from './autoAlternates.js';
|
|
3
|
+
const makeReq = (globalSeoData = {}, shouldThrow = false)=>({
|
|
4
|
+
payload: {
|
|
5
|
+
findGlobal: shouldThrow ? vi.fn().mockRejectedValue(new Error('not found')) : vi.fn().mockResolvedValue(globalSeoData)
|
|
6
|
+
}
|
|
7
|
+
});
|
|
8
|
+
const runHook = (hook, doc, req = makeReq({
|
|
9
|
+
autoGenerateAlternates: true
|
|
10
|
+
}))=>hook({
|
|
11
|
+
collection: {},
|
|
12
|
+
context: {},
|
|
13
|
+
doc,
|
|
14
|
+
req: req
|
|
15
|
+
});
|
|
16
|
+
describe('buildAutoAlternatesHook', ()=>{
|
|
17
|
+
it('returns doc unchanged when no generateAlternateURL is configured', async ()=>{
|
|
18
|
+
const hook = buildAutoAlternatesHook({
|
|
19
|
+
collectionSlug: 'posts',
|
|
20
|
+
locales: [
|
|
21
|
+
'en',
|
|
22
|
+
'fr'
|
|
23
|
+
]
|
|
24
|
+
});
|
|
25
|
+
const doc = {
|
|
26
|
+
title: 'Hello',
|
|
27
|
+
meta: {}
|
|
28
|
+
};
|
|
29
|
+
const result = await runHook(hook, doc);
|
|
30
|
+
expect(result).toEqual(doc);
|
|
31
|
+
});
|
|
32
|
+
it('returns doc unchanged when locales array is empty', async ()=>{
|
|
33
|
+
const hook = buildAutoAlternatesHook({
|
|
34
|
+
collectionSlug: 'posts',
|
|
35
|
+
locales: [],
|
|
36
|
+
generateAlternateURL: ()=>'https://example.com/en'
|
|
37
|
+
});
|
|
38
|
+
const doc = {
|
|
39
|
+
title: 'Hello',
|
|
40
|
+
meta: {}
|
|
41
|
+
};
|
|
42
|
+
const result = await runHook(hook, doc);
|
|
43
|
+
expect(result).toEqual(doc);
|
|
44
|
+
});
|
|
45
|
+
it('returns doc unchanged when manual alternates already exist', async ()=>{
|
|
46
|
+
const hook = buildAutoAlternatesHook({
|
|
47
|
+
collectionSlug: 'posts',
|
|
48
|
+
locales: [
|
|
49
|
+
'en',
|
|
50
|
+
'fr'
|
|
51
|
+
],
|
|
52
|
+
generateAlternateURL: ()=>'https://example.com/generated'
|
|
53
|
+
});
|
|
54
|
+
const doc = {
|
|
55
|
+
title: 'Hello',
|
|
56
|
+
meta: {
|
|
57
|
+
alternates: [
|
|
58
|
+
{
|
|
59
|
+
locale: 'en',
|
|
60
|
+
url: 'https://example.com/manual'
|
|
61
|
+
}
|
|
62
|
+
]
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
const result = await runHook(hook, doc);
|
|
66
|
+
expect(result.meta.alternates).toEqual([
|
|
67
|
+
{
|
|
68
|
+
locale: 'en',
|
|
69
|
+
url: 'https://example.com/manual'
|
|
70
|
+
}
|
|
71
|
+
]);
|
|
72
|
+
});
|
|
73
|
+
it('returns doc unchanged when global autoGenerateAlternates is false', async ()=>{
|
|
74
|
+
const hook = buildAutoAlternatesHook({
|
|
75
|
+
collectionSlug: 'posts',
|
|
76
|
+
locales: [
|
|
77
|
+
'en',
|
|
78
|
+
'fr'
|
|
79
|
+
],
|
|
80
|
+
generateAlternateURL: ()=>'https://example.com/en'
|
|
81
|
+
});
|
|
82
|
+
const doc = {
|
|
83
|
+
title: 'Hello',
|
|
84
|
+
meta: {}
|
|
85
|
+
};
|
|
86
|
+
const req = makeReq({
|
|
87
|
+
autoGenerateAlternates: false
|
|
88
|
+
});
|
|
89
|
+
const result = await runHook(hook, doc, req);
|
|
90
|
+
expect(result.meta?.alternates).toBeUndefined();
|
|
91
|
+
});
|
|
92
|
+
it('returns doc unchanged when global-seo fetch throws (first boot)', async ()=>{
|
|
93
|
+
const hook = buildAutoAlternatesHook({
|
|
94
|
+
collectionSlug: 'posts',
|
|
95
|
+
locales: [
|
|
96
|
+
'en',
|
|
97
|
+
'fr'
|
|
98
|
+
],
|
|
99
|
+
generateAlternateURL: ()=>'https://example.com/en'
|
|
100
|
+
});
|
|
101
|
+
const doc = {
|
|
102
|
+
title: 'Hello',
|
|
103
|
+
meta: {}
|
|
104
|
+
};
|
|
105
|
+
const req = makeReq({}, true);
|
|
106
|
+
const result = await runHook(hook, doc, req);
|
|
107
|
+
expect(result).toEqual(doc);
|
|
108
|
+
});
|
|
109
|
+
it('generates alternates for each configured locale', async ()=>{
|
|
110
|
+
const hook = buildAutoAlternatesHook({
|
|
111
|
+
collectionSlug: 'posts',
|
|
112
|
+
locales: [
|
|
113
|
+
'en',
|
|
114
|
+
'fr',
|
|
115
|
+
'de'
|
|
116
|
+
],
|
|
117
|
+
generateAlternateURL: ({ locale })=>`https://example.com/${locale}/page`
|
|
118
|
+
});
|
|
119
|
+
const doc = {
|
|
120
|
+
slug: 'page',
|
|
121
|
+
meta: {}
|
|
122
|
+
};
|
|
123
|
+
const result = await runHook(hook, doc);
|
|
124
|
+
expect(result.meta.alternates).toEqual([
|
|
125
|
+
{
|
|
126
|
+
locale: 'en',
|
|
127
|
+
url: 'https://example.com/en/page'
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
locale: 'fr',
|
|
131
|
+
url: 'https://example.com/fr/page'
|
|
132
|
+
},
|
|
133
|
+
{
|
|
134
|
+
locale: 'de',
|
|
135
|
+
url: 'https://example.com/de/page'
|
|
136
|
+
}
|
|
137
|
+
]);
|
|
138
|
+
});
|
|
139
|
+
it('skips locales where generateAlternateURL returns null', async ()=>{
|
|
140
|
+
const hook = buildAutoAlternatesHook({
|
|
141
|
+
collectionSlug: 'posts',
|
|
142
|
+
locales: [
|
|
143
|
+
'en',
|
|
144
|
+
'fr',
|
|
145
|
+
'de'
|
|
146
|
+
],
|
|
147
|
+
generateAlternateURL: ({ locale })=>locale === 'fr' ? null : `https://example.com/${locale}`
|
|
148
|
+
});
|
|
149
|
+
const doc = {
|
|
150
|
+
meta: {}
|
|
151
|
+
};
|
|
152
|
+
const result = await runHook(hook, doc);
|
|
153
|
+
expect(result.meta.alternates).toEqual([
|
|
154
|
+
{
|
|
155
|
+
locale: 'en',
|
|
156
|
+
url: 'https://example.com/en'
|
|
157
|
+
},
|
|
158
|
+
{
|
|
159
|
+
locale: 'de',
|
|
160
|
+
url: 'https://example.com/de'
|
|
161
|
+
}
|
|
162
|
+
]);
|
|
163
|
+
});
|
|
164
|
+
it('returns doc unchanged when all locales return null', async ()=>{
|
|
165
|
+
const hook = buildAutoAlternatesHook({
|
|
166
|
+
collectionSlug: 'posts',
|
|
167
|
+
locales: [
|
|
168
|
+
'en',
|
|
169
|
+
'fr'
|
|
170
|
+
],
|
|
171
|
+
generateAlternateURL: ()=>null
|
|
172
|
+
});
|
|
173
|
+
const doc = {
|
|
174
|
+
meta: {}
|
|
175
|
+
};
|
|
176
|
+
const result = await runHook(hook, doc);
|
|
177
|
+
expect(result.meta?.alternates).toBeUndefined();
|
|
178
|
+
});
|
|
179
|
+
it('merges generated alternates with existing meta fields', async ()=>{
|
|
180
|
+
const hook = buildAutoAlternatesHook({
|
|
181
|
+
collectionSlug: 'posts',
|
|
182
|
+
locales: [
|
|
183
|
+
'en'
|
|
184
|
+
],
|
|
185
|
+
generateAlternateURL: ()=>'https://example.com/en'
|
|
186
|
+
});
|
|
187
|
+
const doc = {
|
|
188
|
+
meta: {
|
|
189
|
+
title: 'My Title',
|
|
190
|
+
description: 'My Desc'
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
const result = await runHook(hook, doc);
|
|
194
|
+
expect(result.meta.title).toBe('My Title');
|
|
195
|
+
expect(result.meta.description).toBe('My Desc');
|
|
196
|
+
expect(result.meta.alternates).toEqual([
|
|
197
|
+
{
|
|
198
|
+
locale: 'en',
|
|
199
|
+
url: 'https://example.com/en'
|
|
200
|
+
}
|
|
201
|
+
]);
|
|
202
|
+
});
|
|
203
|
+
it('passes collectionSlug and doc to generateAlternateURL', async ()=>{
|
|
204
|
+
const generateAlternateURL = vi.fn().mockResolvedValue('https://example.com/en');
|
|
205
|
+
const hook = buildAutoAlternatesHook({
|
|
206
|
+
collectionSlug: 'posts',
|
|
207
|
+
locales: [
|
|
208
|
+
'en'
|
|
209
|
+
],
|
|
210
|
+
generateAlternateURL
|
|
211
|
+
});
|
|
212
|
+
const doc = {
|
|
213
|
+
id: '123',
|
|
214
|
+
meta: {}
|
|
215
|
+
};
|
|
216
|
+
await runHook(hook, doc);
|
|
217
|
+
expect(generateAlternateURL).toHaveBeenCalledWith({
|
|
218
|
+
collectionSlug: 'posts',
|
|
219
|
+
doc,
|
|
220
|
+
locale: 'en'
|
|
221
|
+
});
|
|
222
|
+
});
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
//# sourceMappingURL=autoAlternates.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/hooks/autoAlternates.test.ts"],"sourcesContent":["import { describe, expect, it, vi } from 'vitest';\n\nimport { buildAutoAlternatesHook } from './autoAlternates.js';\n\nconst makeReq = (\n globalSeoData: Record<string, unknown> = {},\n shouldThrow = false\n) => ({\n payload: {\n findGlobal: shouldThrow\n ? vi.fn().mockRejectedValue(new Error('not found'))\n : vi.fn().mockResolvedValue(globalSeoData),\n },\n});\n\nconst runHook = (\n hook: ReturnType<typeof buildAutoAlternatesHook>,\n doc: Record<string, unknown>,\n req = makeReq({ autoGenerateAlternates: true })\n) =>\n hook({\n collection: {} as any,\n context: {},\n doc,\n req: req as any,\n });\n\ndescribe('buildAutoAlternatesHook', () => {\n it('returns doc unchanged when no generateAlternateURL is configured', async () => {\n const hook = buildAutoAlternatesHook({\n collectionSlug: 'posts',\n locales: ['en', 'fr'],\n });\n const doc = { title: 'Hello', meta: {} };\n const result = await runHook(hook, doc);\n expect(result).toEqual(doc);\n });\n\n it('returns doc unchanged when locales array is empty', async () => {\n const hook = buildAutoAlternatesHook({\n collectionSlug: 'posts',\n locales: [],\n generateAlternateURL: () => 'https://example.com/en',\n });\n const doc = { title: 'Hello', meta: {} };\n const result = await runHook(hook, doc);\n expect(result).toEqual(doc);\n });\n\n it('returns doc unchanged when manual alternates already exist', async () => {\n const hook = buildAutoAlternatesHook({\n collectionSlug: 'posts',\n locales: ['en', 'fr'],\n generateAlternateURL: () => 'https://example.com/generated',\n });\n const doc = {\n title: 'Hello',\n meta: {\n alternates: [{ locale: 'en', url: 'https://example.com/manual' }],\n },\n };\n const result = await runHook(hook, doc);\n expect((result as any).meta.alternates).toEqual([\n { locale: 'en', url: 'https://example.com/manual' },\n ]);\n });\n\n it('returns doc unchanged when global autoGenerateAlternates is false', async () => {\n const hook = buildAutoAlternatesHook({\n collectionSlug: 'posts',\n locales: ['en', 'fr'],\n generateAlternateURL: () => 'https://example.com/en',\n });\n const doc = { title: 'Hello', meta: {} };\n const req = makeReq({ autoGenerateAlternates: false });\n const result = await runHook(hook, doc, req);\n expect((result as any).meta?.alternates).toBeUndefined();\n });\n\n it('returns doc unchanged when global-seo fetch throws (first boot)', async () => {\n const hook = buildAutoAlternatesHook({\n collectionSlug: 'posts',\n locales: ['en', 'fr'],\n generateAlternateURL: () => 'https://example.com/en',\n });\n const doc = { title: 'Hello', meta: {} };\n const req = makeReq({}, true);\n const result = await runHook(hook, doc, req);\n expect(result).toEqual(doc);\n });\n\n it('generates alternates for each configured locale', async () => {\n const hook = buildAutoAlternatesHook({\n collectionSlug: 'posts',\n locales: ['en', 'fr', 'de'],\n generateAlternateURL: ({ locale }) =>\n `https://example.com/${locale}/page`,\n });\n const doc = { slug: 'page', meta: {} };\n const result = await runHook(hook, doc);\n expect((result as any).meta.alternates).toEqual([\n { locale: 'en', url: 'https://example.com/en/page' },\n { locale: 'fr', url: 'https://example.com/fr/page' },\n { locale: 'de', url: 'https://example.com/de/page' },\n ]);\n });\n\n it('skips locales where generateAlternateURL returns null', async () => {\n const hook = buildAutoAlternatesHook({\n collectionSlug: 'posts',\n locales: ['en', 'fr', 'de'],\n generateAlternateURL: ({ locale }) =>\n locale === 'fr' ? null : `https://example.com/${locale}`,\n });\n const doc = { meta: {} };\n const result = await runHook(hook, doc);\n expect((result as any).meta.alternates).toEqual([\n { locale: 'en', url: 'https://example.com/en' },\n { locale: 'de', url: 'https://example.com/de' },\n ]);\n });\n\n it('returns doc unchanged when all locales return null', async () => {\n const hook = buildAutoAlternatesHook({\n collectionSlug: 'posts',\n locales: ['en', 'fr'],\n generateAlternateURL: () => null,\n });\n const doc = { meta: {} };\n const result = await runHook(hook, doc);\n expect((result as any).meta?.alternates).toBeUndefined();\n });\n\n it('merges generated alternates with existing meta fields', async () => {\n const hook = buildAutoAlternatesHook({\n collectionSlug: 'posts',\n locales: ['en'],\n generateAlternateURL: () => 'https://example.com/en',\n });\n const doc = { meta: { title: 'My Title', description: 'My Desc' } };\n const result = await runHook(hook, doc);\n expect((result as any).meta.title).toBe('My Title');\n expect((result as any).meta.description).toBe('My Desc');\n expect((result as any).meta.alternates).toEqual([\n { locale: 'en', url: 'https://example.com/en' },\n ]);\n });\n\n it('passes collectionSlug and doc to generateAlternateURL', async () => {\n const generateAlternateURL = vi\n .fn()\n .mockResolvedValue('https://example.com/en');\n const hook = buildAutoAlternatesHook({\n collectionSlug: 'posts',\n locales: ['en'],\n generateAlternateURL,\n });\n const doc = { id: '123', meta: {} };\n await runHook(hook, doc);\n expect(generateAlternateURL).toHaveBeenCalledWith({\n collectionSlug: 'posts',\n doc,\n locale: 'en',\n });\n });\n});\n"],"names":["describe","expect","it","vi","buildAutoAlternatesHook","makeReq","globalSeoData","shouldThrow","payload","findGlobal","fn","mockRejectedValue","Error","mockResolvedValue","runHook","hook","doc","req","autoGenerateAlternates","collection","context","collectionSlug","locales","title","meta","result","toEqual","generateAlternateURL","alternates","locale","url","toBeUndefined","slug","description","toBe","id","toHaveBeenCalledWith"],"mappings":"AAAA,SAASA,QAAQ,EAAEC,MAAM,EAAEC,EAAE,EAAEC,EAAE,QAAQ,SAAS;AAElD,SAASC,uBAAuB,QAAQ,sBAAsB;AAE9D,MAAMC,UAAU,CACdC,gBAAyC,CAAC,CAAC,EAC3CC,cAAc,KAAK,GACf,CAAA;QACJC,SAAS;YACPC,YAAYF,cACRJ,GAAGO,EAAE,GAAGC,iBAAiB,CAAC,IAAIC,MAAM,gBACpCT,GAAGO,EAAE,GAAGG,iBAAiB,CAACP;QAChC;IACF,CAAA;AAEA,MAAMQ,UAAU,CACdC,MACAC,KACAC,MAAMZ,QAAQ;IAAEa,wBAAwB;AAAK,EAAE,GAE/CH,KAAK;QACHI,YAAY,CAAC;QACbC,SAAS,CAAC;QACVJ;QACAC,KAAKA;IACP;AAEFjB,SAAS,2BAA2B;IAClCE,GAAG,oEAAoE;QACrE,MAAMa,OAAOX,wBAAwB;YACnCiB,gBAAgB;YAChBC,SAAS;gBAAC;gBAAM;aAAK;QACvB;QACA,MAAMN,MAAM;YAAEO,OAAO;YAASC,MAAM,CAAC;QAAE;QACvC,MAAMC,SAAS,MAAMX,QAAQC,MAAMC;QACnCf,OAAOwB,QAAQC,OAAO,CAACV;IACzB;IAEAd,GAAG,qDAAqD;QACtD,MAAMa,OAAOX,wBAAwB;YACnCiB,gBAAgB;YAChBC,SAAS,EAAE;YACXK,sBAAsB,IAAM;QAC9B;QACA,MAAMX,MAAM;YAAEO,OAAO;YAASC,MAAM,CAAC;Q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CiB,gBAAgB;YAChBC,SAAS;gBAAC;aAAK;YACfK,sBAAsB,IAAM;QAC9B;QACA,MAAMX,MAAM;YAAEQ,MAAM;gBAAED,OAAO;gBAAYU,aAAa;YAAU;QAAE;QAClE,MAAMR,SAAS,MAAMX,QAAQC,MAAMC;QACnCf,OAAO,AAACwB,OAAeD,IAAI,CAACD,KAAK,EAAEW,IAAI,CAAC;QACxCjC,OAAO,AAACwB,OAAeD,IAAI,CAACS,WAAW,EAAEC,IAAI,CAAC;QAC9CjC,OAAO,AAACwB,OAAeD,IAAI,CAACI,UAAU,EAAEF,OAAO,CAAC;YAC9C;gBAAEG,QAAQ;gBAAMC,KAAK;YAAyB;SAC/C;IACH;IAEA5B,GAAG,yDAAyD;QAC1D,MAAMyB,uBAAuBxB,GAC1BO,EAAE,GACFG,iBAAiB,CAAC;QACrB,MAAME,OAAOX,wBAAwB;YACnCiB,gBAAgB;YAChBC,SAAS;gBAAC;aAAK;YACfK;QACF;QACA,MAAMX,MAAM;YAAEmB,IAAI;YAAOX,MAAM,CAAC;QAAE;QAClC,MAAMV,QAAQC,MAAMC;QACpBf,OAAO0B,sBAAsBS,oBAAoB,CAAC;YAChDf,gBAAgB;YAChBL;YACAa,QAAQ;QACV;IACF;AACF"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { CollectionBeforeChangeHook } from 'payload';
|
|
2
|
+
import type { GenerateTitleArgs, GenerateURLArgs } from '../index.js';
|
|
3
|
+
type BuildGenerateMetaHookArgs = {
|
|
4
|
+
collectionSlug: string;
|
|
5
|
+
generateTitle?: (args: GenerateTitleArgs) => string | null | Promise<string | null>;
|
|
6
|
+
generateURL?: (args: GenerateURLArgs) => string | null | Promise<string | null>;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* beforeChange hook — auto-populates meta.title and meta.url when:
|
|
10
|
+
* - generateTitle is configured and meta.title is empty
|
|
11
|
+
* - generateURL is configured and meta.url is empty
|
|
12
|
+
*
|
|
13
|
+
* Existing values are never overwritten — the editor always wins.
|
|
14
|
+
* Both functions are called in parallel for efficiency.
|
|
15
|
+
*/
|
|
16
|
+
export declare const buildGenerateMetaHook: ({ collectionSlug, generateTitle, generateURL, }: BuildGenerateMetaHookArgs) => CollectionBeforeChangeHook;
|
|
17
|
+
export {};
|