@umituz/web-seo 2.0.21 → 2.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.
Files changed (103) hide show
  1. package/dist/domains/seo/components/JsonLd.d.ts +7 -6
  2. package/dist/domains/seo/components/JsonLd.js +26 -77
  3. package/dist/domains/seo/components/SEO.d.ts +7 -0
  4. package/dist/domains/seo/components/SEO.js +10 -33
  5. package/dist/domains/seo/core/constants.d.ts +15 -0
  6. package/dist/domains/seo/core/constants.js +15 -0
  7. package/dist/domains/seo/core/deep-equal.d.ts +8 -0
  8. package/dist/domains/seo/core/deep-equal.js +28 -0
  9. package/dist/domains/seo/core/dom-queue.d.ts +10 -0
  10. package/dist/domains/seo/core/dom-queue.js +37 -0
  11. package/dist/domains/seo/core/escape.d.ts +12 -0
  12. package/dist/domains/seo/core/escape.js +41 -0
  13. package/dist/domains/seo/core/hash.d.ts +9 -0
  14. package/dist/domains/seo/core/hash.js +29 -0
  15. package/dist/domains/seo/core/init.d.ts +15 -2
  16. package/dist/domains/seo/core/init.js +38 -18
  17. package/dist/domains/seo/core/seo-strategies.d.ts +29 -0
  18. package/dist/domains/seo/core/seo-strategies.js +694 -0
  19. package/dist/domains/seo/core/seo-strategy.d.ts +37 -0
  20. package/dist/domains/seo/core/seo-strategy.js +14 -0
  21. package/dist/domains/seo/hooks/useJsonLd.d.ts +6 -7
  22. package/dist/domains/seo/hooks/useJsonLd.js +27 -61
  23. package/dist/domains/seo/hooks/useSEO.d.ts +6 -0
  24. package/dist/domains/seo/hooks/useSEO.js +7 -29
  25. package/dist/domains/seo/ssr/helmet.d.ts +25 -0
  26. package/dist/domains/seo/ssr/helmet.js +23 -0
  27. package/dist/domains/seo/ssr/index.d.ts +10 -0
  28. package/dist/domains/seo/ssr/index.js +8 -0
  29. package/dist/domains/seo/ssr/renderer.d.ts +11 -36
  30. package/dist/domains/seo/ssr/renderer.js +51 -285
  31. package/dist/domains/seo/ssr/serializer.d.ts +5 -60
  32. package/dist/domains/seo/ssr/serializer.js +26 -166
  33. package/dist/domains/seo/types/index.d.ts +0 -4
  34. package/dist/domains/seo/types/schema.d.ts +22 -1
  35. package/dist/domains/seo/utils/dom/dom-context.d.ts +13 -0
  36. package/dist/domains/seo/utils/dom/dom-context.js +62 -0
  37. package/dist/domains/seo/utils/dom/dom-mutator.d.ts +31 -0
  38. package/dist/domains/seo/utils/dom/dom-mutator.js +89 -0
  39. package/dist/domains/seo/utils/dom/index.d.ts +1 -0
  40. package/dist/domains/seo/utils/dom/index.js +1 -0
  41. package/dist/domains/seo/utils/index.d.ts +3 -4
  42. package/dist/domains/seo/utils/index.js +3 -5
  43. package/dist/domains/seo/utils/schema-generators/base.d.ts +25 -0
  44. package/dist/domains/seo/utils/schema-generators/base.js +50 -0
  45. package/dist/domains/seo/utils/schema-generators/factory.d.ts +27 -0
  46. package/dist/domains/seo/utils/schema-generators/factory.js +49 -0
  47. package/dist/domains/seo/utils/schema-generators/generators/article.d.ts +4 -0
  48. package/dist/domains/seo/utils/schema-generators/generators/article.js +39 -0
  49. package/dist/domains/seo/utils/schema-generators/generators/breadcrumb.d.ts +6 -0
  50. package/dist/domains/seo/utils/schema-generators/generators/breadcrumb.js +18 -0
  51. package/dist/domains/seo/utils/schema-generators/generators/course.d.ts +3 -0
  52. package/dist/domains/seo/utils/schema-generators/generators/course.js +70 -0
  53. package/dist/domains/seo/utils/schema-generators/generators/faq.d.ts +3 -0
  54. package/dist/domains/seo/utils/schema-generators/generators/faq.js +21 -0
  55. package/dist/domains/seo/utils/schema-generators/generators/local-business.d.ts +3 -0
  56. package/dist/domains/seo/utils/schema-generators/generators/local-business.js +45 -0
  57. package/dist/domains/seo/utils/schema-generators/generators/organization.d.ts +3 -0
  58. package/dist/domains/seo/utils/schema-generators/generators/organization.js +25 -0
  59. package/dist/domains/seo/utils/schema-generators/generators/person.d.ts +3 -0
  60. package/dist/domains/seo/utils/schema-generators/generators/person.js +27 -0
  61. package/dist/domains/seo/utils/schema-generators/generators/product.d.ts +3 -0
  62. package/dist/domains/seo/utils/schema-generators/generators/product.js +40 -0
  63. package/dist/domains/seo/utils/schema-generators/generators/video.d.ts +3 -0
  64. package/dist/domains/seo/utils/schema-generators/generators/video.js +23 -0
  65. package/dist/domains/seo/utils/schema-generators/generators/website.d.ts +3 -0
  66. package/dist/domains/seo/utils/schema-generators/generators/website.js +30 -0
  67. package/dist/domains/seo/utils/schema-generators/index.d.ts +15 -2
  68. package/dist/domains/seo/utils/schema-generators/index.js +13 -2
  69. package/dist/domains/seo/utils/schema-generators/types.d.ts +142 -0
  70. package/dist/domains/seo/utils/schema-generators/types.js +5 -0
  71. package/dist/domains/seo/utils/validation/hreflang-validator.d.ts +7 -0
  72. package/dist/domains/seo/utils/validation/hreflang-validator.js +13 -0
  73. package/dist/domains/seo/utils/validation/image-validator.d.ts +7 -0
  74. package/dist/domains/seo/utils/validation/image-validator.js +24 -0
  75. package/dist/domains/seo/utils/validation/index.d.ts +10 -2
  76. package/dist/domains/seo/utils/validation/index.js +10 -2
  77. package/dist/domains/seo/utils/validation/jsonld-validator.d.ts +9 -0
  78. package/dist/domains/seo/utils/validation/jsonld-validator.js +74 -0
  79. package/dist/domains/seo/utils/validation/language-code.d.ts +7 -0
  80. package/dist/domains/seo/utils/validation/language-code.js +8 -0
  81. package/dist/domains/seo/utils/validation/length-validator.d.ts +10 -0
  82. package/dist/domains/seo/utils/validation/length-validator.js +40 -0
  83. package/dist/domains/seo/utils/validation/seo-auditor.d.ts +17 -0
  84. package/dist/domains/seo/utils/validation/seo-auditor.js +144 -0
  85. package/dist/domains/seo/utils/validation/text-helpers.d.ts +7 -0
  86. package/dist/domains/seo/utils/validation/text-helpers.js +12 -0
  87. package/dist/domains/seo/utils/validation/url-validator.d.ts +8 -0
  88. package/dist/domains/seo/utils/validation/url-validator.js +26 -0
  89. package/dist/domains/seo/vite/index.d.ts +6 -4
  90. package/dist/domains/seo/vite/index.js +5 -3
  91. package/dist/domains/seo/vite/plugin.d.ts +3 -55
  92. package/dist/domains/seo/vite/plugin.js +28 -186
  93. package/dist/domains/seo/vite/presets.d.ts +14 -0
  94. package/dist/domains/seo/vite/presets.js +33 -0
  95. package/dist/domains/seo/vite/robots-generator.d.ts +9 -0
  96. package/dist/domains/seo/vite/robots-generator.js +36 -0
  97. package/dist/domains/seo/vite/sitemap-generator.d.ts +15 -0
  98. package/dist/domains/seo/vite/sitemap-generator.js +56 -0
  99. package/dist/domains/seo/vite/types.d.ts +29 -0
  100. package/dist/domains/seo/vite/types.js +4 -0
  101. package/dist/index.d.ts +8 -7
  102. package/dist/index.js +7 -6
  103. package/package.json +1 -1
