@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.
@@ -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
- return str
15
- .replace(/&/g, "&")
16
- .replace(/</g, "&lt;")
17
- .replace(/>/g, "&gt;")
18
- .replace(/"/g, "&quot;")
19
- .replace(/'/g, "&#39;");
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 '&amp;';
27
+ case '<': return '&lt;';
28
+ case '>': return '&gt;';
29
+ case '"': return '&quot;';
30
+ case "'": return '&#39;';
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
- const metaTags = [];
26
- const linkTags = [];
27
- const jsonLdScripts = [];
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
- metaTags.push('<meta charset="utf-8" />');
34
- metaTags.push('<meta name="viewport" content="width=device-width, initial-scale=1" />');
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
- metaTags.push(`<meta name="description" content="${escapeHTML(options.description)}" />`);
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
- metaTags.push(`<meta name="robots" content="${robotsContent}" />`);
105
+ metaTagsBuilder.push(`<meta name="robots" content="${robotsContent}" />`);
61
106
  }
62
107
  // Canonical
63
108
  if (options.canonical) {
64
- linkTags.push(`<link rel="canonical" href="${escapeHTML(options.canonical)}" />`);
109
+ linkTagsBuilder.push(`<link rel="canonical" href="${escapeHTML(options.canonical)}" />`);
65
110
  }
66
111
  // Theme color
67
112
  if (options.themeColor) {
68
- metaTags.push(`<meta name="theme-color" content="${escapeHTML(options.themeColor)}" />`);
113
+ metaTagsBuilder.push(`<meta name="theme-color" content="${escapeHTML(options.themeColor)}" />`);
69
114
  }
70
115
  // Favicon
71
116
  if (options.favicon) {
72
- linkTags.push(`<link rel="icon" type="image/x-icon" href="${escapeHTML(options.favicon)}" />`);
117
+ linkTagsBuilder.push(`<link rel="icon" type="image/x-icon" href="${escapeHTML(options.favicon)}" />`);
73
118
  }
74
119
  if (options.appleTouchIcon) {
75
- linkTags.push(`<link rel="apple-touch-icon" sizes="180x180" href="${escapeHTML(options.appleTouchIcon)}" />`);
120
+ linkTagsBuilder.push(`<link rel="apple-touch-icon" sizes="180x180" href="${escapeHTML(options.appleTouchIcon)}" />`);
76
121
  }
77
122
  if (options.maskIcon) {
78
- linkTags.push(`<link rel="mask-icon" href="${escapeHTML(options.maskIcon)}" />`);
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.forEach((entry) => {
83
- linkTags.push(`<link rel="alternate" hreflang="${escapeHTML(entry.lang)}" href="${escapeHTML(entry.url)}" />`);
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.forEach((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
- linkTags.push(`<link ${attrs.join(" ")} />`);
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.forEach((origin) => {
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
- linkTags.push(`<link ${attrs.join(" ")} />`);
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.forEach((domain) => {
111
- linkTags.push(`<link rel="dns-prefetch" href="${escapeHTML(domain.href)}" />`);
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.forEach((m) => {
161
+ for (const m of options.meta) {
117
162
  if (m.name) {
118
- metaTags.push(`<meta name="${escapeHTML(m.name)}" content="${escapeHTML(m.content)}" />`);
163
+ metaTagsBuilder.push(`<meta name="${escapeHTML(m.name)}" content="${escapeHTML(m.content)}" />`);
119
164
  }
120
165
  if (m.property) {
121
- metaTags.push(`<meta property="${escapeHTML(m.property)}" content="${escapeHTML(m.content)}" />`);
166
+ metaTagsBuilder.push(`<meta property="${escapeHTML(m.property)}" content="${escapeHTML(m.content)}" />`);
122
167
  }
123
168
  if (m.httpEquiv) {
124
- metaTags.push(`<meta http-equiv="${escapeHTML(m.httpEquiv)}" content="${escapeHTML(m.content)}" />`);
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.forEach((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
- linkTags.push(`<link ${attrs.join(" ")} />`);
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
- metaTags.push(`<meta property="og:title" content="${escapeHTML(options.openGraph.title)}" />`);
193
+ metaTagsBuilder.push(`<meta property="og:title" content="${escapeHTML(options.openGraph.title)}" />`);
149
194
  if (options.openGraph.description)
150
- metaTags.push(`<meta property="og:description" content="${escapeHTML(options.openGraph.description)}" />`);
195
+ metaTagsBuilder.push(`<meta property="og:description" content="${escapeHTML(options.openGraph.description)}" />`);
151
196
  if (options.openGraph.url)
152
- metaTags.push(`<meta property="og:url" content="${escapeHTML(options.openGraph.url)}" />`);
197
+ metaTagsBuilder.push(`<meta property="og:url" content="${escapeHTML(options.openGraph.url)}" />`);
153
198
  if (options.openGraph.type)
154
- metaTags.push(`<meta property="og:type" content="${escapeHTML(options.openGraph.type)}" />`);
199
+ metaTagsBuilder.push(`<meta property="og:type" content="${escapeHTML(options.openGraph.type)}" />`);
155
200
  if (options.openGraph.siteName)
156
- metaTags.push(`<meta property="og:site_name" content="${escapeHTML(options.openGraph.siteName)}" />`);
201
+ metaTagsBuilder.push(`<meta property="og:site_name" content="${escapeHTML(options.openGraph.siteName)}" />`);
157
202
  if (options.openGraph.locale)
158
- metaTags.push(`<meta property="og:locale" content="${escapeHTML(options.openGraph.locale)}" />`);
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.forEach((img) => {
163
- metaTags.push(`<meta property="og:image" content="${escapeHTML(img.url)}" />`);
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
- metaTags.push(`<meta property="og:image:alt" content="${escapeHTML(img.alt)}" />`);
166
- });
210
+ metaTagsBuilder.push(`<meta property="og:image:alt" content="${escapeHTML(img.alt)}" />`);
211
+ }
167
212
  }
168
213
  else {
169
- metaTags.push(`<meta property="og:image" content="${escapeHTML(options.openGraph.image)}" />`);
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
- metaTags.push(`<meta name="twitter:card" content="${escapeHTML(options.twitter.cardType)}" />`);
221
+ metaTagsBuilder.push(`<meta name="twitter:card" content="${escapeHTML(options.twitter.cardType)}" />`);
177
222
  if (options.twitter.site)
178
- metaTags.push(`<meta name="twitter:site" content="${escapeHTML(options.twitter.site)}" />`);
223
+ metaTagsBuilder.push(`<meta name="twitter:site" content="${escapeHTML(options.twitter.site)}" />`);
179
224
  if (options.twitter.handle)
180
- metaTags.push(`<meta name="twitter:creator" content="${escapeHTML(options.twitter.handle)}" />`);
225
+ metaTagsBuilder.push(`<meta name="twitter:creator" content="${escapeHTML(options.twitter.handle)}" />`);
181
226
  if (options.twitter.title)
182
- metaTags.push(`<meta name="twitter:title" content="${escapeHTML(options.twitter.title)}" />`);
227
+ metaTagsBuilder.push(`<meta name="twitter:title" content="${escapeHTML(options.twitter.title)}" />`);
183
228
  if (options.twitter.description)
184
- metaTags.push(`<meta name="twitter:description" content="${escapeHTML(options.twitter.description)}" />`);
229
+ metaTagsBuilder.push(`<meta name="twitter:description" content="${escapeHTML(options.twitter.description)}" />`);
185
230
  if (options.twitter.image)
186
- metaTags.push(`<meta name="twitter:image" content="${escapeHTML(options.twitter.image)}" />`);
231
+ metaTagsBuilder.push(`<meta name="twitter:image" content="${escapeHTML(options.twitter.image)}" />`);
187
232
  if (options.twitter.imageAlt)
188
- metaTags.push(`<meta name="twitter:image:alt" content="${escapeHTML(options.twitter.imageAlt)}" />`);
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.forEach((data) => {
193
- jsonLdScripts.push(`<script type="application/ld+json">${JSON.stringify(data)}</script>`);
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
- return {
243
+ // Build result using cached arrays
244
+ const result = {
197
245
  title,
198
- meta: metaTags.join("\n "),
199
- link: linkTags.join("\n "),
200
- jsonLd: jsonLdScripts.join("\n "),
246
+ meta: metaTagsBuilder.join("\n "),
247
+ link: linkTagsBuilder.join("\n "),
248
+ jsonLd: jsonLdScriptsBuilder.join("\n "),
201
249
  toHTML() {
202
- return ` <title>${this.title}</title>\n${this.meta ? ` ${this.meta}\n` : ""}${this.link ? ` ${this.link}\n` : ""}${this.jsonLd ? ` ${this.jsonLd}\n` : ""}`;
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").then(({ updateSEO }) => {
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
  /**
@@ -148,8 +148,6 @@ export interface SEOptions {
148
148
  maskIcon?: string;
149
149
  themeColor?: string;
150
150
  viewport?: string;
151
- additionalMetaTags?: MetaTag[];
152
- additionalLinkTags?: LinkTag[];
153
151
  }
154
152
  export interface OpenGraphImage {
155
153
  url: string;
@@ -5,7 +5,7 @@
5
5
  */
6
6
  import type { SEOptions, MetaTag, LinkTag } from "../types/index.js";
7
7
  /**
8
- * Update SEO elements in the DOM
8
+ * Update SEO elements in the DOM with optimized batching
9
9
  */
10
10
  export declare const updateSEO: (options: SEOptions) => void;
11
11
  /**