@umituz/web-seo 2.0.7 → 2.0.10

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.
@@ -39,7 +39,7 @@ export const JsonLd = ({ data, strategy = "afterInteractive", id }) => {
39
39
  });
40
40
  // Cleanup function
41
41
  return () => {
42
- dataArray.forEach((jsonData, _index) => {
42
+ dataArray.forEach((jsonData) => {
43
43
  const script = document.querySelector(`script[type="application/ld+json"][data-seo-id="${id || JSON.stringify(jsonData)}"]`);
44
44
  if (script) {
45
45
  script.remove();
@@ -3,7 +3,7 @@ export const initSEO = (config) => {
3
3
  if (typeof window === "undefined")
4
4
  return;
5
5
  const update = () => {
6
- const lang = config.i18n?.language?.split("-")[0] || "en";
6
+ const lang = config.i18n?.language ? config.i18n.language.split("-")[0] || "en" : "en";
7
7
  const title = config.i18n ? config.i18n.t(config.titleKey || "app.title") : document.title;
8
8
  const description = config.i18n ? config.i18n.t(config.descriptionKey || "app.description") : "";
9
9
  const canonical = config.canonicalPrefix ? `${config.canonicalPrefix}/${lang}` : undefined;
@@ -33,6 +33,7 @@ export declare const generateHead: (options: SEOptions) => string;
33
33
  export declare const extractSEOData: (options: SEOptions) => string;
34
34
  /**
35
35
  * Hydrate SEO on client side
36
+ * @throws {SyntaxError} If serializedData is not valid JSON
36
37
  */
37
38
  export declare const hydrateSEO: (serializedData: string) => void;
38
39
  export interface RenderedMetaTag {
@@ -4,6 +4,20 @@
4
4
  * Server-side rendering utilities for SEO.
5
5
  * Generates HTML strings for meta tags and structured data.
6
6
  */
7
+ // ============================================================================
8
+ // HTML Escaping (Security)
9
+ // ============================================================================
10
+ /**
11
+ * Escape HTML special characters to prevent XSS attacks
12
+ */
13
+ 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;");
20
+ };
7
21
  /**
8
22
  * Render SEO options to HTML strings for server-side rendering
9
23
  */
@@ -13,14 +27,14 @@ export const renderSEO = (options) => {
13
27
  const jsonLdScripts = [];
14
28
  // Title
15
29
  const title = options.titleTemplate
16
- ? options.titleTemplate.replace("%s", options.title || "")
17
- : options.title || "";
30
+ ? escapeHTML(options.titleTemplate.replace("%s", options.title || ""))
31
+ : escapeHTML(options.title || "");
18
32
  // Charset
19
33
  metaTags.push('<meta charset="utf-8" />');
20
34
  metaTags.push('<meta name="viewport" content="width=device-width, initial-scale=1" />');
21
35
  // Description
22
36
  if (options.description) {
23
- metaTags.push(`<meta name="description" content="${options.description}" />`);
37
+ metaTags.push(`<meta name="description" content="${escapeHTML(options.description)}" />`);
24
38
  }
25
39
  // Robots
26
40
  if (options.robots !== undefined) {
@@ -47,131 +61,131 @@ export const renderSEO = (options) => {
47
61
  }
48
62
  // Canonical
49
63
  if (options.canonical) {
50
- linkTags.push(`<link rel="canonical" href="${options.canonical}" />`);
64
+ linkTags.push(`<link rel="canonical" href="${escapeHTML(options.canonical)}" />`);
51
65
  }
52
66
  // Theme color
53
67
  if (options.themeColor) {
54
- metaTags.push(`<meta name="theme-color" content="${options.themeColor}" />`);
68
+ metaTags.push(`<meta name="theme-color" content="${escapeHTML(options.themeColor)}" />`);
55
69
  }
56
70
  // Favicon
57
71
  if (options.favicon) {
58
- linkTags.push(`<link rel="icon" type="image/x-icon" href="${options.favicon}" />`);
72
+ linkTags.push(`<link rel="icon" type="image/x-icon" href="${escapeHTML(options.favicon)}" />`);
59
73
  }
60
74
  if (options.appleTouchIcon) {
61
- linkTags.push(`<link rel="apple-touch-icon" sizes="180x180" href="${options.appleTouchIcon}" />`);
75
+ linkTags.push(`<link rel="apple-touch-icon" sizes="180x180" href="${escapeHTML(options.appleTouchIcon)}" />`);
62
76
  }
63
77
  if (options.maskIcon) {
64
- linkTags.push(`<link rel="mask-icon" href="${options.maskIcon}" />`);
78
+ linkTags.push(`<link rel="mask-icon" href="${escapeHTML(options.maskIcon)}" />`);
65
79
  }
66
80
  // Hreflang
67
81
  if (options.hreflang && options.hreflang.length > 0) {
68
82
  options.hreflang.forEach((entry) => {
69
- linkTags.push(`<link rel="alternate" hreflang="${entry.lang}" href="${entry.url}" />`);
83
+ linkTags.push(`<link rel="alternate" hreflang="${escapeHTML(entry.lang)}" href="${escapeHTML(entry.url)}" />`);
70
84
  });
71
85
  }
72
86
  // Preload
73
87
  if (options.preload && options.preload.length > 0) {
74
88
  options.preload.forEach((preload) => {
75
- const attrs = [`rel="preload"`, `href="${preload.href}"`, `as="${preload.as}"`];
89
+ const attrs = [`rel="preload"`, `href="${escapeHTML(preload.href)}"`, `as="${escapeHTML(preload.as)}"`];
76
90
  if (preload.type)
77
- attrs.push(`type="${preload.type}"`);
91
+ attrs.push(`type="${escapeHTML(preload.type)}"`);
78
92
  if (preload.crossOrigin)
79
- attrs.push(`crossorigin="${preload.crossOrigin}"`);
93
+ attrs.push(`crossorigin="${escapeHTML(preload.crossOrigin)}"`);
80
94
  if (preload.integrity)
81
- attrs.push(`integrity="${preload.integrity}"`);
95
+ attrs.push(`integrity="${escapeHTML(preload.integrity)}"`);
82
96
  linkTags.push(`<link ${attrs.join(" ")} />`);
83
97
  });
84
98
  }
85
99
  // Preconnect
86
100
  if (options.preconnect && options.preconnect.length > 0) {
87
101
  options.preconnect.forEach((origin) => {
88
- const attrs = [`rel="preconnect"`, `href="${origin.href}"`];
102
+ const attrs = [`rel="preconnect"`, `href="${escapeHTML(origin.href)}"`];
89
103
  if (origin.crossOrigin)
90
- attrs.push(`crossorigin="${origin.crossOrigin}"`);
104
+ attrs.push(`crossorigin="${escapeHTML(origin.crossOrigin)}"`);
91
105
  linkTags.push(`<link ${attrs.join(" ")} />`);
92
106
  });
93
107
  }
94
108
  // DNS Prefetch
95
109
  if (options.dnsPrefetch && options.dnsPrefetch.length > 0) {
96
110
  options.dnsPrefetch.forEach((domain) => {
97
- linkTags.push(`<link rel="dns-prefetch" href="${domain.href}" />`);
111
+ linkTags.push(`<link rel="dns-prefetch" href="${escapeHTML(domain.href)}" />`);
98
112
  });
99
113
  }
100
114
  // Custom meta tags
101
115
  if (options.meta && options.meta.length > 0) {
102
116
  options.meta.forEach((m) => {
103
117
  if (m.name) {
104
- metaTags.push(`<meta name="${m.name}" content="${m.content}" />`);
118
+ metaTags.push(`<meta name="${escapeHTML(m.name)}" content="${escapeHTML(m.content)}" />`);
105
119
  }
106
120
  if (m.property) {
107
- metaTags.push(`<meta property="${m.property}" content="${m.content}" />`);
121
+ metaTags.push(`<meta property="${escapeHTML(m.property)}" content="${escapeHTML(m.content)}" />`);
108
122
  }
109
123
  if (m.httpEquiv) {
110
- metaTags.push(`<meta http-equiv="${m.httpEquiv}" content="${m.content}" />`);
124
+ metaTags.push(`<meta http-equiv="${escapeHTML(m.httpEquiv)}" content="${escapeHTML(m.content)}" />`);
111
125
  }
112
126
  });
113
127
  }
114
128
  // Custom link tags
115
129
  if (options.link && options.link.length > 0) {
116
130
  options.link.forEach((link) => {
117
- const attrs = [`rel="${link.rel}"`, `href="${link.href}"`];
131
+ const attrs = [`rel="${escapeHTML(link.rel)}"`, `href="${escapeHTML(link.href)}"`];
118
132
  if (link.type)
119
- attrs.push(`type="${link.type}"`);
133
+ attrs.push(`type="${escapeHTML(link.type)}"`);
120
134
  if (link.sizes)
121
- attrs.push(`sizes="${link.sizes}"`);
135
+ attrs.push(`sizes="${escapeHTML(link.sizes)}"`);
122
136
  if (link.media)
123
- attrs.push(`media="${link.media}"`);
137
+ attrs.push(`media="${escapeHTML(link.media)}"`);
124
138
  if (link.crossOrigin)
125
- attrs.push(`crossorigin="${link.crossOrigin}"`);
139
+ attrs.push(`crossorigin="${escapeHTML(link.crossOrigin)}"`);
126
140
  if (link.as)
127
- attrs.push(`as="${link.as}"`);
141
+ attrs.push(`as="${escapeHTML(link.as)}"`);
128
142
  linkTags.push(`<link ${attrs.join(" ")} />`);
129
143
  });
130
144
  }
131
145
  // Open Graph
132
146
  if (options.openGraph) {
133
147
  if (options.openGraph.title)
134
- metaTags.push(`<meta property="og:title" content="${options.openGraph.title}" />`);
148
+ metaTags.push(`<meta property="og:title" content="${escapeHTML(options.openGraph.title)}" />`);
135
149
  if (options.openGraph.description)
136
- metaTags.push(`<meta property="og:description" content="${options.openGraph.description}" />`);
150
+ metaTags.push(`<meta property="og:description" content="${escapeHTML(options.openGraph.description)}" />`);
137
151
  if (options.openGraph.url)
138
- metaTags.push(`<meta property="og:url" content="${options.openGraph.url}" />`);
152
+ metaTags.push(`<meta property="og:url" content="${escapeHTML(options.openGraph.url)}" />`);
139
153
  if (options.openGraph.type)
140
- metaTags.push(`<meta property="og:type" content="${options.openGraph.type}" />`);
154
+ metaTags.push(`<meta property="og:type" content="${escapeHTML(options.openGraph.type)}" />`);
141
155
  if (options.openGraph.siteName)
142
- metaTags.push(`<meta property="og:site_name" content="${options.openGraph.siteName}" />`);
156
+ metaTags.push(`<meta property="og:site_name" content="${escapeHTML(options.openGraph.siteName)}" />`);
143
157
  if (options.openGraph.locale)
144
- metaTags.push(`<meta property="og:locale" content="${options.openGraph.locale}" />`);
158
+ metaTags.push(`<meta property="og:locale" content="${escapeHTML(options.openGraph.locale)}" />`);
145
159
  // Images
146
160
  if (options.openGraph.image) {
147
161
  if (Array.isArray(options.openGraph.image)) {
148
162
  options.openGraph.image.forEach((img) => {
149
- metaTags.push(`<meta property="og:image" content="${img.url}" />`);
163
+ metaTags.push(`<meta property="og:image" content="${escapeHTML(img.url)}" />`);
150
164
  if (img.alt)
151
- metaTags.push(`<meta property="og:image:alt" content="${img.alt}" />`);
165
+ metaTags.push(`<meta property="og:image:alt" content="${escapeHTML(img.alt)}" />`);
152
166
  });
153
167
  }
154
168
  else {
155
- metaTags.push(`<meta property="og:image" content="${options.openGraph.image}" />`);
169
+ metaTags.push(`<meta property="og:image" content="${escapeHTML(options.openGraph.image)}" />`);
156
170
  }
157
171
  }
158
172
  }
159
173
  // Twitter Card
160
174
  if (options.twitter) {
161
175
  if (options.twitter.cardType)
162
- metaTags.push(`<meta name="twitter:card" content="${options.twitter.cardType}" />`);
176
+ metaTags.push(`<meta name="twitter:card" content="${escapeHTML(options.twitter.cardType)}" />`);
163
177
  if (options.twitter.site)
164
- metaTags.push(`<meta name="twitter:site" content="${options.twitter.site}" />`);
178
+ metaTags.push(`<meta name="twitter:site" content="${escapeHTML(options.twitter.site)}" />`);
165
179
  if (options.twitter.handle)
166
- metaTags.push(`<meta name="twitter:creator" content="${options.twitter.handle}" />`);
180
+ metaTags.push(`<meta name="twitter:creator" content="${escapeHTML(options.twitter.handle)}" />`);
167
181
  if (options.twitter.title)
168
- metaTags.push(`<meta name="twitter:title" content="${options.twitter.title}" />`);
182
+ metaTags.push(`<meta name="twitter:title" content="${escapeHTML(options.twitter.title)}" />`);
169
183
  if (options.twitter.description)
170
- metaTags.push(`<meta name="twitter:description" content="${options.twitter.description}" />`);
184
+ metaTags.push(`<meta name="twitter:description" content="${escapeHTML(options.twitter.description)}" />`);
171
185
  if (options.twitter.image)
172
- metaTags.push(`<meta name="twitter:image" content="${options.twitter.image}" />`);
186
+ metaTags.push(`<meta name="twitter:image" content="${escapeHTML(options.twitter.image)}" />`);
173
187
  if (options.twitter.imageAlt)
174
- metaTags.push(`<meta name="twitter:image:alt" content="${options.twitter.imageAlt}" />`);
188
+ metaTags.push(`<meta name="twitter:image:alt" content="${escapeHTML(options.twitter.imageAlt)}" />`);
175
189
  }
176
190
  // JSON-LD
177
191
  if (options.jsonLd && options.jsonLd.length > 0) {
@@ -200,7 +214,7 @@ export const renderSEO = (options) => {
200
214
  */
201
215
  export const generateHead = (options) => {
202
216
  const rendered = renderSEO(options);
203
- return ` <head${options.lang ? ` lang="${options.lang}"` : ""}>
217
+ return ` <head${options.lang ? ` lang="${escapeHTML(options.lang)}"` : ""}>
204
218
  ${rendered.toHTML().split("\n").map(line => ` ${line}`).join("\n")}
205
219
  </head>`;
206
220
  };
@@ -212,11 +226,19 @@ export const extractSEOData = (options) => {
212
226
  };
213
227
  /**
214
228
  * Hydrate SEO on client side
229
+ * @throws {SyntaxError} If serializedData is not valid JSON
215
230
  */
216
231
  export const hydrateSEO = (serializedData) => {
217
232
  if (typeof window === "undefined")
218
233
  return;
219
- const options = JSON.parse(serializedData);
234
+ let options;
235
+ try {
236
+ options = JSON.parse(serializedData);
237
+ }
238
+ catch (error) {
239
+ console.error("[SEO] Failed to hydrate SEO data:", error);
240
+ return;
241
+ }
220
242
  // Import updateSEO dynamically to avoid circular dependencies
221
243
  import("../utils/dom.js").then(({ updateSEO }) => {
222
244
  updateSEO(options);
@@ -11,6 +11,7 @@ import type { SEOptions } from "../types/index.js";
11
11
  export declare const serializeSEO: (options: SEOptions) => string;
12
12
  /**
13
13
  * Deserialize SEO options from a string
14
+ * @throws {SyntaxError} If data is not valid JSON
14
15
  */
15
16
  export declare const deserializeSEO: (data: string) => SEOptions;
16
17
  /**
@@ -35,6 +36,7 @@ export declare const createSEOState: (options: SEOptions) => SEOState;
35
36
  export declare const serializeSEOState: (state: SEOState) => string;
36
37
  /**
37
38
  * Deserialize SEO state
39
+ * @throws {SyntaxError} If data is not valid JSON
38
40
  */
39
41
  export declare const deserializeSEOState: (data: string) => SEOState;
40
42
  /**
@@ -15,9 +15,15 @@ export const serializeSEO = (options) => {
15
15
  };
16
16
  /**
17
17
  * Deserialize SEO options from a string
18
+ * @throws {SyntaxError} If data is not valid JSON
18
19
  */
19
20
  export const deserializeSEO = (data) => {
20
- return JSON.parse(data);
21
+ try {
22
+ return JSON.parse(data);
23
+ }
24
+ catch (error) {
25
+ throw new Error(`[SEO] Failed to deserialize SEO data: ${error instanceof Error ? error.message : "Unknown error"}`);
26
+ }
21
27
  };
22
28
  /**
23
29
  * Serialize SEO options to base64 for safe URL transmission
@@ -27,8 +33,10 @@ export const serializeSEOToBase64 = (options) => {
27
33
  if (typeof Buffer !== "undefined") {
28
34
  return Buffer.from(json).toString("base64");
29
35
  }
30
- // Browser fallback
31
- return btoa(json);
36
+ // Browser fallback with UTF-8 support
37
+ const utf8Bytes = new TextEncoder().encode(json);
38
+ const binaryString = Array.from(utf8Bytes, (byte) => String.fromCharCode(byte)).join("");
39
+ return btoa(binaryString);
32
40
  };
33
41
  /**
34
42
  * Deserialize SEO options from base64
@@ -39,8 +47,13 @@ export const deserializeSEOFromBase64 = (base64) => {
39
47
  json = Buffer.from(base64, "base64").toString("utf-8");
40
48
  }
41
49
  else {
42
- // Browser fallback
43
- json = atob(base64);
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);
44
57
  }
45
58
  return deserializeSEO(json);
46
59
  };
@@ -61,9 +74,15 @@ export const serializeSEOState = (state) => {
61
74
  };
62
75
  /**
63
76
  * Deserialize SEO state
77
+ * @throws {SyntaxError} If data is not valid JSON
64
78
  */
65
79
  export const deserializeSEOState = (data) => {
66
- return JSON.parse(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
+ }
67
86
  };
68
87
  /**
69
88
  * Check if SEO state is stale (older than specified milliseconds)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umituz/web-seo",
3
- "version": "2.0.7",
3
+ "version": "2.0.10",
4
4
  "description": "Comprehensive SEO solution for React Vite applications with structured data, performance hints, and build-time optimization",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",