@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.
- package/dist/domains/seo/components/JsonLd.d.ts +7 -6
- package/dist/domains/seo/components/JsonLd.js +26 -77
- package/dist/domains/seo/components/SEO.d.ts +7 -0
- package/dist/domains/seo/components/SEO.js +10 -33
- package/dist/domains/seo/core/constants.d.ts +15 -0
- package/dist/domains/seo/core/constants.js +15 -0
- package/dist/domains/seo/core/deep-equal.d.ts +8 -0
- package/dist/domains/seo/core/deep-equal.js +28 -0
- package/dist/domains/seo/core/dom-queue.d.ts +10 -0
- package/dist/domains/seo/core/dom-queue.js +37 -0
- package/dist/domains/seo/core/escape.d.ts +12 -0
- package/dist/domains/seo/core/escape.js +41 -0
- package/dist/domains/seo/core/hash.d.ts +9 -0
- package/dist/domains/seo/core/hash.js +29 -0
- package/dist/domains/seo/core/init.d.ts +15 -2
- package/dist/domains/seo/core/init.js +38 -18
- package/dist/domains/seo/core/seo-strategies.d.ts +29 -0
- package/dist/domains/seo/core/seo-strategies.js +694 -0
- package/dist/domains/seo/core/seo-strategy.d.ts +37 -0
- package/dist/domains/seo/core/seo-strategy.js +14 -0
- package/dist/domains/seo/hooks/useJsonLd.d.ts +6 -7
- package/dist/domains/seo/hooks/useJsonLd.js +27 -61
- package/dist/domains/seo/hooks/useSEO.d.ts +6 -0
- package/dist/domains/seo/hooks/useSEO.js +7 -29
- package/dist/domains/seo/ssr/helmet.d.ts +25 -0
- package/dist/domains/seo/ssr/helmet.js +23 -0
- package/dist/domains/seo/ssr/index.d.ts +10 -0
- package/dist/domains/seo/ssr/index.js +8 -0
- package/dist/domains/seo/ssr/renderer.d.ts +11 -36
- package/dist/domains/seo/ssr/renderer.js +51 -285
- package/dist/domains/seo/ssr/serializer.d.ts +5 -60
- package/dist/domains/seo/ssr/serializer.js +26 -166
- package/dist/domains/seo/types/index.d.ts +0 -4
- package/dist/domains/seo/types/schema.d.ts +22 -1
- package/dist/domains/seo/utils/dom/dom-context.d.ts +13 -0
- package/dist/domains/seo/utils/dom/dom-context.js +62 -0
- package/dist/domains/seo/utils/dom/dom-mutator.d.ts +31 -0
- package/dist/domains/seo/utils/dom/dom-mutator.js +89 -0
- package/dist/domains/seo/utils/dom/index.d.ts +1 -0
- package/dist/domains/seo/utils/dom/index.js +1 -0
- package/dist/domains/seo/utils/index.d.ts +3 -4
- package/dist/domains/seo/utils/index.js +3 -5
- package/dist/domains/seo/utils/schema-generators/base.d.ts +25 -0
- package/dist/domains/seo/utils/schema-generators/base.js +50 -0
- package/dist/domains/seo/utils/schema-generators/factory.d.ts +27 -0
- package/dist/domains/seo/utils/schema-generators/factory.js +49 -0
- package/dist/domains/seo/utils/schema-generators/generators/article.d.ts +4 -0
- package/dist/domains/seo/utils/schema-generators/generators/article.js +39 -0
- package/dist/domains/seo/utils/schema-generators/generators/breadcrumb.d.ts +6 -0
- package/dist/domains/seo/utils/schema-generators/generators/breadcrumb.js +18 -0
- package/dist/domains/seo/utils/schema-generators/generators/course.d.ts +3 -0
- package/dist/domains/seo/utils/schema-generators/generators/course.js +70 -0
- package/dist/domains/seo/utils/schema-generators/generators/faq.d.ts +3 -0
- package/dist/domains/seo/utils/schema-generators/generators/faq.js +21 -0
- package/dist/domains/seo/utils/schema-generators/generators/local-business.d.ts +3 -0
- package/dist/domains/seo/utils/schema-generators/generators/local-business.js +45 -0
- package/dist/domains/seo/utils/schema-generators/generators/organization.d.ts +3 -0
- package/dist/domains/seo/utils/schema-generators/generators/organization.js +25 -0
- package/dist/domains/seo/utils/schema-generators/generators/person.d.ts +3 -0
- package/dist/domains/seo/utils/schema-generators/generators/person.js +27 -0
- package/dist/domains/seo/utils/schema-generators/generators/product.d.ts +3 -0
- package/dist/domains/seo/utils/schema-generators/generators/product.js +40 -0
- package/dist/domains/seo/utils/schema-generators/generators/video.d.ts +3 -0
- package/dist/domains/seo/utils/schema-generators/generators/video.js +23 -0
- package/dist/domains/seo/utils/schema-generators/generators/website.d.ts +3 -0
- package/dist/domains/seo/utils/schema-generators/generators/website.js +30 -0
- package/dist/domains/seo/utils/schema-generators/index.d.ts +15 -2
- package/dist/domains/seo/utils/schema-generators/index.js +13 -2
- package/dist/domains/seo/utils/schema-generators/types.d.ts +142 -0
- package/dist/domains/seo/utils/schema-generators/types.js +5 -0
- package/dist/domains/seo/utils/validation/hreflang-validator.d.ts +7 -0
- package/dist/domains/seo/utils/validation/hreflang-validator.js +13 -0
- package/dist/domains/seo/utils/validation/image-validator.d.ts +7 -0
- package/dist/domains/seo/utils/validation/image-validator.js +24 -0
- package/dist/domains/seo/utils/validation/index.d.ts +10 -2
- package/dist/domains/seo/utils/validation/index.js +10 -2
- package/dist/domains/seo/utils/validation/jsonld-validator.d.ts +9 -0
- package/dist/domains/seo/utils/validation/jsonld-validator.js +74 -0
- package/dist/domains/seo/utils/validation/language-code.d.ts +7 -0
- package/dist/domains/seo/utils/validation/language-code.js +8 -0
- package/dist/domains/seo/utils/validation/length-validator.d.ts +10 -0
- package/dist/domains/seo/utils/validation/length-validator.js +40 -0
- package/dist/domains/seo/utils/validation/seo-auditor.d.ts +17 -0
- package/dist/domains/seo/utils/validation/seo-auditor.js +144 -0
- package/dist/domains/seo/utils/validation/text-helpers.d.ts +7 -0
- package/dist/domains/seo/utils/validation/text-helpers.js +12 -0
- package/dist/domains/seo/utils/validation/url-validator.d.ts +8 -0
- package/dist/domains/seo/utils/validation/url-validator.js +26 -0
- package/dist/domains/seo/vite/index.d.ts +6 -4
- package/dist/domains/seo/vite/index.js +5 -3
- package/dist/domains/seo/vite/plugin.d.ts +3 -55
- package/dist/domains/seo/vite/plugin.js +28 -186
- package/dist/domains/seo/vite/presets.d.ts +14 -0
- package/dist/domains/seo/vite/presets.js +33 -0
- package/dist/domains/seo/vite/robots-generator.d.ts +9 -0
- package/dist/domains/seo/vite/robots-generator.js +36 -0
- package/dist/domains/seo/vite/sitemap-generator.d.ts +15 -0
- package/dist/domains/seo/vite/sitemap-generator.js +56 -0
- package/dist/domains/seo/vite/types.d.ts +29 -0
- package/dist/domains/seo/vite/types.js +4 -0
- package/dist/index.d.ts +8 -7
- package/dist/index.js +7 -6
- package/package.json +1 -1
|
@@ -1,253 +1,44 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* SEO SSR Renderer
|
|
2
|
+
* SEO SSR Renderer.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
* Generates HTML strings for meta tags and structured data. Uses the same
|
|
5
|
+
* strategy registry as the DOM mutator, so DOM and SSR output are
|
|
6
|
+
* guaranteed to stay in sync.
|
|
6
7
|
*/
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
if (escapeCache.has(str)) {
|
|
21
|
-
return escapeCache.get(str);
|
|
22
|
-
}
|
|
23
|
-
// Single-pass replacement for better performance
|
|
24
|
-
const result = str.replace(/[&<>"']/g, (char) => {
|
|
25
|
-
switch (char) {
|
|
26
|
-
case '&': return '&';
|
|
27
|
-
case '<': return '<';
|
|
28
|
-
case '>': return '>';
|
|
29
|
-
case '"': return '"';
|
|
30
|
-
case "'": return ''';
|
|
31
|
-
default: return char;
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
|
-
// Cache the result (limit cache size to prevent memory issues)
|
|
35
|
-
if (escapeCache.size < 1000) {
|
|
36
|
-
escapeCache.set(str, result);
|
|
37
|
-
}
|
|
38
|
-
return result;
|
|
39
|
-
};
|
|
40
|
-
/**
|
|
41
|
-
* Escape JSON content for safe inclusion in HTML script tags
|
|
42
|
-
* Prevents script breakage from </script> sequences in JSON data
|
|
43
|
-
* Optimized with caching
|
|
44
|
-
*/
|
|
45
|
-
const escapeJSONForScript = (json) => {
|
|
46
|
-
// Cache JSON escapes for better performance
|
|
47
|
-
const cacheKey = `json:${json}`;
|
|
48
|
-
if (escapeCache.has(cacheKey)) {
|
|
49
|
-
return escapeCache.get(cacheKey);
|
|
50
|
-
}
|
|
51
|
-
// Replace </script> with <\/script> to prevent breaking out of script tag
|
|
52
|
-
const result = json.replace(/<\/script>/gi, "<\\/script>");
|
|
53
|
-
if (escapeCache.size < 1000) {
|
|
54
|
-
escapeCache.set(cacheKey, result);
|
|
55
|
-
}
|
|
56
|
-
return result;
|
|
8
|
+
import { escapeHTML } from "../core/escape.js";
|
|
9
|
+
import { SEO_STRATEGIES } from "../core/seo-strategies.js";
|
|
10
|
+
import { updateSEO } from "../utils/dom/index.js";
|
|
11
|
+
const buildHTMLContext = () => ({ meta: [], link: [], jsonLd: [] });
|
|
12
|
+
const SEPARATOR = "\n ";
|
|
13
|
+
const joinTags = (ctx) => ({
|
|
14
|
+
meta: ctx.meta.join(SEPARATOR),
|
|
15
|
+
link: ctx.link.join(SEPARATOR),
|
|
16
|
+
jsonLd: ctx.jsonLd.join(SEPARATOR),
|
|
17
|
+
});
|
|
18
|
+
const findTitle = (meta) => {
|
|
19
|
+
const match = meta.match(/<title>([\s\S]*?)<\/title>/);
|
|
20
|
+
return match ? match[1] : "";
|
|
57
21
|
};
|
|
58
|
-
|
|
59
|
-
const renderCache = new WeakMap();
|
|
22
|
+
const stripTitle = (meta) => meta.replace(/<title>[\s\S]*?<\/title>\n?/, "");
|
|
60
23
|
/**
|
|
61
|
-
* Render SEO options to HTML strings for server-side rendering
|
|
62
|
-
* Optimized with caching and efficient string building
|
|
24
|
+
* Render SEO options to HTML strings for server-side rendering.
|
|
63
25
|
*/
|
|
64
|
-
export const renderSEO = (options
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
const
|
|
72
|
-
const
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
? escapeHTML(options.titleTemplate.replace("%s", options.title || ""))
|
|
76
|
-
: escapeHTML(options.title || "");
|
|
77
|
-
// Charset
|
|
78
|
-
metaTagsBuilder.push('<meta charset="utf-8" />');
|
|
79
|
-
metaTagsBuilder.push('<meta name="viewport" content="width=device-width, initial-scale=1" />');
|
|
80
|
-
// Description
|
|
81
|
-
if (options.description) {
|
|
82
|
-
metaTagsBuilder.push(`<meta name="description" content="${escapeHTML(options.description)}" />`);
|
|
83
|
-
}
|
|
84
|
-
// Robots
|
|
85
|
-
if (options.robots !== undefined) {
|
|
86
|
-
let robotsContent = "";
|
|
87
|
-
if (typeof options.robots === "boolean") {
|
|
88
|
-
robotsContent = options.robots ? "index, follow" : "noindex, nofollow";
|
|
89
|
-
}
|
|
90
|
-
else if (Array.isArray(options.robots)) {
|
|
91
|
-
robotsContent = options.robots.join(", ");
|
|
92
|
-
}
|
|
93
|
-
else {
|
|
94
|
-
const directives = [];
|
|
95
|
-
if (options.robots.index !== undefined)
|
|
96
|
-
directives.push(options.robots.index ? "index" : "noindex");
|
|
97
|
-
if (options.robots.follow !== undefined)
|
|
98
|
-
directives.push(options.robots.follow ? "follow" : "nofollow");
|
|
99
|
-
if (options.robots.archive !== undefined)
|
|
100
|
-
directives.push(options.robots.archive ? "all" : "noarchive");
|
|
101
|
-
if (options.robots.snippet !== undefined)
|
|
102
|
-
directives.push(options.robots.snippet ? "all" : "nosnippet");
|
|
103
|
-
robotsContent = directives.join(", ");
|
|
104
|
-
}
|
|
105
|
-
metaTagsBuilder.push(`<meta name="robots" content="${robotsContent}" />`);
|
|
106
|
-
}
|
|
107
|
-
// Canonical
|
|
108
|
-
if (options.canonical) {
|
|
109
|
-
linkTagsBuilder.push(`<link rel="canonical" href="${escapeHTML(options.canonical)}" />`);
|
|
110
|
-
}
|
|
111
|
-
// Theme color
|
|
112
|
-
if (options.themeColor) {
|
|
113
|
-
metaTagsBuilder.push(`<meta name="theme-color" content="${escapeHTML(options.themeColor)}" />`);
|
|
114
|
-
}
|
|
115
|
-
// Favicon
|
|
116
|
-
if (options.favicon) {
|
|
117
|
-
linkTagsBuilder.push(`<link rel="icon" type="image/x-icon" href="${escapeHTML(options.favicon)}" />`);
|
|
118
|
-
}
|
|
119
|
-
if (options.appleTouchIcon) {
|
|
120
|
-
linkTagsBuilder.push(`<link rel="apple-touch-icon" sizes="180x180" href="${escapeHTML(options.appleTouchIcon)}" />`);
|
|
121
|
-
}
|
|
122
|
-
if (options.maskIcon) {
|
|
123
|
-
linkTagsBuilder.push(`<link rel="mask-icon" href="${escapeHTML(options.maskIcon)}" />`);
|
|
124
|
-
}
|
|
125
|
-
// Hreflang
|
|
126
|
-
if (options.hreflang && options.hreflang.length > 0) {
|
|
127
|
-
for (const entry of options.hreflang) {
|
|
128
|
-
linkTagsBuilder.push(`<link rel="alternate" hreflang="${escapeHTML(entry.lang)}" href="${escapeHTML(entry.url)}" />`);
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
// Preload
|
|
132
|
-
if (options.preload && options.preload.length > 0) {
|
|
133
|
-
for (const preload of options.preload) {
|
|
134
|
-
const attrs = [`rel="preload"`, `href="${escapeHTML(preload.href)}"`, `as="${escapeHTML(preload.as)}"`];
|
|
135
|
-
if (preload.type)
|
|
136
|
-
attrs.push(`type="${escapeHTML(preload.type)}"`);
|
|
137
|
-
if (preload.crossOrigin)
|
|
138
|
-
attrs.push(`crossorigin="${escapeHTML(preload.crossOrigin)}"`);
|
|
139
|
-
if (preload.integrity)
|
|
140
|
-
attrs.push(`integrity="${escapeHTML(preload.integrity)}"`);
|
|
141
|
-
linkTagsBuilder.push(`<link ${attrs.join(" ")} />`);
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
// Preconnect
|
|
145
|
-
if (options.preconnect && options.preconnect.length > 0) {
|
|
146
|
-
for (const origin of options.preconnect) {
|
|
147
|
-
const attrs = [`rel="preconnect"`, `href="${escapeHTML(origin.href)}"`];
|
|
148
|
-
if (origin.crossOrigin)
|
|
149
|
-
attrs.push(`crossorigin="${escapeHTML(origin.crossOrigin)}"`);
|
|
150
|
-
linkTagsBuilder.push(`<link ${attrs.join(" ")} />`);
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
// DNS Prefetch
|
|
154
|
-
if (options.dnsPrefetch && options.dnsPrefetch.length > 0) {
|
|
155
|
-
for (const domain of options.dnsPrefetch) {
|
|
156
|
-
linkTagsBuilder.push(`<link rel="dns-prefetch" href="${escapeHTML(domain.href)}" />`);
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
// Custom meta tags
|
|
160
|
-
if (options.meta && options.meta.length > 0) {
|
|
161
|
-
for (const m of options.meta) {
|
|
162
|
-
if (m.name) {
|
|
163
|
-
metaTagsBuilder.push(`<meta name="${escapeHTML(m.name)}" content="${escapeHTML(m.content)}" />`);
|
|
164
|
-
}
|
|
165
|
-
if (m.property) {
|
|
166
|
-
metaTagsBuilder.push(`<meta property="${escapeHTML(m.property)}" content="${escapeHTML(m.content)}" />`);
|
|
167
|
-
}
|
|
168
|
-
if (m.httpEquiv) {
|
|
169
|
-
metaTagsBuilder.push(`<meta http-equiv="${escapeHTML(m.httpEquiv)}" content="${escapeHTML(m.content)}" />`);
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
// Custom link tags
|
|
174
|
-
if (options.link && options.link.length > 0) {
|
|
175
|
-
for (const link of options.link) {
|
|
176
|
-
const attrs = [`rel="${escapeHTML(link.rel)}"`, `href="${escapeHTML(link.href)}"`];
|
|
177
|
-
if (link.type)
|
|
178
|
-
attrs.push(`type="${escapeHTML(link.type)}"`);
|
|
179
|
-
if (link.sizes)
|
|
180
|
-
attrs.push(`sizes="${escapeHTML(link.sizes)}"`);
|
|
181
|
-
if (link.media)
|
|
182
|
-
attrs.push(`media="${escapeHTML(link.media)}"`);
|
|
183
|
-
if (link.crossOrigin)
|
|
184
|
-
attrs.push(`crossorigin="${escapeHTML(link.crossOrigin)}"`);
|
|
185
|
-
if (link.as)
|
|
186
|
-
attrs.push(`as="${escapeHTML(link.as)}"`);
|
|
187
|
-
linkTagsBuilder.push(`<link ${attrs.join(" ")} />`);
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
// Open Graph
|
|
191
|
-
if (options.openGraph) {
|
|
192
|
-
if (options.openGraph.title)
|
|
193
|
-
metaTagsBuilder.push(`<meta property="og:title" content="${escapeHTML(options.openGraph.title)}" />`);
|
|
194
|
-
if (options.openGraph.description)
|
|
195
|
-
metaTagsBuilder.push(`<meta property="og:description" content="${escapeHTML(options.openGraph.description)}" />`);
|
|
196
|
-
if (options.openGraph.url)
|
|
197
|
-
metaTagsBuilder.push(`<meta property="og:url" content="${escapeHTML(options.openGraph.url)}" />`);
|
|
198
|
-
if (options.openGraph.type)
|
|
199
|
-
metaTagsBuilder.push(`<meta property="og:type" content="${escapeHTML(options.openGraph.type)}" />`);
|
|
200
|
-
if (options.openGraph.siteName)
|
|
201
|
-
metaTagsBuilder.push(`<meta property="og:site_name" content="${escapeHTML(options.openGraph.siteName)}" />`);
|
|
202
|
-
if (options.openGraph.locale)
|
|
203
|
-
metaTagsBuilder.push(`<meta property="og:locale" content="${escapeHTML(options.openGraph.locale)}" />`);
|
|
204
|
-
// Images
|
|
205
|
-
if (options.openGraph.image) {
|
|
206
|
-
if (Array.isArray(options.openGraph.image)) {
|
|
207
|
-
for (const img of options.openGraph.image) {
|
|
208
|
-
metaTagsBuilder.push(`<meta property="og:image" content="${escapeHTML(img.url)}" />`);
|
|
209
|
-
if (img.alt)
|
|
210
|
-
metaTagsBuilder.push(`<meta property="og:image:alt" content="${escapeHTML(img.alt)}" />`);
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
else {
|
|
214
|
-
metaTagsBuilder.push(`<meta property="og:image" content="${escapeHTML(options.openGraph.image)}" />`);
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
// Twitter Card
|
|
219
|
-
if (options.twitter) {
|
|
220
|
-
if (options.twitter.cardType)
|
|
221
|
-
metaTagsBuilder.push(`<meta name="twitter:card" content="${escapeHTML(options.twitter.cardType)}" />`);
|
|
222
|
-
if (options.twitter.site)
|
|
223
|
-
metaTagsBuilder.push(`<meta name="twitter:site" content="${escapeHTML(options.twitter.site)}" />`);
|
|
224
|
-
if (options.twitter.handle)
|
|
225
|
-
metaTagsBuilder.push(`<meta name="twitter:creator" content="${escapeHTML(options.twitter.handle)}" />`);
|
|
226
|
-
if (options.twitter.title)
|
|
227
|
-
metaTagsBuilder.push(`<meta name="twitter:title" content="${escapeHTML(options.twitter.title)}" />`);
|
|
228
|
-
if (options.twitter.description)
|
|
229
|
-
metaTagsBuilder.push(`<meta name="twitter:description" content="${escapeHTML(options.twitter.description)}" />`);
|
|
230
|
-
if (options.twitter.image)
|
|
231
|
-
metaTagsBuilder.push(`<meta name="twitter:image" content="${escapeHTML(options.twitter.image)}" />`);
|
|
232
|
-
if (options.twitter.imageAlt)
|
|
233
|
-
metaTagsBuilder.push(`<meta name="twitter:image:alt" content="${escapeHTML(options.twitter.imageAlt)}" />`);
|
|
234
|
-
}
|
|
235
|
-
// JSON-LD
|
|
236
|
-
if (options.jsonLd && options.jsonLd.length > 0) {
|
|
237
|
-
for (const data of options.jsonLd) {
|
|
238
|
-
const json = JSON.stringify(data);
|
|
239
|
-
const safeJson = escapeJSONForScript(json);
|
|
240
|
-
jsonLdScriptsBuilder.push(`<script type="application/ld+json">${safeJson}</script>`);
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
// Build result using cached arrays
|
|
244
|
-
const result = {
|
|
26
|
+
export const renderSEO = (options) => {
|
|
27
|
+
const ctx = buildHTMLContext();
|
|
28
|
+
for (const strategy of SEO_STRATEGIES) {
|
|
29
|
+
if (!strategy.applies(options))
|
|
30
|
+
continue;
|
|
31
|
+
strategy.applyHTML(options, ctx);
|
|
32
|
+
}
|
|
33
|
+
const joined = joinTags(ctx);
|
|
34
|
+
const title = findTitle(joined.meta);
|
|
35
|
+
const meta = stripTitle(joined.meta);
|
|
36
|
+
return {
|
|
245
37
|
title,
|
|
246
|
-
meta
|
|
247
|
-
link:
|
|
248
|
-
jsonLd:
|
|
38
|
+
meta,
|
|
39
|
+
link: joined.link,
|
|
40
|
+
jsonLd: joined.jsonLd,
|
|
249
41
|
toHTML() {
|
|
250
|
-
// Use template literals for better performance
|
|
251
42
|
const parts = [` <title>${this.title}</title>`];
|
|
252
43
|
if (this.meta)
|
|
253
44
|
parts.push(` ${this.meta}`);
|
|
@@ -261,33 +52,28 @@ export const renderSEO = (options, useCache = true) => {
|
|
|
261
52
|
return this.toHTML();
|
|
262
53
|
},
|
|
263
54
|
};
|
|
264
|
-
// Cache the result
|
|
265
|
-
if (useCache) {
|
|
266
|
-
renderCache.set(options, result);
|
|
267
|
-
}
|
|
268
|
-
return result;
|
|
269
55
|
};
|
|
270
|
-
// ============================================================================
|
|
271
|
-
// SSR SEO Utilities
|
|
272
|
-
// ============================================================================
|
|
273
56
|
/**
|
|
274
|
-
* Generate complete
|
|
57
|
+
* Generate a complete `<head>` block for the given SEO options.
|
|
275
58
|
*/
|
|
276
59
|
export const generateHead = (options) => {
|
|
277
60
|
const rendered = renderSEO(options);
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
61
|
+
const langAttr = options.lang ? ` lang="${escapeHTML(options.lang)}"` : "";
|
|
62
|
+
const indented = rendered
|
|
63
|
+
.toHTML()
|
|
64
|
+
.split("\n")
|
|
65
|
+
.map((line) => ` ${line}`)
|
|
66
|
+
.join("\n");
|
|
67
|
+
return ` <head${langAttr}>\n${indented}\n </head>`;
|
|
281
68
|
};
|
|
282
69
|
/**
|
|
283
|
-
* Extract SEO
|
|
70
|
+
* Extract SEO options as a JSON string for client-side hydration.
|
|
284
71
|
*/
|
|
285
|
-
export const extractSEOData = (options) =>
|
|
286
|
-
return JSON.stringify(options);
|
|
287
|
-
};
|
|
72
|
+
export const extractSEOData = (options) => JSON.stringify(options);
|
|
288
73
|
/**
|
|
289
|
-
* Hydrate SEO on client
|
|
290
|
-
*
|
|
74
|
+
* Hydrate SEO on the client by applying the deserialized options to the DOM.
|
|
75
|
+
* Swallows JSON parse errors and dynamic-import errors with a console error
|
|
76
|
+
* — the page should still render even if hydration fails.
|
|
291
77
|
*/
|
|
292
78
|
export const hydrateSEO = (serializedData) => {
|
|
293
79
|
if (typeof window === "undefined")
|
|
@@ -300,30 +86,10 @@ export const hydrateSEO = (serializedData) => {
|
|
|
300
86
|
console.error("[SEO] Failed to hydrate SEO data:", error);
|
|
301
87
|
return;
|
|
302
88
|
}
|
|
303
|
-
|
|
304
|
-
import("../utils/dom.js")
|
|
305
|
-
.then(({ updateSEO }) => {
|
|
89
|
+
try {
|
|
306
90
|
updateSEO(options);
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
console.error("[SEO] Failed to
|
|
310
|
-
}
|
|
311
|
-
};
|
|
312
|
-
/**
|
|
313
|
-
* Render SEO in Helmet-compatible format
|
|
314
|
-
*/
|
|
315
|
-
export const renderAsHelmet = (options) => {
|
|
316
|
-
const rendered = renderSEO(options);
|
|
317
|
-
return {
|
|
318
|
-
base: [],
|
|
319
|
-
bodyAttributes: {},
|
|
320
|
-
htmlAttributes: options.lang ? { lang: options.lang } : {},
|
|
321
|
-
link: [],
|
|
322
|
-
meta: [],
|
|
323
|
-
noscript: [],
|
|
324
|
-
script: [],
|
|
325
|
-
style: [],
|
|
326
|
-
title: rendered.title,
|
|
327
|
-
titleAttributes: {},
|
|
328
|
-
};
|
|
91
|
+
}
|
|
92
|
+
catch (error) {
|
|
93
|
+
console.error("[SEO] Failed to apply hydrated SEO data:", error);
|
|
94
|
+
}
|
|
329
95
|
};
|
|
@@ -1,72 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* SEO Serializer
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
* JSON and base64 (de)serialization of SEO options for SSR/hydration.
|
|
5
|
+
* Uses TextEncoder/TextDecoder for UTF-8 safety in the browser, and
|
|
6
|
+
* Buffer in Node when available.
|
|
6
7
|
*/
|
|
7
8
|
import type { SEOptions } from "../types/index.js";
|
|
8
|
-
/**
|
|
9
|
-
* Serialize SEO options to a string for transmission/hydration
|
|
10
|
-
*/
|
|
11
9
|
export declare const serializeSEO: (options: SEOptions) => string;
|
|
12
|
-
/**
|
|
13
|
-
* Deserialize SEO options from a string
|
|
14
|
-
* @throws {SyntaxError} If data is not valid JSON
|
|
15
|
-
*/
|
|
16
10
|
export declare const deserializeSEO: (data: string) => SEOptions;
|
|
17
|
-
/**
|
|
18
|
-
* Serialize SEO options to base64 for safe URL transmission
|
|
19
|
-
*/
|
|
20
11
|
export declare const serializeSEOToBase64: (options: SEOptions) => string;
|
|
21
|
-
/**
|
|
22
|
-
* Deserialize SEO options from base64
|
|
23
|
-
*/
|
|
24
12
|
export declare const deserializeSEOFromBase64: (base64: string) => SEOptions;
|
|
25
|
-
interface SEOState {
|
|
26
|
-
options: SEOptions;
|
|
27
|
-
timestamp: number;
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Create SEO state with timestamp
|
|
31
|
-
*/
|
|
32
|
-
export declare const createSEOState: (options: SEOptions) => SEOState;
|
|
33
|
-
/**
|
|
34
|
-
* Serialize SEO state
|
|
35
|
-
*/
|
|
36
|
-
export declare const serializeSEOState: (state: SEOState) => string;
|
|
37
|
-
/**
|
|
38
|
-
* Deserialize SEO state
|
|
39
|
-
* @throws {SyntaxError} If data is not valid JSON
|
|
40
|
-
*/
|
|
41
|
-
export declare const deserializeSEOState: (data: string) => SEOState;
|
|
42
|
-
/**
|
|
43
|
-
* Check if SEO state is stale (older than specified milliseconds)
|
|
44
|
-
*/
|
|
45
|
-
export declare const isSEOStateStale: (state: SEOState, maxAge?: number) => boolean;
|
|
46
|
-
/**
|
|
47
|
-
* Compress SEO options using simple compression
|
|
48
|
-
* Removes undefined values and empty arrays
|
|
49
|
-
*/
|
|
50
|
-
export declare const compressSEO: (options: SEOptions) => SEOptions;
|
|
51
|
-
/**
|
|
52
|
-
* Decompress SEO options (identity function, kept for API consistency)
|
|
53
|
-
*/
|
|
54
|
-
export declare const decompressSEO: (options: SEOptions) => SEOptions;
|
|
55
|
-
/**
|
|
56
|
-
* Serialize SEO options to URL-safe query string
|
|
57
|
-
* Only includes essential SEO data for social sharing previews
|
|
58
|
-
*/
|
|
59
|
-
export declare const serializeSEOToQueryString: (options: SEOptions) => string;
|
|
60
|
-
/**
|
|
61
|
-
* Deserialize SEO options from query string
|
|
62
|
-
*/
|
|
63
|
-
export declare const deserializeSEOFromQueryString: (queryString: string) => Partial<SEOptions>;
|
|
64
|
-
/**
|
|
65
|
-
* Validate serialized SEO data before deserialization
|
|
66
|
-
*/
|
|
67
|
-
export declare const validateSerializedSEO: (data: string) => boolean;
|
|
68
13
|
/**
|
|
69
|
-
* Safe deserialize
|
|
14
|
+
* Safe deserialize: returns `null` instead of throwing on invalid input.
|
|
15
|
+
* Use this when consuming data from an untrusted source (URL params, cookies).
|
|
70
16
|
*/
|
|
71
17
|
export declare const safeDeserializeSEO: (data: string) => SEOptions | null;
|
|
72
|
-
export {};
|
|
@@ -1,191 +1,51 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* SEO Serializer
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
|
|
7
|
-
// ============================================================================
|
|
8
|
-
// Serialization
|
|
9
|
-
// ============================================================================
|
|
10
|
-
/**
|
|
11
|
-
* Serialize SEO options to a string for transmission/hydration
|
|
12
|
-
*/
|
|
13
|
-
export const serializeSEO = (options) => {
|
|
14
|
-
return JSON.stringify(options);
|
|
15
|
-
};
|
|
16
|
-
/**
|
|
17
|
-
* Deserialize SEO options from a string
|
|
18
|
-
* @throws {SyntaxError} If data is not valid JSON
|
|
4
|
+
* JSON and base64 (de)serialization of SEO options for SSR/hydration.
|
|
5
|
+
* Uses TextEncoder/TextDecoder for UTF-8 safety in the browser, and
|
|
6
|
+
* Buffer in Node when available.
|
|
19
7
|
*/
|
|
8
|
+
export const serializeSEO = (options) => JSON.stringify(options);
|
|
20
9
|
export const deserializeSEO = (data) => {
|
|
21
10
|
try {
|
|
22
11
|
return JSON.parse(data);
|
|
23
12
|
}
|
|
24
13
|
catch (error) {
|
|
25
|
-
|
|
14
|
+
const reason = error instanceof Error ? error.message : "Unknown error";
|
|
15
|
+
throw new Error(`[SEO] Failed to deserialize SEO data: ${reason}`);
|
|
26
16
|
}
|
|
27
17
|
};
|
|
28
|
-
|
|
29
|
-
* Serialize SEO options to base64 for safe URL transmission
|
|
30
|
-
*/
|
|
31
|
-
export const serializeSEOToBase64 = (options) => {
|
|
32
|
-
const json = serializeSEO(options);
|
|
18
|
+
const toBase64 = (json) => {
|
|
33
19
|
if (typeof Buffer !== "undefined") {
|
|
34
20
|
return Buffer.from(json).toString("base64");
|
|
35
21
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
const binaryString = Array.from(utf8Bytes, (byte) => String.fromCharCode(byte)).join("");
|
|
39
|
-
return btoa(binaryString);
|
|
22
|
+
const bytes = new TextEncoder().encode(json);
|
|
23
|
+
return btoa(Array.from(bytes, (b) => String.fromCharCode(b)).join(""));
|
|
40
24
|
};
|
|
41
|
-
|
|
42
|
-
* Deserialize SEO options from base64
|
|
43
|
-
*/
|
|
44
|
-
export const deserializeSEOFromBase64 = (base64) => {
|
|
45
|
-
let json;
|
|
25
|
+
const fromBase64 = (base64) => {
|
|
46
26
|
if (typeof Buffer !== "undefined") {
|
|
47
|
-
|
|
48
|
-
}
|
|
49
|
-
else {
|
|
50
|
-
// Browser fallback with UTF-8 support
|
|
51
|
-
const binaryString = atob(base64);
|
|
52
|
-
const bytes = new Uint8Array(binaryString.length);
|
|
53
|
-
for (let i = 0; i < binaryString.length; i++) {
|
|
54
|
-
bytes[i] = binaryString.charCodeAt(i);
|
|
55
|
-
}
|
|
56
|
-
json = new TextDecoder().decode(bytes);
|
|
57
|
-
}
|
|
58
|
-
return deserializeSEO(json);
|
|
59
|
-
};
|
|
60
|
-
/**
|
|
61
|
-
* Create SEO state with timestamp
|
|
62
|
-
*/
|
|
63
|
-
export const createSEOState = (options) => {
|
|
64
|
-
return {
|
|
65
|
-
options,
|
|
66
|
-
timestamp: Date.now(),
|
|
67
|
-
};
|
|
68
|
-
};
|
|
69
|
-
/**
|
|
70
|
-
* Serialize SEO state
|
|
71
|
-
*/
|
|
72
|
-
export const serializeSEOState = (state) => {
|
|
73
|
-
return JSON.stringify(state);
|
|
74
|
-
};
|
|
75
|
-
/**
|
|
76
|
-
* Deserialize SEO state
|
|
77
|
-
* @throws {SyntaxError} If data is not valid JSON
|
|
78
|
-
*/
|
|
79
|
-
export const deserializeSEOState = (data) => {
|
|
80
|
-
try {
|
|
81
|
-
return JSON.parse(data);
|
|
82
|
-
}
|
|
83
|
-
catch (error) {
|
|
84
|
-
throw new Error(`[SEO] Failed to deserialize SEO state: ${error instanceof Error ? error.message : "Unknown error"}`);
|
|
85
|
-
}
|
|
86
|
-
};
|
|
87
|
-
/**
|
|
88
|
-
* Check if SEO state is stale (older than specified milliseconds)
|
|
89
|
-
*/
|
|
90
|
-
export const isSEOStateStale = (state, maxAge = 3600000) => {
|
|
91
|
-
return Date.now() - state.timestamp > maxAge;
|
|
92
|
-
};
|
|
93
|
-
// ============================================================================
|
|
94
|
-
// Compression (for large SEO datasets)
|
|
95
|
-
// ============================================================================
|
|
96
|
-
/**
|
|
97
|
-
* Compress SEO options using simple compression
|
|
98
|
-
* Removes undefined values and empty arrays
|
|
99
|
-
*/
|
|
100
|
-
export const compressSEO = (options) => {
|
|
101
|
-
const compressed = {};
|
|
102
|
-
Object.entries(options).forEach(([key, value]) => {
|
|
103
|
-
if (value === undefined || (Array.isArray(value) && value.length === 0)) {
|
|
104
|
-
return;
|
|
105
|
-
}
|
|
106
|
-
// Compress nested objects
|
|
107
|
-
if (typeof value === "object" && !Array.isArray(value)) {
|
|
108
|
-
const compressedNested = compressSEO(value);
|
|
109
|
-
if (Object.keys(compressedNested).length > 0) {
|
|
110
|
-
compressed[key] = compressedNested;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
else {
|
|
114
|
-
compressed[key] = value;
|
|
115
|
-
}
|
|
116
|
-
});
|
|
117
|
-
return compressed;
|
|
118
|
-
};
|
|
119
|
-
/**
|
|
120
|
-
* Decompress SEO options (identity function, kept for API consistency)
|
|
121
|
-
*/
|
|
122
|
-
export const decompressSEO = (options) => {
|
|
123
|
-
return options;
|
|
124
|
-
};
|
|
125
|
-
// ============================================================================
|
|
126
|
-
// Query String Serialization
|
|
127
|
-
// ============================================================================
|
|
128
|
-
/**
|
|
129
|
-
* Serialize SEO options to URL-safe query string
|
|
130
|
-
* Only includes essential SEO data for social sharing previews
|
|
131
|
-
*/
|
|
132
|
-
export const serializeSEOToQueryString = (options) => {
|
|
133
|
-
const params = new URLSearchParams();
|
|
134
|
-
if (options.title)
|
|
135
|
-
params.set("title", options.title);
|
|
136
|
-
if (options.description)
|
|
137
|
-
params.set("description", options.description);
|
|
138
|
-
if (options.openGraph?.image) {
|
|
139
|
-
const image = Array.isArray(options.openGraph.image)
|
|
140
|
-
? options.openGraph.image[0]?.url
|
|
141
|
-
: options.openGraph.image;
|
|
142
|
-
if (image)
|
|
143
|
-
params.set("image", image);
|
|
144
|
-
}
|
|
145
|
-
return params.toString();
|
|
146
|
-
};
|
|
147
|
-
/**
|
|
148
|
-
* Deserialize SEO options from query string
|
|
149
|
-
*/
|
|
150
|
-
export const deserializeSEOFromQueryString = (queryString) => {
|
|
151
|
-
const params = new URLSearchParams(queryString);
|
|
152
|
-
const options = {};
|
|
153
|
-
if (params.has("title"))
|
|
154
|
-
options.title = params.get("title");
|
|
155
|
-
if (params.has("description"))
|
|
156
|
-
options.description = params.get("description");
|
|
157
|
-
if (params.has("image")) {
|
|
158
|
-
options.openGraph = {
|
|
159
|
-
image: params.get("image"),
|
|
160
|
-
};
|
|
161
|
-
}
|
|
162
|
-
return options;
|
|
163
|
-
};
|
|
164
|
-
// ============================================================================
|
|
165
|
-
// Validation
|
|
166
|
-
// ============================================================================
|
|
167
|
-
/**
|
|
168
|
-
* Validate serialized SEO data before deserialization
|
|
169
|
-
*/
|
|
170
|
-
export const validateSerializedSEO = (data) => {
|
|
171
|
-
try {
|
|
172
|
-
const parsed = JSON.parse(data);
|
|
173
|
-
return typeof parsed === "object" && parsed !== null;
|
|
174
|
-
}
|
|
175
|
-
catch {
|
|
176
|
-
return false;
|
|
27
|
+
return Buffer.from(base64, "base64").toString("utf-8");
|
|
177
28
|
}
|
|
29
|
+
const binary = atob(base64);
|
|
30
|
+
const bytes = new Uint8Array(binary.length);
|
|
31
|
+
for (let i = 0; i < binary.length; i++)
|
|
32
|
+
bytes[i] = binary.charCodeAt(i);
|
|
33
|
+
return new TextDecoder().decode(bytes);
|
|
178
34
|
};
|
|
35
|
+
export const serializeSEOToBase64 = (options) => toBase64(serializeSEO(options));
|
|
36
|
+
export const deserializeSEOFromBase64 = (base64) => deserializeSEO(fromBase64(base64));
|
|
179
37
|
/**
|
|
180
|
-
* Safe deserialize
|
|
38
|
+
* Safe deserialize: returns `null` instead of throwing on invalid input.
|
|
39
|
+
* Use this when consuming data from an untrusted source (URL params, cookies).
|
|
181
40
|
*/
|
|
182
41
|
export const safeDeserializeSEO = (data) => {
|
|
183
|
-
if (!validateSerializedSEO(data)) {
|
|
184
|
-
console.error("[SEO] Invalid serialized data");
|
|
185
|
-
return null;
|
|
186
|
-
}
|
|
187
42
|
try {
|
|
188
|
-
|
|
43
|
+
const parsed = JSON.parse(data);
|
|
44
|
+
if (typeof parsed !== "object" || parsed === null) {
|
|
45
|
+
console.error("[SEO] Invalid serialized data: not an object");
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
return parsed;
|
|
189
49
|
}
|
|
190
50
|
catch (error) {
|
|
191
51
|
console.error("[SEO] Error deserializing data:", error);
|
|
@@ -15,10 +15,6 @@ export interface LinkTag {
|
|
|
15
15
|
crossOrigin?: string;
|
|
16
16
|
as?: string;
|
|
17
17
|
}
|
|
18
|
-
export interface ScriptTag {
|
|
19
|
-
type: string;
|
|
20
|
-
innerHTML: string;
|
|
21
|
-
}
|
|
22
18
|
export type RobotsDirective = "index" | "noindex" | "follow" | "nofollow" | "noarchive" | "nosnippet" | "notranslate" | "noimageindex" | "unavailable_after";
|
|
23
19
|
export interface RobotsMeta {
|
|
24
20
|
index?: boolean;
|