@@ -0,0 +1,694 @@
1
+ /**
2
+ * SEO tag strategies (Strategy pattern).
3
+ *
4
+ * One strategy per concern. The same strategy is consumed by:
5
+ * - `dom/dom-mutator.ts` – applies the change to the live DOM
6
+ * - `ssr/renderer.ts` – serializes the change to an HTML string
7
+ *
8
+ * Replaces the historical god-functions in `dom.ts` and `renderer.ts` and
9
+ * fixes the divergence where SSR silently dropped OG image width/height.
10
+ */
11
+ import { escapeHTML, escapeJSONForScript } from "./escape.js";
12
+ const ATTR = {
13
+ rel: "rel",
14
+ href: "href",
15
+ type: "type",
16
+ sizes: "sizes",
17
+ media: "media",
18
+ as: "as",
19
+ crossorigin: "crossorigin",
20
+ hreflang: "hreflang",
21
+ color: "color",
22
+ };
23
+ const robotsDirectivesToMeta = (directives) => {
24
+ const meta = {};
25
+ for (const directive of directives) {
26
+ switch (directive) {
27
+ case "index":
28
+ meta.index = true;
29
+ break;
30
+ case "noindex":
31
+ meta.index = false;
32
+ break;
33
+ case "follow":
34
+ meta.follow = true;
35
+ break;
36
+ case "nofollow":
37
+ meta.follow = false;
38
+ break;
39
+ case "noarchive":
40
+ meta.archive = false;
41
+ break;
42
+ case "nosnippet":
43
+ meta.snippet = false;
44
+ break;
45
+ case "notranslate":
46
+ meta.translate = false;
47
+ break;
48
+ case "noimageindex":
49
+ meta.imageIndex = false;
50
+ break;
51
+ // "unavailable_after" needs a date and is exposed via RobotsMeta only.
52
+ }
53
+ }
54
+ return meta;
55
+ };
56
+ const robotsMetaToString = (robots) => {
57
+ if (Array.isArray(robots)) {
58
+ return robots.join(", ");
59
+ }
60
+ const directives = [];
61
+ const push = (cond, yes, no) => {
62
+ if (cond === undefined)
63
+ return;
64
+ directives.push(cond ? yes : no);
65
+ };
66
+ push(robots.index, "index", "noindex");
67
+ push(robots.follow, "follow", "nofollow");
68
+ push(robots.archive, "all", "noarchive");
69
+ push(robots.snippet, "all", "nosnippet");
70
+ push(robots.translate, "all", "notranslate");
71
+ push(robots.imageIndex, "all", "noimageindex");
72
+ if (robots.maxSnippet !== undefined)
73
+ directives.push(`max-snippet:${robots.maxSnippet}`);
74
+ if (robots.maxImagePreview !== undefined)
75
+ directives.push(`max-image-preview:${robots.maxImagePreview}`);
76
+ if (robots.maxVideoPreview !== undefined)
77
+ directives.push(`max-video-preview:${robots.maxVideoPreview}`);
78
+ if (robots.unavailableAfter !== undefined)
79
+ directives.push(`unavailable_after:${robots.unavailableAfter}`);
80
+ return directives.join(", ");
81
+ };
82
+ // ============================================================================
83
+ // Title
84
+ // ============================================================================
85
+ export const titleStrategy = {
86
+ id: "title",
87
+ applies: (o) => Boolean(o.title || o.titleTemplate),
88
+ applyDOM: (o) => {
89
+ const value = o.titleTemplate ? o.titleTemplate.replace("%s", o.title ?? "") : o.title;
90
+ if (value)
91
+ document.title = value;
92
+ },
93
+ applyHTML: (o, ctx) => {
94
+ const value = o.titleTemplate ? o.titleTemplate.replace("%s", o.title ?? "") : o.title;
95
+ ctx.meta.unshift(`<title>${escapeHTML(value ?? "")}</title>`);
96
+ },
97
+ };
98
+ // ============================================================================
99
+ // Language
100
+ // ============================================================================
101
+ export const langStrategy = {
102
+ id: "lang",
103
+ applies: (o) => Boolean(o.lang),
104
+ applyDOM: (o) => {
105
+ if (o.lang)
106
+ document.documentElement.lang = o.lang;
107
+ },
108
+ applyHTML: (o, ctx) => {
109
+ // <html lang> is emitted by the renderer wrapper, but we expose lang
110
+ // through a special sentinel line that the head wrapper reads.
111
+ if (o.lang)
112
+ ctx.meta.push(`<!-- lang:${o.lang} -->`);
113
+ },
114
+ };
115
+ // ============================================================================
116
+ // Description
117
+ // ============================================================================
118
+ export const descriptionStrategy = {
119
+ id: "description",
120
+ applies: (o) => Boolean(o.description),
121
+ applyDOM: (o, ctx) => {
122
+ if (o.description)
123
+ ctx.setMeta("description", o.description);
124
+ },
125
+ applyHTML: (o, ctx) => {
126
+ if (o.description)
127
+ ctx.meta.push(`<meta name="description" content="${escapeHTML(o.description)}" />`);
128
+ },
129
+ };
130
+ // ============================================================================
131
+ // Canonical
132
+ // ============================================================================
133
+ export const canonicalStrategy = {
134
+ id: "canonical",
135
+ applies: (o) => Boolean(o.canonical),
136
+ applyDOM: (o, ctx) => {
137
+ if (o.canonical)
138
+ ctx.setLink({ [ATTR.rel]: "canonical", [ATTR.href]: o.canonical });
139
+ },
140
+ applyHTML: (o, ctx) => {
141
+ if (o.canonical)
142
+ ctx.link.push(`<link rel="canonical" href="${escapeHTML(o.canonical)}" />`);
143
+ },
144
+ };
145
+ // ============================================================================
146
+ // Robots
147
+ // ============================================================================
148
+ export const robotsStrategy = {
149
+ id: "robots",
150
+ applies: (o) => o.robots !== undefined,
151
+ applyDOM: (o, ctx) => {
152
+ if (o.robots === undefined)
153
+ return;
154
+ const meta = Array.isArray(o.robots) ? robotsDirectivesToMeta(o.robots) : o.robots;
155
+ ctx.setMeta("robots", robotsMetaToString(meta));
156
+ },
157
+ applyHTML: (o, ctx) => {
158
+ if (o.robots === undefined)
159
+ return;
160
+ const meta = Array.isArray(o.robots) ? robotsDirectivesToMeta(o.robots) : o.robots;
161
+ ctx.meta.push(`<meta name="robots" content="${escapeHTML(robotsMetaToString(meta))}" />`);
162
+ },
163
+ };
164
+ // ============================================================================
165
+ // Viewport
166
+ // ============================================================================
167
+ export const viewportStrategy = {
168
+ id: "viewport",
169
+ applies: (o) => Boolean(o.viewport),
170
+ applyDOM: (o, ctx) => {
171
+ if (o.viewport)
172
+ ctx.setMeta("viewport", o.viewport);
173
+ },
174
+ applyHTML: (o, ctx) => {
175
+ if (o.viewport)
176
+ ctx.meta.push(`<meta name="viewport" content="${escapeHTML(o.viewport)}" />`);
177
+ },
178
+ };
179
+ // ============================================================================
180
+ // Theme color
181
+ // ============================================================================
182
+ export const themeColorStrategy = {
183
+ id: "theme-color",
184
+ applies: (o) => Boolean(o.themeColor),
185
+ applyDOM: (o, ctx) => {
186
+ if (o.themeColor)
187
+ ctx.setMeta("theme-color", o.themeColor);
188
+ },
189
+ applyHTML: (o, ctx) => {
190
+ if (o.themeColor)
191
+ ctx.meta.push(`<meta name="theme-color" content="${escapeHTML(o.themeColor)}" />`);
192
+ },
193
+ };
194
+ // ============================================================================
195
+ // Favicons
196
+ // ============================================================================
197
+ export const faviconStrategy = {
198
+ id: "favicons",
199
+ applies: (o) => Boolean(o.favicon || o.appleTouchIcon || o.maskIcon),
200
+ applyDOM: (o, ctx) => {
201
+ if (o.favicon)
202
+ ctx.setLink({ [ATTR.rel]: "icon", [ATTR.type]: "image/x-icon", [ATTR.href]: o.favicon });
203
+ if (o.appleTouchIcon)
204
+ ctx.setLink({ [ATTR.rel]: "apple-touch-icon", [ATTR.sizes]: "180x180", [ATTR.href]: o.appleTouchIcon });
205
+ if (o.maskIcon)
206
+ ctx.setLink({ [ATTR.rel]: "mask-icon", [ATTR.href]: o.maskIcon });
207
+ },
208
+ applyHTML: (o, ctx) => {
209
+ if (o.favicon)
210
+ ctx.link.push(`<link rel="icon" type="image/x-icon" href="${escapeHTML(o.favicon)}" />`);
211
+ if (o.appleTouchIcon)
212
+ ctx.link.push(`<link rel="apple-touch-icon" sizes="180x180" href="${escapeHTML(o.appleTouchIcon)}" />`);
213
+ if (o.maskIcon)
214
+ ctx.link.push(`<link rel="mask-icon" href="${escapeHTML(o.maskIcon)}" />`);
215
+ },
216
+ };
217
+ // ============================================================================
218
+ // Hreflang
219
+ // ============================================================================
220
+ export const hreflangStrategy = {
221
+ id: "hreflang",
222
+ applies: (o) => Boolean(o.hreflang && o.hreflang.length > 0),
223
+ applyDOM: (o, ctx) => {
224
+ if (!o.hreflang)
225
+ return;
226
+ // Drop stale hreflang tags first.
227
+ document.querySelectorAll('link[rel="alternate"][hreflang]').forEach((el) => el.remove());
228
+ for (const entry of o.hreflang) {
229
+ ctx.setLink({ [ATTR.rel]: "alternate", [ATTR.hreflang]: entry.lang, [ATTR.href]: entry.url });
230
+ }
231
+ },
232
+ applyHTML: (o, ctx) => {
233
+ if (!o.hreflang)
234
+ return;
235
+ for (const entry of o.hreflang) {
236
+ ctx.link.push(`<link rel="alternate" hreflang="${escapeHTML(entry.lang)}" href="${escapeHTML(entry.url)}" />`);
237
+ }
238
+ },
239
+ };
240
+ // ============================================================================
241
+ // Preload / Preconnect / DNS prefetch
242
+ // ============================================================================
243
+ const buildPreloadAttrs = (preload) => {
244
+ const attrs = { [ATTR.rel]: "preload", [ATTR.href]: preload.href, [ATTR.as]: preload.as };
245
+ if (preload.type)
246
+ attrs[ATTR.type] = preload.type;
247
+ if (preload.crossOrigin)
248
+ attrs[ATTR.crossorigin] = preload.crossOrigin;
249
+ if (preload.integrity)
250
+ attrs.integrity = preload.integrity;
251
+ return attrs;
252
+ };
253
+ const buildPreconnectAttrs = (origin) => {
254
+ const attrs = { [ATTR.rel]: "preconnect", [ATTR.href]: origin.href };
255
+ if (origin.crossOrigin)
256
+ attrs[ATTR.crossorigin] = origin.crossOrigin;
257
+ return attrs;
258
+ };
259
+ const htmlAttrs = (attrs) => Object.entries(attrs).map(([k, v]) => `${k}="${escapeHTML(v)}"`).join(" ");
260
+ export const preloadStrategy = {
261
+ id: "preload",
262
+ applies: (o) => Boolean(o.preload && o.preload.length > 0),
263
+ applyDOM: (o, ctx) => {
264
+ if (!o.preload)
265
+ return;
266
+ for (const preload of o.preload) {
267
+ const existing = document.querySelector(`link[rel="preload"][href="${preload.href}"]`);
268
+ if (existing)
269
+ existing.remove();
270
+ ctx.setLink(buildPreloadAttrs(preload));
271
+ }
272
+ },
273
+ applyHTML: (o, ctx) => {
274
+ if (!o.preload)
275
+ return;
276
+ for (const preload of o.preload) {
277
+ ctx.link.push(`<link ${htmlAttrs(buildPreloadAttrs(preload))} />`);
278
+ }
279
+ },
280
+ };
281
+ export const preconnectStrategy = {
282
+ id: "preconnect",
283
+ applies: (o) => Boolean(o.preconnect && o.preconnect.length > 0),
284
+ applyDOM: (o, ctx) => {
285
+ if (!o.preconnect)
286
+ return;
287
+ for (const origin of o.preconnect)
288
+ ctx.setLink(buildPreconnectAttrs(origin));
289
+ },
290
+ applyHTML: (o, ctx) => {
291
+ if (!o.preconnect)
292
+ return;
293
+ for (const origin of o.preconnect)
294
+ ctx.link.push(`<link ${htmlAttrs(buildPreconnectAttrs(origin))} />`);
295
+ },
296
+ };
297
+ export const dnsPrefetchStrategy = {
298
+ id: "dns-prefetch",
299
+ applies: (o) => Boolean(o.dnsPrefetch && o.dnsPrefetch.length > 0),
300
+ applyDOM: (o, ctx) => {
301
+ if (!o.dnsPrefetch)
302
+ return;
303
+ for (const domain of o.dnsPrefetch) {
304
+ ctx.setLink({ [ATTR.rel]: "dns-prefetch", [ATTR.href]: domain.href });
305
+ }
306
+ },
307
+ applyHTML: (o, ctx) => {
308
+ if (!o.dnsPrefetch)
309
+ return;
310
+ for (const domain of o.dnsPrefetch) {
311
+ ctx.link.push(`<link rel="dns-prefetch" href="${escapeHTML(domain.href)}" />`);
312
+ }
313
+ },
314
+ };
315
+ // ============================================================================
316
+ // Custom meta tags
317
+ // ============================================================================
318
+ export const customMetaStrategy = {
319
+ id: "custom-meta",
320
+ applies: (o) => Boolean(o.meta && o.meta.length > 0),
321
+ applyDOM: (o, ctx) => {
322
+ if (!o.meta)
323
+ return;
324
+ for (const m of o.meta) {
325
+ if (m.name)
326
+ ctx.setMeta(m.name, m.content);
327
+ else if (m.property)
328
+ ctx.setMeta(m.property, m.content);
329
+ else if (m.httpEquiv)
330
+ ctx.setMeta(m.httpEquiv, m.content);
331
+ }
332
+ },
333
+ applyHTML: (o, ctx) => {
334
+ if (!o.meta)
335
+ return;
336
+ for (const m of o.meta) {
337
+ if (m.name)
338
+ ctx.meta.push(`<meta name="${escapeHTML(m.name)}" content="${escapeHTML(m.content)}" />`);
339
+ else if (m.property)
340
+ ctx.meta.push(`<meta property="${escapeHTML(m.property)}" content="${escapeHTML(m.content)}" />`);
341
+ else if (m.httpEquiv)
342
+ ctx.meta.push(`<meta http-equiv="${escapeHTML(m.httpEquiv)}" content="${escapeHTML(m.content)}" />`);
343
+ }
344
+ },
345
+ };
346
+ // ============================================================================
347
+ // Custom link tags
348
+ // ============================================================================
349
+ export const customLinkStrategy = {
350
+ id: "custom-link",
351
+ applies: (o) => Boolean(o.link && o.link.length > 0),
352
+ applyDOM: (o, ctx) => {
353
+ if (!o.link)
354
+ return;
355
+ for (const link of o.link) {
356
+ const attrs = { [ATTR.rel]: link.rel, [ATTR.href]: link.href };
357
+ if (link.type)
358
+ attrs[ATTR.type] = link.type;
359
+ if (link.sizes)
360
+ attrs[ATTR.sizes] = link.sizes;
361
+ if (link.media)
362
+ attrs[ATTR.media] = link.media;
363
+ if (link.crossOrigin)
364
+ attrs[ATTR.crossorigin] = link.crossOrigin;
365
+ if (link.as)
366
+ attrs[ATTR.as] = link.as;
367
+ ctx.setLink(attrs);
368
+ }
369
+ },
370
+ applyHTML: (o, ctx) => {
371
+ if (!o.link)
372
+ return;
373
+ for (const link of o.link) {
374
+ const attrs = { [ATTR.rel]: link.rel, [ATTR.href]: link.href };
375
+ if (link.type)
376
+ attrs[ATTR.type] = link.type;
377
+ if (link.sizes)
378
+ attrs[ATTR.sizes] = link.sizes;
379
+ if (link.media)
380
+ attrs[ATTR.media] = link.media;
381
+ if (link.crossOrigin)
382
+ attrs[ATTR.crossorigin] = link.crossOrigin;
383
+ if (link.as)
384
+ attrs[ATTR.as] = link.as;
385
+ ctx.link.push(`<link ${htmlAttrs(attrs)} />`);
386
+ }
387
+ },
388
+ };
389
+ // ============================================================================
390
+ // Open Graph
391
+ // ============================================================================
392
+ const ogProperty = (name, value, ctx) => {
393
+ if (!value)
394
+ return;
395
+ ctx.meta.push(`<meta property="og:${name}" content="${escapeHTML(value)}" />`);
396
+ };
397
+ const ogImageTags = (image) => {
398
+ const sink = { meta: [] };
399
+ if (!image)
400
+ return sink;
401
+ if (Array.isArray(image)) {
402
+ // Spec: emit one <meta property="og:image" ...> per image, plus
403
+ // per-image og:image:width / :height / :alt / :type.
404
+ for (const img of image) {
405
+ ogProperty("image", img.url, sink);
406
+ if (img.width)
407
+ ogProperty("image:width", String(img.width), sink);
408
+ if (img.height)
409
+ ogProperty("image:height", String(img.height), sink);
410
+ if (img.alt)
411
+ ogProperty("image:alt", img.alt, sink);
412
+ if (img.type)
413
+ ogProperty("image:type", img.type, sink);
414
+ if (img.secureUrl)
415
+ ogProperty("image:secure_url", img.secureUrl, sink);
416
+ }
417
+ }
418
+ else {
419
+ ogProperty("image", image, sink);
420
+ }
421
+ return sink;
422
+ };
423
+ export const openGraphStrategy = {
424
+ id: "open-graph",
425
+ applies: (o) => Boolean(o.openGraph),
426
+ applyDOM: (o, ctx) => {
427
+ const og = o.openGraph;
428
+ if (!og)
429
+ return;
430
+ if (og.title)
431
+ ctx.setMeta("og:title", og.title);
432
+ if (og.description)
433
+ ctx.setMeta("og:description", og.description);
434
+ if (og.url)
435
+ ctx.setMeta("og:url", og.url);
436
+ if (og.type)
437
+ ctx.setMeta("og:type", og.type);
438
+ if (og.siteName)
439
+ ctx.setMeta("og:site_name", og.siteName);
440
+ if (og.locale)
441
+ ctx.setMeta("og:locale", og.locale);
442
+ if (og.image) {
443
+ if (Array.isArray(og.image)) {
444
+ for (const img of og.image) {
445
+ ctx.setMeta("og:image", img.url);
446
+ if (img.secureUrl)
447
+ ctx.setMeta("og:image:secure_url", img.secureUrl);
448
+ if (img.type)
449
+ ctx.setMeta("og:image:type", img.type);
450
+ if (img.width)
451
+ ctx.setMeta("og:image:width", String(img.width));
452
+ if (img.height)
453
+ ctx.setMeta("og:image:height", String(img.height));
454
+ if (img.alt)
455
+ ctx.setMeta("og:image:alt", img.alt);
456
+ }
457
+ }
458
+ else {
459
+ ctx.setMeta("og:image", og.image);
460
+ }
461
+ }
462
+ if (og.alternateLocale) {
463
+ og.alternateLocale.forEach((locale, index) => {
464
+ ctx.setMeta(`og:locale:alternate${index > 0 ? `:${index}` : ""}`, locale);
465
+ });
466
+ }
467
+ if (og.type === "article" && og.article) {
468
+ ctx.setMeta("og:article:published_time", og.article.publishedTime);
469
+ ctx.setMeta("og:article:modified_time", og.article.modifiedTime);
470
+ ctx.setMeta("og:article:expiration_time", og.article.expirationTime);
471
+ ctx.setMeta("og:article:author", og.article.author);
472
+ ctx.setMeta("og:article:section", og.article.section);
473
+ og.article.tag?.forEach((tag, index) => {
474
+ ctx.setMeta(`og:article:tag${index > 0 ? `:${index}` : ""}`, tag);
475
+ });
476
+ }
477
+ if (og.type === "product" && og.product) {
478
+ ctx.setMeta("og:product:price:amount", String(og.product.price));
479
+ ctx.setMeta("og:product:price:currency", og.product.priceCurrency);
480
+ ctx.setMeta("og:product:availability", og.product.availability);
481
+ ctx.setMeta("og:product:brand", og.product.brand);
482
+ ctx.setMeta("og:product:sku", og.product.sku);
483
+ ctx.setMeta("og:product:condition", og.product.condition);
484
+ }
485
+ if (og.type === "profile") {
486
+ ctx.setMeta("og:profile:first_name", og.firstName);
487
+ ctx.setMeta("og:profile:last_name", og.lastName);
488
+ ctx.setMeta("og:profile:username", og.username);
489
+ }
490
+ if (og.type === "music" && og.music) {
491
+ ctx.setMeta("og:music:album", og.music.album);
492
+ ctx.setMeta("og:music:musician", og.music.musician);
493
+ ctx.setMeta("og:music:song", og.music.song);
494
+ ctx.setMeta("og:music:release_date", og.music.releaseDate);
495
+ ctx.setMeta("og:music:duration", og.music.duration);
496
+ ctx.setMeta("og:music:creator", og.music.creator);
497
+ }
498
+ },
499
+ applyHTML: (o, ctx) => {
500
+ const og = o.openGraph;
501
+ if (!og)
502
+ return;
503
+ ogProperty("title", og.title, ctx);
504
+ ogProperty("description", og.description, ctx);
505
+ ogProperty("url", og.url, ctx);
506
+ ogProperty("type", og.type, ctx);
507
+ ogProperty("site_name", og.siteName, ctx);
508
+ ogProperty("locale", og.locale, ctx);
509
+ const imageSink = ogImageTags(og.image);
510
+ ctx.meta.push(...imageSink.meta);
511
+ og.alternateLocale?.forEach((locale, index) => {
512
+ ctx.meta.push(`<meta property="og:locale:alternate${index > 0 ? `:${index}` : ""}" content="${escapeHTML(locale)}" />`);
513
+ });
514
+ if (og.type === "article" && og.article) {
515
+ ogProperty("article:published_time", og.article.publishedTime, ctx);
516
+ ogProperty("article:modified_time", og.article.modifiedTime, ctx);
517
+ ogProperty("article:expiration_time", og.article.expirationTime, ctx);
518
+ ogProperty("article:author", og.article.author, ctx);
519
+ ogProperty("article:section", og.article.section, ctx);
520
+ og.article.tag?.forEach((tag, index) => {
521
+ ctx.meta.push(`<meta property="og:article:tag${index > 0 ? `:${index}` : ""}" content="${escapeHTML(tag)}" />`);
522
+ });
523
+ }
524
+ if (og.type === "product" && og.product) {
525
+ ogProperty("product:price:amount", String(og.product.price), ctx);
526
+ ogProperty("product:price:currency", og.product.priceCurrency, ctx);
527
+ ogProperty("product:availability", og.product.availability, ctx);
528
+ ogProperty("product:brand", og.product.brand, ctx);
529
+ ogProperty("product:sku", og.product.sku, ctx);
530
+ ogProperty("product:condition", og.product.condition, ctx);
531
+ }
532
+ if (og.type === "profile") {
533
+ ogProperty("profile:first_name", og.firstName, ctx);
534
+ ogProperty("profile:last_name", og.lastName, ctx);
535
+ ogProperty("profile:username", og.username, ctx);
536
+ }
537
+ if (og.type === "music" && og.music) {
538
+ ogProperty("music:album", og.music.album, ctx);
539
+ ogProperty("music:musician", og.music.musician, ctx);
540
+ ogProperty("music:song", og.music.song, ctx);
541
+ ogProperty("music:release_date", og.music.releaseDate, ctx);
542
+ ogProperty("music:duration", og.music.duration, ctx);
543
+ ogProperty("music:creator", og.music.creator, ctx);
544
+ }
545
+ },
546
+ };
547
+ // ============================================================================
548
+ // Twitter Card
549
+ // ============================================================================
550
+ /**
551
+ * Twitter cards should fall back to OpenGraph values for the common
552
+ * title/description/image fields when those are omitted. We only pick
553
+ * the overlapping fields — Twitter has no concept of OG's `firstName`,
554
+ * `product`, etc.
555
+ */
556
+ const twitterFallback = (twitter, openGraph) => ({
557
+ cardType: twitter.cardType,
558
+ site: twitter.site,
559
+ handle: twitter.handle,
560
+ title: twitter.title ?? openGraph?.title,
561
+ description: twitter.description ?? openGraph?.description,
562
+ image: twitter.image ?? (typeof openGraph?.image === "string" ? openGraph.image : undefined),
563
+ imageAlt: twitter.imageAlt,
564
+ player: twitter.player,
565
+ playerWidth: twitter.playerWidth,
566
+ playerHeight: twitter.playerHeight,
567
+ playerStream: twitter.playerStream,
568
+ playerStreamContentType: twitter.playerStreamContentType,
569
+ app: twitter.app,
570
+ });
571
+ export const twitterStrategy = {
572
+ id: "twitter",
573
+ applies: (o) => Boolean(o.twitter),
574
+ applyDOM: (o, ctx) => {
575
+ if (!o.twitter)
576
+ return;
577
+ const tw = twitterFallback(o.twitter, o.openGraph);
578
+ if (tw.cardType)
579
+ ctx.setMeta("twitter:card", tw.cardType);
580
+ if (tw.site)
581
+ ctx.setMeta("twitter:site", tw.site);
582
+ if (tw.handle)
583
+ ctx.setMeta("twitter:creator", tw.handle);
584
+ if (tw.title)
585
+ ctx.setMeta("twitter:title", tw.title);
586
+ if (tw.description)
587
+ ctx.setMeta("twitter:description", tw.description);
588
+ if (tw.image)
589
+ ctx.setMeta("twitter:image", tw.image);
590
+ if (tw.imageAlt)
591
+ ctx.setMeta("twitter:image:alt", tw.imageAlt);
592
+ if (tw.cardType === "player") {
593
+ ctx.setMeta("twitter:player", tw.player);
594
+ ctx.setMeta("twitter:player:width", tw.playerWidth);
595
+ ctx.setMeta("twitter:player:height", tw.playerHeight);
596
+ ctx.setMeta("twitter:player:stream", tw.playerStream);
597
+ ctx.setMeta("twitter:player:stream:content_type", tw.playerStreamContentType);
598
+ }
599
+ if (tw.app?.iphone) {
600
+ ctx.setMeta("twitter:app:name:iphone", tw.app.iphone.name);
601
+ ctx.setMeta("twitter:app:id:iphone", tw.app.iphone.id);
602
+ ctx.setMeta("twitter:app:url:iphone", tw.app.iphone.url);
603
+ }
604
+ if (tw.app?.ipad) {
605
+ ctx.setMeta("twitter:app:name:ipad", tw.app.ipad.name);
606
+ ctx.setMeta("twitter:app:id:ipad", tw.app.ipad.id);
607
+ ctx.setMeta("twitter:app:url:ipad", tw.app.ipad.url);
608
+ }
609
+ if (tw.app?.googleplay) {
610
+ ctx.setMeta("twitter:app:name:googleplay", tw.app.googleplay.name);
611
+ ctx.setMeta("twitter:app:id:googleplay", tw.app.googleplay.id);
612
+ ctx.setMeta("twitter:app:url:googleplay", tw.app.googleplay.url);
613
+ }
614
+ },
615
+ applyHTML: (o, ctx) => {
616
+ if (!o.twitter)
617
+ return;
618
+ const tw = twitterFallback(o.twitter, o.openGraph);
619
+ const push = (name, value) => {
620
+ if (value)
621
+ ctx.meta.push(`<meta name="twitter:${name}" content="${escapeHTML(value)}" />`);
622
+ };
623
+ push("card", tw.cardType);
624
+ push("site", tw.site);
625
+ push("creator", tw.handle);
626
+ push("title", tw.title);
627
+ push("description", tw.description);
628
+ push("image", tw.image);
629
+ push("image:alt", tw.imageAlt);
630
+ if (tw.cardType === "player") {
631
+ push("player", tw.player);
632
+ push("player:width", tw.playerWidth);
633
+ push("player:height", tw.playerHeight);
634
+ push("player:stream", tw.playerStream);
635
+ push("player:stream:content_type", tw.playerStreamContentType);
636
+ }
637
+ if (tw.app?.iphone) {
638
+ push("app:name:iphone", tw.app.iphone.name);
639
+ push("app:id:iphone", tw.app.iphone.id);
640
+ push("app:url:iphone", tw.app.iphone.url);
641
+ }
642
+ if (tw.app?.ipad) {
643
+ push("app:name:ipad", tw.app.ipad.name);
644
+ push("app:id:ipad", tw.app.ipad.id);
645
+ push("app:url:ipad", tw.app.ipad.url);
646
+ }
647
+ if (tw.app?.googleplay) {
648
+ push("app:name:googleplay", tw.app.googleplay.name);
649
+ push("app:id:googleplay", tw.app.googleplay.id);
650
+ push("app:url:googleplay", tw.app.googleplay.url);
651
+ }
652
+ },
653
+ };
654
+ // ============================================================================
655
+ // JSON-LD
656
+ // ============================================================================
657
+ export const jsonLdStrategy = {
658
+ id: "json-ld",
659
+ applies: (o) => Boolean(o.jsonLd && o.jsonLd.length > 0),
660
+ applyDOM: () => {
661
+ // JSON-LD injection is owned by the JsonLd component / useJsonLd hook
662
+ // because each <script> needs a stable id, conflict resolution, and
663
+ // cleanup on unmount. A strategy that "applies" it would have to
664
+ // duplicate all of that logic, so we no-op here intentionally.
665
+ },
666
+ applyHTML: (o, ctx) => {
667
+ if (!o.jsonLd)
668
+ return;
669
+ for (const data of o.jsonLd) {
670
+ ctx.jsonLd.push(`<script type="application/ld+json">${escapeJSONForScript(JSON.stringify(data))}</script>`);
671
+ }
672
+ },
673
+ };
674
+ // ============================================================================
675
+ // Registry
676
+ // ============================================================================
677
+ export const SEO_STRATEGIES = Object.freeze([
678
+ titleStrategy,
679
+ descriptionStrategy,
680
+ canonicalStrategy,
681
+ robotsStrategy,
682
+ viewportStrategy,
683
+ themeColorStrategy,
684
+ faviconStrategy,
685
+ hreflangStrategy,
686
+ preloadStrategy,
687
+ preconnectStrategy,
688
+ dnsPrefetchStrategy,
689
+ customMetaStrategy,
690
+ customLinkStrategy,
691
+ openGraphStrategy,
692
+ twitterStrategy,
693
+ jsonLdStrategy,
694
+ ]);