@umituz/web-seo 2.0.12 → 2.0.15
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 +1 -1
- package/dist/domains/seo/components/JsonLd.js +59 -14
- package/dist/domains/seo/components/SEO.d.ts +1 -2
- package/dist/domains/seo/components/SEO.js +40 -44
- package/dist/domains/seo/core/init.d.ts +2 -1
- package/dist/domains/seo/core/init.js +8 -2
- package/dist/domains/seo/hooks/useJsonLd.js +44 -12
- package/dist/domains/seo/hooks/useSEO.js +36 -43
- package/dist/domains/seo/ssr/renderer.d.ts +2 -1
- package/dist/domains/seo/ssr/renderer.js +132 -67
- package/dist/domains/seo/types/index.d.ts +0 -2
- package/dist/domains/seo/utils/dom.d.ts +1 -1
- package/dist/domains/seo/utils/dom.js +158 -151
- package/dist/domains/seo/utils/meta-tags/basic.js +5 -1
- package/dist/domains/seo/utils/meta-tags/social.d.ts +1 -1
- package/dist/domains/seo/utils/meta-tags/social.js +1 -1
- package/dist/domains/seo/utils/schema-generators/schema.js +1 -3
- package/dist/domains/seo/utils/validation/validators.d.ts +6 -2
- package/dist/domains/seo/utils/validation/validators.js +52 -5
- package/dist/domains/seo/vite/plugin.d.ts +2 -0
- package/dist/domains/seo/vite/plugin.js +13 -8
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -2
- package/package.json +3 -3
|
@@ -5,36 +5,81 @@
|
|
|
5
5
|
* Generates HTML strings for meta tags and structured data.
|
|
6
6
|
*/
|
|
7
7
|
// ============================================================================
|
|
8
|
-
// HTML Escaping (Security)
|
|
8
|
+
// HTML Escaping (Security) - Optimized
|
|
9
9
|
// ============================================================================
|
|
10
|
+
// Escape cache for better performance
|
|
11
|
+
const escapeCache = new Map();
|
|
10
12
|
/**
|
|
11
13
|
* Escape HTML special characters to prevent XSS attacks
|
|
14
|
+
* Optimized with caching and single-pass replacement
|
|
12
15
|
*/
|
|
13
16
|
const escapeHTML = (str) => {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
.
|
|
19
|
-
|
|
17
|
+
if (!str)
|
|
18
|
+
return "";
|
|
19
|
+
// Check cache first
|
|
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;
|
|
20
57
|
};
|
|
58
|
+
// Render cache for better SSR performance
|
|
59
|
+
const renderCache = new WeakMap();
|
|
21
60
|
/**
|
|
22
61
|
* Render SEO options to HTML strings for server-side rendering
|
|
62
|
+
* Optimized with caching and efficient string building
|
|
23
63
|
*/
|
|
24
|
-
export const renderSEO = (options) => {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
64
|
+
export const renderSEO = (options, useCache = true) => {
|
|
65
|
+
// Check cache first
|
|
66
|
+
if (useCache && renderCache.has(options)) {
|
|
67
|
+
return renderCache.get(options);
|
|
68
|
+
}
|
|
69
|
+
// Use string builders for better performance
|
|
70
|
+
const metaTagsBuilder = [];
|
|
71
|
+
const linkTagsBuilder = [];
|
|
72
|
+
const jsonLdScriptsBuilder = [];
|
|
28
73
|
// Title
|
|
29
74
|
const title = options.titleTemplate
|
|
30
75
|
? escapeHTML(options.titleTemplate.replace("%s", options.title || ""))
|
|
31
76
|
: escapeHTML(options.title || "");
|
|
32
77
|
// Charset
|
|
33
|
-
|
|
34
|
-
|
|
78
|
+
metaTagsBuilder.push('<meta charset="utf-8" />');
|
|
79
|
+
metaTagsBuilder.push('<meta name="viewport" content="width=device-width, initial-scale=1" />');
|
|
35
80
|
// Description
|
|
36
81
|
if (options.description) {
|
|
37
|
-
|
|
82
|
+
metaTagsBuilder.push(`<meta name="description" content="${escapeHTML(options.description)}" />`);
|
|
38
83
|
}
|
|
39
84
|
// Robots
|
|
40
85
|
if (options.robots !== undefined) {
|
|
@@ -57,35 +102,35 @@ export const renderSEO = (options) => {
|
|
|
57
102
|
directives.push(options.robots.snippet ? "all" : "nosnippet");
|
|
58
103
|
robotsContent = directives.join(", ");
|
|
59
104
|
}
|
|
60
|
-
|
|
105
|
+
metaTagsBuilder.push(`<meta name="robots" content="${robotsContent}" />`);
|
|
61
106
|
}
|
|
62
107
|
// Canonical
|
|
63
108
|
if (options.canonical) {
|
|
64
|
-
|
|
109
|
+
linkTagsBuilder.push(`<link rel="canonical" href="${escapeHTML(options.canonical)}" />`);
|
|
65
110
|
}
|
|
66
111
|
// Theme color
|
|
67
112
|
if (options.themeColor) {
|
|
68
|
-
|
|
113
|
+
metaTagsBuilder.push(`<meta name="theme-color" content="${escapeHTML(options.themeColor)}" />`);
|
|
69
114
|
}
|
|
70
115
|
// Favicon
|
|
71
116
|
if (options.favicon) {
|
|
72
|
-
|
|
117
|
+
linkTagsBuilder.push(`<link rel="icon" type="image/x-icon" href="${escapeHTML(options.favicon)}" />`);
|
|
73
118
|
}
|
|
74
119
|
if (options.appleTouchIcon) {
|
|
75
|
-
|
|
120
|
+
linkTagsBuilder.push(`<link rel="apple-touch-icon" sizes="180x180" href="${escapeHTML(options.appleTouchIcon)}" />`);
|
|
76
121
|
}
|
|
77
122
|
if (options.maskIcon) {
|
|
78
|
-
|
|
123
|
+
linkTagsBuilder.push(`<link rel="mask-icon" href="${escapeHTML(options.maskIcon)}" />`);
|
|
79
124
|
}
|
|
80
125
|
// Hreflang
|
|
81
126
|
if (options.hreflang && options.hreflang.length > 0) {
|
|
82
|
-
options.hreflang
|
|
83
|
-
|
|
84
|
-
}
|
|
127
|
+
for (const entry of options.hreflang) {
|
|
128
|
+
linkTagsBuilder.push(`<link rel="alternate" hreflang="${escapeHTML(entry.lang)}" href="${escapeHTML(entry.url)}" />`);
|
|
129
|
+
}
|
|
85
130
|
}
|
|
86
131
|
// Preload
|
|
87
132
|
if (options.preload && options.preload.length > 0) {
|
|
88
|
-
options.preload
|
|
133
|
+
for (const preload of options.preload) {
|
|
89
134
|
const attrs = [`rel="preload"`, `href="${escapeHTML(preload.href)}"`, `as="${escapeHTML(preload.as)}"`];
|
|
90
135
|
if (preload.type)
|
|
91
136
|
attrs.push(`type="${escapeHTML(preload.type)}"`);
|
|
@@ -93,41 +138,41 @@ export const renderSEO = (options) => {
|
|
|
93
138
|
attrs.push(`crossorigin="${escapeHTML(preload.crossOrigin)}"`);
|
|
94
139
|
if (preload.integrity)
|
|
95
140
|
attrs.push(`integrity="${escapeHTML(preload.integrity)}"`);
|
|
96
|
-
|
|
97
|
-
}
|
|
141
|
+
linkTagsBuilder.push(`<link ${attrs.join(" ")} />`);
|
|
142
|
+
}
|
|
98
143
|
}
|
|
99
144
|
// Preconnect
|
|
100
145
|
if (options.preconnect && options.preconnect.length > 0) {
|
|
101
|
-
options.preconnect
|
|
146
|
+
for (const origin of options.preconnect) {
|
|
102
147
|
const attrs = [`rel="preconnect"`, `href="${escapeHTML(origin.href)}"`];
|
|
103
148
|
if (origin.crossOrigin)
|
|
104
149
|
attrs.push(`crossorigin="${escapeHTML(origin.crossOrigin)}"`);
|
|
105
|
-
|
|
106
|
-
}
|
|
150
|
+
linkTagsBuilder.push(`<link ${attrs.join(" ")} />`);
|
|
151
|
+
}
|
|
107
152
|
}
|
|
108
153
|
// DNS Prefetch
|
|
109
154
|
if (options.dnsPrefetch && options.dnsPrefetch.length > 0) {
|
|
110
|
-
options.dnsPrefetch
|
|
111
|
-
|
|
112
|
-
}
|
|
155
|
+
for (const domain of options.dnsPrefetch) {
|
|
156
|
+
linkTagsBuilder.push(`<link rel="dns-prefetch" href="${escapeHTML(domain.href)}" />`);
|
|
157
|
+
}
|
|
113
158
|
}
|
|
114
159
|
// Custom meta tags
|
|
115
160
|
if (options.meta && options.meta.length > 0) {
|
|
116
|
-
options.meta
|
|
161
|
+
for (const m of options.meta) {
|
|
117
162
|
if (m.name) {
|
|
118
|
-
|
|
163
|
+
metaTagsBuilder.push(`<meta name="${escapeHTML(m.name)}" content="${escapeHTML(m.content)}" />`);
|
|
119
164
|
}
|
|
120
165
|
if (m.property) {
|
|
121
|
-
|
|
166
|
+
metaTagsBuilder.push(`<meta property="${escapeHTML(m.property)}" content="${escapeHTML(m.content)}" />`);
|
|
122
167
|
}
|
|
123
168
|
if (m.httpEquiv) {
|
|
124
|
-
|
|
169
|
+
metaTagsBuilder.push(`<meta http-equiv="${escapeHTML(m.httpEquiv)}" content="${escapeHTML(m.content)}" />`);
|
|
125
170
|
}
|
|
126
|
-
}
|
|
171
|
+
}
|
|
127
172
|
}
|
|
128
173
|
// Custom link tags
|
|
129
174
|
if (options.link && options.link.length > 0) {
|
|
130
|
-
options.link
|
|
175
|
+
for (const link of options.link) {
|
|
131
176
|
const attrs = [`rel="${escapeHTML(link.rel)}"`, `href="${escapeHTML(link.href)}"`];
|
|
132
177
|
if (link.type)
|
|
133
178
|
attrs.push(`type="${escapeHTML(link.type)}"`);
|
|
@@ -139,72 +184,88 @@ export const renderSEO = (options) => {
|
|
|
139
184
|
attrs.push(`crossorigin="${escapeHTML(link.crossOrigin)}"`);
|
|
140
185
|
if (link.as)
|
|
141
186
|
attrs.push(`as="${escapeHTML(link.as)}"`);
|
|
142
|
-
|
|
143
|
-
}
|
|
187
|
+
linkTagsBuilder.push(`<link ${attrs.join(" ")} />`);
|
|
188
|
+
}
|
|
144
189
|
}
|
|
145
190
|
// Open Graph
|
|
146
191
|
if (options.openGraph) {
|
|
147
192
|
if (options.openGraph.title)
|
|
148
|
-
|
|
193
|
+
metaTagsBuilder.push(`<meta property="og:title" content="${escapeHTML(options.openGraph.title)}" />`);
|
|
149
194
|
if (options.openGraph.description)
|
|
150
|
-
|
|
195
|
+
metaTagsBuilder.push(`<meta property="og:description" content="${escapeHTML(options.openGraph.description)}" />`);
|
|
151
196
|
if (options.openGraph.url)
|
|
152
|
-
|
|
197
|
+
metaTagsBuilder.push(`<meta property="og:url" content="${escapeHTML(options.openGraph.url)}" />`);
|
|
153
198
|
if (options.openGraph.type)
|
|
154
|
-
|
|
199
|
+
metaTagsBuilder.push(`<meta property="og:type" content="${escapeHTML(options.openGraph.type)}" />`);
|
|
155
200
|
if (options.openGraph.siteName)
|
|
156
|
-
|
|
201
|
+
metaTagsBuilder.push(`<meta property="og:site_name" content="${escapeHTML(options.openGraph.siteName)}" />`);
|
|
157
202
|
if (options.openGraph.locale)
|
|
158
|
-
|
|
203
|
+
metaTagsBuilder.push(`<meta property="og:locale" content="${escapeHTML(options.openGraph.locale)}" />`);
|
|
159
204
|
// Images
|
|
160
205
|
if (options.openGraph.image) {
|
|
161
206
|
if (Array.isArray(options.openGraph.image)) {
|
|
162
|
-
options.openGraph.image
|
|
163
|
-
|
|
207
|
+
for (const img of options.openGraph.image) {
|
|
208
|
+
metaTagsBuilder.push(`<meta property="og:image" content="${escapeHTML(img.url)}" />`);
|
|
164
209
|
if (img.alt)
|
|
165
|
-
|
|
166
|
-
}
|
|
210
|
+
metaTagsBuilder.push(`<meta property="og:image:alt" content="${escapeHTML(img.alt)}" />`);
|
|
211
|
+
}
|
|
167
212
|
}
|
|
168
213
|
else {
|
|
169
|
-
|
|
214
|
+
metaTagsBuilder.push(`<meta property="og:image" content="${escapeHTML(options.openGraph.image)}" />`);
|
|
170
215
|
}
|
|
171
216
|
}
|
|
172
217
|
}
|
|
173
218
|
// Twitter Card
|
|
174
219
|
if (options.twitter) {
|
|
175
220
|
if (options.twitter.cardType)
|
|
176
|
-
|
|
221
|
+
metaTagsBuilder.push(`<meta name="twitter:card" content="${escapeHTML(options.twitter.cardType)}" />`);
|
|
177
222
|
if (options.twitter.site)
|
|
178
|
-
|
|
223
|
+
metaTagsBuilder.push(`<meta name="twitter:site" content="${escapeHTML(options.twitter.site)}" />`);
|
|
179
224
|
if (options.twitter.handle)
|
|
180
|
-
|
|
225
|
+
metaTagsBuilder.push(`<meta name="twitter:creator" content="${escapeHTML(options.twitter.handle)}" />`);
|
|
181
226
|
if (options.twitter.title)
|
|
182
|
-
|
|
227
|
+
metaTagsBuilder.push(`<meta name="twitter:title" content="${escapeHTML(options.twitter.title)}" />`);
|
|
183
228
|
if (options.twitter.description)
|
|
184
|
-
|
|
229
|
+
metaTagsBuilder.push(`<meta name="twitter:description" content="${escapeHTML(options.twitter.description)}" />`);
|
|
185
230
|
if (options.twitter.image)
|
|
186
|
-
|
|
231
|
+
metaTagsBuilder.push(`<meta name="twitter:image" content="${escapeHTML(options.twitter.image)}" />`);
|
|
187
232
|
if (options.twitter.imageAlt)
|
|
188
|
-
|
|
233
|
+
metaTagsBuilder.push(`<meta name="twitter:image:alt" content="${escapeHTML(options.twitter.imageAlt)}" />`);
|
|
189
234
|
}
|
|
190
235
|
// JSON-LD
|
|
191
236
|
if (options.jsonLd && options.jsonLd.length > 0) {
|
|
192
|
-
options.jsonLd
|
|
193
|
-
|
|
194
|
-
|
|
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
|
+
}
|
|
195
242
|
}
|
|
196
|
-
|
|
243
|
+
// Build result using cached arrays
|
|
244
|
+
const result = {
|
|
197
245
|
title,
|
|
198
|
-
meta:
|
|
199
|
-
link:
|
|
200
|
-
jsonLd:
|
|
246
|
+
meta: metaTagsBuilder.join("\n "),
|
|
247
|
+
link: linkTagsBuilder.join("\n "),
|
|
248
|
+
jsonLd: jsonLdScriptsBuilder.join("\n "),
|
|
201
249
|
toHTML() {
|
|
202
|
-
|
|
250
|
+
// Use template literals for better performance
|
|
251
|
+
const parts = [` <title>${this.title}</title>`];
|
|
252
|
+
if (this.meta)
|
|
253
|
+
parts.push(` ${this.meta}`);
|
|
254
|
+
if (this.link)
|
|
255
|
+
parts.push(` ${this.link}`);
|
|
256
|
+
if (this.jsonLd)
|
|
257
|
+
parts.push(` ${this.jsonLd}`);
|
|
258
|
+
return parts.join("\n");
|
|
203
259
|
},
|
|
204
260
|
toString() {
|
|
205
261
|
return this.toHTML();
|
|
206
262
|
},
|
|
207
263
|
};
|
|
264
|
+
// Cache the result
|
|
265
|
+
if (useCache) {
|
|
266
|
+
renderCache.set(options, result);
|
|
267
|
+
}
|
|
268
|
+
return result;
|
|
208
269
|
};
|
|
209
270
|
// ============================================================================
|
|
210
271
|
// SSR SEO Utilities
|
|
@@ -240,8 +301,12 @@ export const hydrateSEO = (serializedData) => {
|
|
|
240
301
|
return;
|
|
241
302
|
}
|
|
242
303
|
// Import updateSEO dynamically to avoid circular dependencies
|
|
243
|
-
import("../utils/dom.js")
|
|
304
|
+
import("../utils/dom.js")
|
|
305
|
+
.then(({ updateSEO }) => {
|
|
244
306
|
updateSEO(options);
|
|
307
|
+
})
|
|
308
|
+
.catch((error) => {
|
|
309
|
+
console.error("[SEO] Failed to load updateSEO:", error);
|
|
245
310
|
});
|
|
246
311
|
};
|
|
247
312
|
/**
|