@umituz/web-seo 2.0.14 → 2.0.16
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 +43 -9
- package/dist/domains/seo/components/SEO.d.ts +1 -1
- package/dist/domains/seo/components/SEO.js +40 -44
- package/dist/domains/seo/hooks/useJsonLd.js +20 -6
- 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 +118 -67
- package/dist/domains/seo/utils/dom.d.ts +1 -1
- package/dist/domains/seo/utils/dom.js +158 -125
- package/dist/domains/seo/utils/validation/validators.d.ts +6 -2
- package/dist/domains/seo/utils/validation/validators.js +52 -5
- package/package.json +1 -1
|
@@ -12,5 +12,5 @@ interface JsonLdProps {
|
|
|
12
12
|
/** Unique identifier for this JSON-LD block (for debugging) */
|
|
13
13
|
id?: string;
|
|
14
14
|
}
|
|
15
|
-
export declare const JsonLd: ({ data, strategy, id }: JsonLdProps) => null
|
|
15
|
+
export declare const JsonLd: import("react").MemoExoticComponent<({ data, strategy, id }: JsonLdProps) => null>;
|
|
16
16
|
export default JsonLd;
|
|
@@ -4,19 +4,47 @@
|
|
|
4
4
|
* React component for injecting JSON-LD structured data into the page.
|
|
5
5
|
* Supports all Schema.org types with automatic validation.
|
|
6
6
|
*/
|
|
7
|
-
import { useEffect, useRef, useMemo } from "react";
|
|
8
|
-
//
|
|
7
|
+
import { useEffect, useRef, useMemo, memo } from "react";
|
|
8
|
+
// Optimized hash generation with caching using cyrb53 algorithm
|
|
9
|
+
const hashCache = new WeakMap();
|
|
9
10
|
const generateDataId = (data) => {
|
|
11
|
+
// Check cache first for better performance
|
|
12
|
+
if (hashCache.has(data)) {
|
|
13
|
+
return hashCache.get(data);
|
|
14
|
+
}
|
|
15
|
+
// Use optimized hashing using cyrb53 algorithm (better distribution & performance)
|
|
10
16
|
const str = JSON.stringify(data);
|
|
11
|
-
let
|
|
12
|
-
for (let i = 0; i < str.length; i++) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
17
|
+
let h1 = 0xdeadbeef, h2 = 0x41c6ce57;
|
|
18
|
+
for (let i = 0, ch; i < str.length; i++) {
|
|
19
|
+
ch = str.charCodeAt(i);
|
|
20
|
+
h1 = Math.imul(h1 ^ ch, 2654435761);
|
|
21
|
+
h2 = Math.imul(h2 ^ ch, 1597334677);
|
|
22
|
+
}
|
|
23
|
+
h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^ Math.imul(h2 ^ (h2 >>> 13), 3266489909);
|
|
24
|
+
h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^ Math.imul(h1 ^ (h1 >>> 13), 3266489909);
|
|
25
|
+
const hash = (4294967296 * (2097151 & h2) + (h1 >>> 0)).toString(36);
|
|
26
|
+
const result = `jsonld-${hash}`;
|
|
27
|
+
// Cache the result for future use
|
|
28
|
+
hashCache.set(data, result);
|
|
29
|
+
return result;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Deep comparison for JSON-LD data
|
|
33
|
+
*/
|
|
34
|
+
const deepEqual = (a, b) => {
|
|
35
|
+
// Handle arrays
|
|
36
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
37
|
+
if (a.length !== b.length)
|
|
38
|
+
return false;
|
|
39
|
+
return a.every((item, index) => deepEqual(item, b[index]));
|
|
40
|
+
}
|
|
41
|
+
// Handle single objects
|
|
42
|
+
if (!Array.isArray(a) && !Array.isArray(b)) {
|
|
43
|
+
return JSON.stringify(a) === JSON.stringify(b);
|
|
16
44
|
}
|
|
17
|
-
return
|
|
45
|
+
return false;
|
|
18
46
|
};
|
|
19
|
-
|
|
47
|
+
const JsonLdComponent = ({ data, strategy = "afterInteractive", id }) => {
|
|
20
48
|
const scriptRef = useRef(null);
|
|
21
49
|
// Generate stable IDs for data items
|
|
22
50
|
const dataIds = useMemo(() => {
|
|
@@ -60,4 +88,10 @@ export const JsonLd = ({ data, strategy = "afterInteractive", id }) => {
|
|
|
60
88
|
}, [data, strategy, dataIds]);
|
|
61
89
|
return null;
|
|
62
90
|
};
|
|
91
|
+
// Memoize component with custom comparison to prevent unnecessary re-renders
|
|
92
|
+
export const JsonLd = memo(JsonLdComponent, (prevProps, nextProps) => {
|
|
93
|
+
return (prevProps.strategy === nextProps.strategy &&
|
|
94
|
+
prevProps.id === nextProps.id &&
|
|
95
|
+
deepEqual(prevProps.data, nextProps.data));
|
|
96
|
+
});
|
|
63
97
|
export default JsonLd;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { SEOptions } from "../types/index.js";
|
|
2
|
-
export declare const SEO: (props: SEOptions) => null
|
|
2
|
+
export declare const SEO: import("react").MemoExoticComponent<(props: SEOptions) => null>;
|
|
@@ -1,48 +1,44 @@
|
|
|
1
|
-
import { useEffect,
|
|
1
|
+
import { useEffect, useRef, memo } from "react";
|
|
2
2
|
import { updateSEO } from "../utils/dom.js";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
props.link,
|
|
33
|
-
props.openGraph,
|
|
34
|
-
props.twitter,
|
|
35
|
-
props.jsonLd,
|
|
36
|
-
props.hreflang,
|
|
37
|
-
props.preload,
|
|
38
|
-
props.preconnect,
|
|
39
|
-
props.dnsPrefetch,
|
|
40
|
-
props.robots,
|
|
41
|
-
props.breadcrumbs,
|
|
42
|
-
props.viewport,
|
|
43
|
-
]);
|
|
3
|
+
/**
|
|
4
|
+
* Deep comparison function for objects and arrays
|
|
5
|
+
* Performs efficient structural equality check
|
|
6
|
+
*/
|
|
7
|
+
const deepEqual = (a, b) => {
|
|
8
|
+
// Primitive values and same reference
|
|
9
|
+
if (a === b)
|
|
10
|
+
return true;
|
|
11
|
+
// Handle null/undefined
|
|
12
|
+
if (a == null || b == null)
|
|
13
|
+
return a === b;
|
|
14
|
+
// Handle arrays
|
|
15
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
16
|
+
if (a.length !== b.length)
|
|
17
|
+
return false;
|
|
18
|
+
return a.every((item, index) => deepEqual(item, b[index]));
|
|
19
|
+
}
|
|
20
|
+
// Handle objects
|
|
21
|
+
if (typeof a === 'object' && typeof b === 'object' && a !== null && b !== null) {
|
|
22
|
+
const keysA = Object.keys(a);
|
|
23
|
+
const keysB = Object.keys(b);
|
|
24
|
+
if (keysA.length !== keysB.length)
|
|
25
|
+
return false;
|
|
26
|
+
return keysA.every(key => keysB.includes(key) && deepEqual(a[key], b[key]));
|
|
27
|
+
}
|
|
28
|
+
return false;
|
|
29
|
+
};
|
|
30
|
+
const SEOComponent = (props) => {
|
|
31
|
+
const prevPropsRef = useRef(null);
|
|
44
32
|
useEffect(() => {
|
|
45
|
-
|
|
46
|
-
|
|
33
|
+
// Only update if props have actually changed using deep comparison
|
|
34
|
+
if (!prevPropsRef.current || !deepEqual(prevPropsRef.current, props)) {
|
|
35
|
+
prevPropsRef.current = props;
|
|
36
|
+
updateSEO(props);
|
|
37
|
+
}
|
|
38
|
+
}, [props]);
|
|
47
39
|
return null; // Component does not render anything
|
|
48
40
|
};
|
|
41
|
+
// Memoize component with custom comparison to prevent unnecessary re-renders
|
|
42
|
+
export const SEO = memo(SEOComponent, (prevProps, nextProps) => {
|
|
43
|
+
return deepEqual(prevProps, nextProps);
|
|
44
|
+
});
|
|
@@ -6,15 +6,29 @@
|
|
|
6
6
|
*/
|
|
7
7
|
import { useEffect, useCallback, useMemo } from "react";
|
|
8
8
|
// Generate a stable hash from JSON-LD data for consistent identification
|
|
9
|
+
// Optimized with better hashing algorithm and memoization
|
|
10
|
+
const hashCache = new WeakMap();
|
|
9
11
|
const generateDataId = (data) => {
|
|
12
|
+
// Check cache first for better performance
|
|
13
|
+
if (hashCache.has(data)) {
|
|
14
|
+
return hashCache.get(data);
|
|
15
|
+
}
|
|
16
|
+
// Use base64 encoding for faster, more reliable hashing
|
|
10
17
|
const str = JSON.stringify(data);
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
18
|
+
// Optimized hashing using cyrb53 algorithm (better distribution & performance)
|
|
19
|
+
let h1 = 0xdeadbeef, h2 = 0x41c6ce57;
|
|
20
|
+
for (let i = 0, ch; i < str.length; i++) {
|
|
21
|
+
ch = str.charCodeAt(i);
|
|
22
|
+
h1 = Math.imul(h1 ^ ch, 2654435761);
|
|
23
|
+
h2 = Math.imul(h2 ^ ch, 1597334677);
|
|
16
24
|
}
|
|
17
|
-
|
|
25
|
+
h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^ Math.imul(h2 ^ (h2 >>> 13), 3266489909);
|
|
26
|
+
h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^ Math.imul(h1 ^ (h1 >>> 13), 3266489909);
|
|
27
|
+
const hash = (4294967296 * (2097151 & h2) + (h1 >>> 0)).toString(36);
|
|
28
|
+
const result = `jsonld-${hash}`;
|
|
29
|
+
// Cache the result for future use
|
|
30
|
+
hashCache.set(data, result);
|
|
31
|
+
return result;
|
|
18
32
|
};
|
|
19
33
|
export const useJsonLd = (data, options) => {
|
|
20
34
|
const { strategy = "afterInteractive", id } = options || {};
|
|
@@ -1,47 +1,40 @@
|
|
|
1
|
-
import { useEffect,
|
|
1
|
+
import { useEffect, useRef } from "react";
|
|
2
2
|
import { updateSEO } from "../utils/dom.js";
|
|
3
|
+
/**
|
|
4
|
+
* Deep comparison function for objects and arrays
|
|
5
|
+
* Performs efficient structural equality check
|
|
6
|
+
*/
|
|
7
|
+
const deepEqual = (a, b) => {
|
|
8
|
+
// Primitive values and same reference
|
|
9
|
+
if (a === b)
|
|
10
|
+
return true;
|
|
11
|
+
// Handle null/undefined
|
|
12
|
+
if (a == null || b == null)
|
|
13
|
+
return a === b;
|
|
14
|
+
// Handle arrays
|
|
15
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
16
|
+
if (a.length !== b.length)
|
|
17
|
+
return false;
|
|
18
|
+
return a.every((item, index) => deepEqual(item, b[index]));
|
|
19
|
+
}
|
|
20
|
+
// Handle objects
|
|
21
|
+
if (typeof a === 'object' && typeof b === 'object' && a !== null && b !== null) {
|
|
22
|
+
const keysA = Object.keys(a);
|
|
23
|
+
const keysB = Object.keys(b);
|
|
24
|
+
if (keysA.length !== keysB.length)
|
|
25
|
+
return false;
|
|
26
|
+
return keysA.every(key => keysB.includes(key) && deepEqual(a[key], b[key]));
|
|
27
|
+
}
|
|
28
|
+
return false;
|
|
29
|
+
};
|
|
3
30
|
export const useSEO = (options) => {
|
|
4
|
-
//
|
|
5
|
-
const
|
|
6
|
-
options.title,
|
|
7
|
-
options.description,
|
|
8
|
-
options.lang,
|
|
9
|
-
options.canonical,
|
|
10
|
-
options.themeColor,
|
|
11
|
-
options.favicon,
|
|
12
|
-
JSON.stringify(options.meta),
|
|
13
|
-
JSON.stringify(options.link),
|
|
14
|
-
JSON.stringify(options.openGraph),
|
|
15
|
-
JSON.stringify(options.twitter),
|
|
16
|
-
JSON.stringify(options.jsonLd),
|
|
17
|
-
JSON.stringify(options.hreflang),
|
|
18
|
-
JSON.stringify(options.preload),
|
|
19
|
-
JSON.stringify(options.preconnect),
|
|
20
|
-
JSON.stringify(options.dnsPrefetch),
|
|
21
|
-
JSON.stringify(options.robots),
|
|
22
|
-
JSON.stringify(options.breadcrumbs),
|
|
23
|
-
options.viewport,
|
|
24
|
-
], [
|
|
25
|
-
options.title,
|
|
26
|
-
options.description,
|
|
27
|
-
options.lang,
|
|
28
|
-
options.canonical,
|
|
29
|
-
options.themeColor,
|
|
30
|
-
options.favicon,
|
|
31
|
-
options.meta,
|
|
32
|
-
options.link,
|
|
33
|
-
options.openGraph,
|
|
34
|
-
options.twitter,
|
|
35
|
-
options.jsonLd,
|
|
36
|
-
options.hreflang,
|
|
37
|
-
options.preload,
|
|
38
|
-
options.preconnect,
|
|
39
|
-
options.dnsPrefetch,
|
|
40
|
-
options.robots,
|
|
41
|
-
options.breadcrumbs,
|
|
42
|
-
options.viewport,
|
|
43
|
-
]);
|
|
31
|
+
// Use ref to store the previous options for deep comparison
|
|
32
|
+
const prevOptionsRef = useRef(null);
|
|
44
33
|
useEffect(() => {
|
|
45
|
-
|
|
46
|
-
|
|
34
|
+
// Only update if options have actually changed
|
|
35
|
+
if (!prevOptionsRef.current || !deepEqual(prevOptionsRef.current, options)) {
|
|
36
|
+
prevOptionsRef.current = options;
|
|
37
|
+
updateSEO(options);
|
|
38
|
+
}
|
|
39
|
+
}, [options]);
|
|
47
40
|
};
|
|
@@ -21,8 +21,9 @@ export interface RenderedSEO {
|
|
|
21
21
|
}
|
|
22
22
|
/**
|
|
23
23
|
* Render SEO options to HTML strings for server-side rendering
|
|
24
|
+
* Optimized with caching and efficient string building
|
|
24
25
|
*/
|
|
25
|
-
export declare const renderSEO: (options: SEOptions) => RenderedSEO;
|
|
26
|
+
export declare const renderSEO: (options: SEOptions, useCache?: boolean) => RenderedSEO;
|
|
26
27
|
/**
|
|
27
28
|
* Generate complete HTML head with SEO tags
|
|
28
29
|
*/
|
|
@@ -5,44 +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;
|
|
20
39
|
};
|
|
21
40
|
/**
|
|
22
41
|
* Escape JSON content for safe inclusion in HTML script tags
|
|
23
42
|
* Prevents script breakage from </script> sequences in JSON data
|
|
43
|
+
* Optimized with caching
|
|
24
44
|
*/
|
|
25
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
|
+
}
|
|
26
51
|
// Replace </script> with <\/script> to prevent breaking out of script tag
|
|
27
|
-
|
|
52
|
+
const result = json.replace(/<\/script>/gi, "<\\/script>");
|
|
53
|
+
if (escapeCache.size < 1000) {
|
|
54
|
+
escapeCache.set(cacheKey, result);
|
|
55
|
+
}
|
|
56
|
+
return result;
|
|
28
57
|
};
|
|
58
|
+
// Render cache for better SSR performance
|
|
59
|
+
const renderCache = new WeakMap();
|
|
29
60
|
/**
|
|
30
61
|
* Render SEO options to HTML strings for server-side rendering
|
|
62
|
+
* Optimized with caching and efficient string building
|
|
31
63
|
*/
|
|
32
|
-
export const renderSEO = (options) => {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
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 = [];
|
|
36
73
|
// Title
|
|
37
74
|
const title = options.titleTemplate
|
|
38
75
|
? escapeHTML(options.titleTemplate.replace("%s", options.title || ""))
|
|
39
76
|
: escapeHTML(options.title || "");
|
|
40
77
|
// Charset
|
|
41
|
-
|
|
42
|
-
|
|
78
|
+
metaTagsBuilder.push('<meta charset="utf-8" />');
|
|
79
|
+
metaTagsBuilder.push('<meta name="viewport" content="width=device-width, initial-scale=1" />');
|
|
43
80
|
// Description
|
|
44
81
|
if (options.description) {
|
|
45
|
-
|
|
82
|
+
metaTagsBuilder.push(`<meta name="description" content="${escapeHTML(options.description)}" />`);
|
|
46
83
|
}
|
|
47
84
|
// Robots
|
|
48
85
|
if (options.robots !== undefined) {
|
|
@@ -65,35 +102,35 @@ export const renderSEO = (options) => {
|
|
|
65
102
|
directives.push(options.robots.snippet ? "all" : "nosnippet");
|
|
66
103
|
robotsContent = directives.join(", ");
|
|
67
104
|
}
|
|
68
|
-
|
|
105
|
+
metaTagsBuilder.push(`<meta name="robots" content="${robotsContent}" />`);
|
|
69
106
|
}
|
|
70
107
|
// Canonical
|
|
71
108
|
if (options.canonical) {
|
|
72
|
-
|
|
109
|
+
linkTagsBuilder.push(`<link rel="canonical" href="${escapeHTML(options.canonical)}" />`);
|
|
73
110
|
}
|
|
74
111
|
// Theme color
|
|
75
112
|
if (options.themeColor) {
|
|
76
|
-
|
|
113
|
+
metaTagsBuilder.push(`<meta name="theme-color" content="${escapeHTML(options.themeColor)}" />`);
|
|
77
114
|
}
|
|
78
115
|
// Favicon
|
|
79
116
|
if (options.favicon) {
|
|
80
|
-
|
|
117
|
+
linkTagsBuilder.push(`<link rel="icon" type="image/x-icon" href="${escapeHTML(options.favicon)}" />`);
|
|
81
118
|
}
|
|
82
119
|
if (options.appleTouchIcon) {
|
|
83
|
-
|
|
120
|
+
linkTagsBuilder.push(`<link rel="apple-touch-icon" sizes="180x180" href="${escapeHTML(options.appleTouchIcon)}" />`);
|
|
84
121
|
}
|
|
85
122
|
if (options.maskIcon) {
|
|
86
|
-
|
|
123
|
+
linkTagsBuilder.push(`<link rel="mask-icon" href="${escapeHTML(options.maskIcon)}" />`);
|
|
87
124
|
}
|
|
88
125
|
// Hreflang
|
|
89
126
|
if (options.hreflang && options.hreflang.length > 0) {
|
|
90
|
-
options.hreflang
|
|
91
|
-
|
|
92
|
-
}
|
|
127
|
+
for (const entry of options.hreflang) {
|
|
128
|
+
linkTagsBuilder.push(`<link rel="alternate" hreflang="${escapeHTML(entry.lang)}" href="${escapeHTML(entry.url)}" />`);
|
|
129
|
+
}
|
|
93
130
|
}
|
|
94
131
|
// Preload
|
|
95
132
|
if (options.preload && options.preload.length > 0) {
|
|
96
|
-
options.preload
|
|
133
|
+
for (const preload of options.preload) {
|
|
97
134
|
const attrs = [`rel="preload"`, `href="${escapeHTML(preload.href)}"`, `as="${escapeHTML(preload.as)}"`];
|
|
98
135
|
if (preload.type)
|
|
99
136
|
attrs.push(`type="${escapeHTML(preload.type)}"`);
|
|
@@ -101,41 +138,41 @@ export const renderSEO = (options) => {
|
|
|
101
138
|
attrs.push(`crossorigin="${escapeHTML(preload.crossOrigin)}"`);
|
|
102
139
|
if (preload.integrity)
|
|
103
140
|
attrs.push(`integrity="${escapeHTML(preload.integrity)}"`);
|
|
104
|
-
|
|
105
|
-
}
|
|
141
|
+
linkTagsBuilder.push(`<link ${attrs.join(" ")} />`);
|
|
142
|
+
}
|
|
106
143
|
}
|
|
107
144
|
// Preconnect
|
|
108
145
|
if (options.preconnect && options.preconnect.length > 0) {
|
|
109
|
-
options.preconnect
|
|
146
|
+
for (const origin of options.preconnect) {
|
|
110
147
|
const attrs = [`rel="preconnect"`, `href="${escapeHTML(origin.href)}"`];
|
|
111
148
|
if (origin.crossOrigin)
|
|
112
149
|
attrs.push(`crossorigin="${escapeHTML(origin.crossOrigin)}"`);
|
|
113
|
-
|
|
114
|
-
}
|
|
150
|
+
linkTagsBuilder.push(`<link ${attrs.join(" ")} />`);
|
|
151
|
+
}
|
|
115
152
|
}
|
|
116
153
|
// DNS Prefetch
|
|
117
154
|
if (options.dnsPrefetch && options.dnsPrefetch.length > 0) {
|
|
118
|
-
options.dnsPrefetch
|
|
119
|
-
|
|
120
|
-
}
|
|
155
|
+
for (const domain of options.dnsPrefetch) {
|
|
156
|
+
linkTagsBuilder.push(`<link rel="dns-prefetch" href="${escapeHTML(domain.href)}" />`);
|
|
157
|
+
}
|
|
121
158
|
}
|
|
122
159
|
// Custom meta tags
|
|
123
160
|
if (options.meta && options.meta.length > 0) {
|
|
124
|
-
options.meta
|
|
161
|
+
for (const m of options.meta) {
|
|
125
162
|
if (m.name) {
|
|
126
|
-
|
|
163
|
+
metaTagsBuilder.push(`<meta name="${escapeHTML(m.name)}" content="${escapeHTML(m.content)}" />`);
|
|
127
164
|
}
|
|
128
165
|
if (m.property) {
|
|
129
|
-
|
|
166
|
+
metaTagsBuilder.push(`<meta property="${escapeHTML(m.property)}" content="${escapeHTML(m.content)}" />`);
|
|
130
167
|
}
|
|
131
168
|
if (m.httpEquiv) {
|
|
132
|
-
|
|
169
|
+
metaTagsBuilder.push(`<meta http-equiv="${escapeHTML(m.httpEquiv)}" content="${escapeHTML(m.content)}" />`);
|
|
133
170
|
}
|
|
134
|
-
}
|
|
171
|
+
}
|
|
135
172
|
}
|
|
136
173
|
// Custom link tags
|
|
137
174
|
if (options.link && options.link.length > 0) {
|
|
138
|
-
options.link
|
|
175
|
+
for (const link of options.link) {
|
|
139
176
|
const attrs = [`rel="${escapeHTML(link.rel)}"`, `href="${escapeHTML(link.href)}"`];
|
|
140
177
|
if (link.type)
|
|
141
178
|
attrs.push(`type="${escapeHTML(link.type)}"`);
|
|
@@ -147,74 +184,88 @@ export const renderSEO = (options) => {
|
|
|
147
184
|
attrs.push(`crossorigin="${escapeHTML(link.crossOrigin)}"`);
|
|
148
185
|
if (link.as)
|
|
149
186
|
attrs.push(`as="${escapeHTML(link.as)}"`);
|
|
150
|
-
|
|
151
|
-
}
|
|
187
|
+
linkTagsBuilder.push(`<link ${attrs.join(" ")} />`);
|
|
188
|
+
}
|
|
152
189
|
}
|
|
153
190
|
// Open Graph
|
|
154
191
|
if (options.openGraph) {
|
|
155
192
|
if (options.openGraph.title)
|
|
156
|
-
|
|
193
|
+
metaTagsBuilder.push(`<meta property="og:title" content="${escapeHTML(options.openGraph.title)}" />`);
|
|
157
194
|
if (options.openGraph.description)
|
|
158
|
-
|
|
195
|
+
metaTagsBuilder.push(`<meta property="og:description" content="${escapeHTML(options.openGraph.description)}" />`);
|
|
159
196
|
if (options.openGraph.url)
|
|
160
|
-
|
|
197
|
+
metaTagsBuilder.push(`<meta property="og:url" content="${escapeHTML(options.openGraph.url)}" />`);
|
|
161
198
|
if (options.openGraph.type)
|
|
162
|
-
|
|
199
|
+
metaTagsBuilder.push(`<meta property="og:type" content="${escapeHTML(options.openGraph.type)}" />`);
|
|
163
200
|
if (options.openGraph.siteName)
|
|
164
|
-
|
|
201
|
+
metaTagsBuilder.push(`<meta property="og:site_name" content="${escapeHTML(options.openGraph.siteName)}" />`);
|
|
165
202
|
if (options.openGraph.locale)
|
|
166
|
-
|
|
203
|
+
metaTagsBuilder.push(`<meta property="og:locale" content="${escapeHTML(options.openGraph.locale)}" />`);
|
|
167
204
|
// Images
|
|
168
205
|
if (options.openGraph.image) {
|
|
169
206
|
if (Array.isArray(options.openGraph.image)) {
|
|
170
|
-
options.openGraph.image
|
|
171
|
-
|
|
207
|
+
for (const img of options.openGraph.image) {
|
|
208
|
+
metaTagsBuilder.push(`<meta property="og:image" content="${escapeHTML(img.url)}" />`);
|
|
172
209
|
if (img.alt)
|
|
173
|
-
|
|
174
|
-
}
|
|
210
|
+
metaTagsBuilder.push(`<meta property="og:image:alt" content="${escapeHTML(img.alt)}" />`);
|
|
211
|
+
}
|
|
175
212
|
}
|
|
176
213
|
else {
|
|
177
|
-
|
|
214
|
+
metaTagsBuilder.push(`<meta property="og:image" content="${escapeHTML(options.openGraph.image)}" />`);
|
|
178
215
|
}
|
|
179
216
|
}
|
|
180
217
|
}
|
|
181
218
|
// Twitter Card
|
|
182
219
|
if (options.twitter) {
|
|
183
220
|
if (options.twitter.cardType)
|
|
184
|
-
|
|
221
|
+
metaTagsBuilder.push(`<meta name="twitter:card" content="${escapeHTML(options.twitter.cardType)}" />`);
|
|
185
222
|
if (options.twitter.site)
|
|
186
|
-
|
|
223
|
+
metaTagsBuilder.push(`<meta name="twitter:site" content="${escapeHTML(options.twitter.site)}" />`);
|
|
187
224
|
if (options.twitter.handle)
|
|
188
|
-
|
|
225
|
+
metaTagsBuilder.push(`<meta name="twitter:creator" content="${escapeHTML(options.twitter.handle)}" />`);
|
|
189
226
|
if (options.twitter.title)
|
|
190
|
-
|
|
227
|
+
metaTagsBuilder.push(`<meta name="twitter:title" content="${escapeHTML(options.twitter.title)}" />`);
|
|
191
228
|
if (options.twitter.description)
|
|
192
|
-
|
|
229
|
+
metaTagsBuilder.push(`<meta name="twitter:description" content="${escapeHTML(options.twitter.description)}" />`);
|
|
193
230
|
if (options.twitter.image)
|
|
194
|
-
|
|
231
|
+
metaTagsBuilder.push(`<meta name="twitter:image" content="${escapeHTML(options.twitter.image)}" />`);
|
|
195
232
|
if (options.twitter.imageAlt)
|
|
196
|
-
|
|
233
|
+
metaTagsBuilder.push(`<meta name="twitter:image:alt" content="${escapeHTML(options.twitter.imageAlt)}" />`);
|
|
197
234
|
}
|
|
198
235
|
// JSON-LD
|
|
199
236
|
if (options.jsonLd && options.jsonLd.length > 0) {
|
|
200
|
-
options.jsonLd
|
|
237
|
+
for (const data of options.jsonLd) {
|
|
201
238
|
const json = JSON.stringify(data);
|
|
202
239
|
const safeJson = escapeJSONForScript(json);
|
|
203
|
-
|
|
204
|
-
}
|
|
240
|
+
jsonLdScriptsBuilder.push(`<script type="application/ld+json">${safeJson}</script>`);
|
|
241
|
+
}
|
|
205
242
|
}
|
|
206
|
-
|
|
243
|
+
// Build result using cached arrays
|
|
244
|
+
const result = {
|
|
207
245
|
title,
|
|
208
|
-
meta:
|
|
209
|
-
link:
|
|
210
|
-
jsonLd:
|
|
246
|
+
meta: metaTagsBuilder.join("\n "),
|
|
247
|
+
link: linkTagsBuilder.join("\n "),
|
|
248
|
+
jsonLd: jsonLdScriptsBuilder.join("\n "),
|
|
211
249
|
toHTML() {
|
|
212
|
-
|
|
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");
|
|
213
259
|
},
|
|
214
260
|
toString() {
|
|
215
261
|
return this.toHTML();
|
|
216
262
|
},
|
|
217
263
|
};
|
|
264
|
+
// Cache the result
|
|
265
|
+
if (useCache) {
|
|
266
|
+
renderCache.set(options, result);
|
|
267
|
+
}
|
|
268
|
+
return result;
|
|
218
269
|
};
|
|
219
270
|
// ============================================================================
|
|
220
271
|
// SSR SEO Utilities
|
|
@@ -7,137 +7,170 @@ import { setMetaTag, setLinkTag, setRobotsMeta } from "./meta-tags/basic.js";
|
|
|
7
7
|
import { setHreflangTags, setFavicon, setAppleTouchIcon, setMaskIcon, setThemeColor, setViewport, } from "./meta-tags/advanced.js";
|
|
8
8
|
import { setOpenGraph, setTwitterCard } from "./meta-tags/social.js";
|
|
9
9
|
import { setPreloadHints, setPreconnectHints, setDnsPrefetchHints, } from "./meta-tags/performance.js";
|
|
10
|
+
// DOM update batching system
|
|
11
|
+
const updateQueue = [];
|
|
12
|
+
let updateScheduled = false;
|
|
13
|
+
const scheduleDOMUpdates = () => {
|
|
14
|
+
if (updateScheduled)
|
|
15
|
+
return;
|
|
16
|
+
updateScheduled = true;
|
|
17
|
+
// Use requestAnimationFrame to batch DOM updates and prevent layout thrashing
|
|
18
|
+
requestAnimationFrame(() => {
|
|
19
|
+
// Execute all queued updates in a single frame
|
|
20
|
+
const updates = updateQueue.splice(0, updateQueue.length);
|
|
21
|
+
updates.forEach(update => {
|
|
22
|
+
try {
|
|
23
|
+
update();
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
console.error('DOM update error:', error);
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
updateScheduled = false;
|
|
30
|
+
// Process any updates that were queued during execution
|
|
31
|
+
if (updateQueue.length > 0) {
|
|
32
|
+
scheduleDOMUpdates();
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
};
|
|
36
|
+
const queueDOMUpdate = (update) => {
|
|
37
|
+
updateQueue.push(update);
|
|
38
|
+
scheduleDOMUpdates();
|
|
39
|
+
};
|
|
10
40
|
/**
|
|
11
|
-
* Update SEO elements in the DOM
|
|
41
|
+
* Update SEO elements in the DOM with optimized batching
|
|
12
42
|
*/
|
|
13
43
|
export const updateSEO = (options) => {
|
|
14
44
|
if (typeof document === "undefined")
|
|
15
45
|
return;
|
|
16
|
-
//
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
//
|
|
38
|
-
if (
|
|
39
|
-
//
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
46
|
+
// Queue all DOM updates for better performance
|
|
47
|
+
queueDOMUpdate(() => {
|
|
48
|
+
// Title with template support
|
|
49
|
+
if (options.title) {
|
|
50
|
+
const title = options.titleTemplate
|
|
51
|
+
? options.titleTemplate.replace("%s", options.title)
|
|
52
|
+
: options.title;
|
|
53
|
+
document.title = title;
|
|
54
|
+
}
|
|
55
|
+
// Language
|
|
56
|
+
if (options.lang) {
|
|
57
|
+
document.documentElement.lang = options.lang;
|
|
58
|
+
}
|
|
59
|
+
// Description
|
|
60
|
+
if (options.description) {
|
|
61
|
+
setMetaTag("name", "description", options.description);
|
|
62
|
+
}
|
|
63
|
+
// Canonical
|
|
64
|
+
if (options.canonical) {
|
|
65
|
+
setLinkTag({ rel: "canonical", href: options.canonical });
|
|
66
|
+
}
|
|
67
|
+
// Robots
|
|
68
|
+
if (options.robots !== undefined) {
|
|
69
|
+
// Handle both RobotsMeta and RobotsDirective[] types
|
|
70
|
+
if (Array.isArray(options.robots)) {
|
|
71
|
+
// Convert directive array to RobotsMeta object
|
|
72
|
+
const robotsMeta = {};
|
|
73
|
+
options.robots.forEach((directive) => {
|
|
74
|
+
switch (directive) {
|
|
75
|
+
case "index":
|
|
76
|
+
robotsMeta.index = true;
|
|
77
|
+
break;
|
|
78
|
+
case "noindex":
|
|
79
|
+
robotsMeta.index = false;
|
|
80
|
+
break;
|
|
81
|
+
case "follow":
|
|
82
|
+
robotsMeta.follow = true;
|
|
83
|
+
break;
|
|
84
|
+
case "nofollow":
|
|
85
|
+
robotsMeta.follow = false;
|
|
86
|
+
break;
|
|
87
|
+
case "noarchive":
|
|
88
|
+
robotsMeta.archive = false;
|
|
89
|
+
break;
|
|
90
|
+
case "nosnippet":
|
|
91
|
+
robotsMeta.snippet = false;
|
|
92
|
+
break;
|
|
93
|
+
case "notranslate":
|
|
94
|
+
robotsMeta.translate = false;
|
|
95
|
+
break;
|
|
96
|
+
case "noimageindex":
|
|
97
|
+
robotsMeta.imageIndex = false;
|
|
98
|
+
break;
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
setRobotsMeta(robotsMeta);
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
setRobotsMeta(options.robots);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
// Viewport
|
|
108
|
+
if (options.viewport) {
|
|
109
|
+
setViewport(options.viewport);
|
|
110
|
+
}
|
|
111
|
+
// Theme Color
|
|
112
|
+
if (options.themeColor) {
|
|
113
|
+
setThemeColor(options.themeColor);
|
|
114
|
+
}
|
|
115
|
+
// Favicon
|
|
116
|
+
if (options.favicon) {
|
|
117
|
+
setFavicon(options.favicon);
|
|
118
|
+
}
|
|
119
|
+
if (options.appleTouchIcon) {
|
|
120
|
+
setAppleTouchIcon(options.appleTouchIcon);
|
|
121
|
+
}
|
|
122
|
+
if (options.maskIcon) {
|
|
123
|
+
setMaskIcon(options.maskIcon);
|
|
124
|
+
}
|
|
125
|
+
// Hreflang
|
|
126
|
+
if (options.hreflang && options.hreflang.length > 0) {
|
|
127
|
+
setHreflangTags(options.hreflang);
|
|
128
|
+
}
|
|
129
|
+
// Performance Hints
|
|
130
|
+
if (options.preload && options.preload.length > 0) {
|
|
131
|
+
setPreloadHints(options.preload);
|
|
132
|
+
}
|
|
133
|
+
if (options.preconnect && options.preconnect.length > 0) {
|
|
134
|
+
setPreconnectHints(options.preconnect);
|
|
135
|
+
}
|
|
136
|
+
if (options.dnsPrefetch && options.dnsPrefetch.length > 0) {
|
|
137
|
+
setDnsPrefetchHints(options.dnsPrefetch);
|
|
138
|
+
}
|
|
139
|
+
// Custom Meta Tags
|
|
140
|
+
if (options.meta && Array.isArray(options.meta)) {
|
|
141
|
+
options.meta.forEach((m) => {
|
|
142
|
+
if (m.name)
|
|
143
|
+
setMetaTag("name", m.name, m.content);
|
|
144
|
+
if (m.property)
|
|
145
|
+
setMetaTag("property", m.property, m.content);
|
|
146
|
+
if (m.httpEquiv)
|
|
147
|
+
setMetaTag("httpEquiv", m.httpEquiv, m.content);
|
|
148
|
+
if (m.charset)
|
|
149
|
+
setMetaTag("charset", m.charset);
|
|
68
150
|
});
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
}
|
|
93
|
-
// Hreflang
|
|
94
|
-
if (options.hreflang && options.hreflang.length > 0) {
|
|
95
|
-
setHreflangTags(options.hreflang);
|
|
96
|
-
}
|
|
97
|
-
// Performance Hints
|
|
98
|
-
if (options.preload && options.preload.length > 0) {
|
|
99
|
-
setPreloadHints(options.preload);
|
|
100
|
-
}
|
|
101
|
-
if (options.preconnect && options.preconnect.length > 0) {
|
|
102
|
-
setPreconnectHints(options.preconnect);
|
|
103
|
-
}
|
|
104
|
-
if (options.dnsPrefetch && options.dnsPrefetch.length > 0) {
|
|
105
|
-
setDnsPrefetchHints(options.dnsPrefetch);
|
|
106
|
-
}
|
|
107
|
-
// Custom Meta Tags
|
|
108
|
-
if (options.meta && Array.isArray(options.meta)) {
|
|
109
|
-
options.meta.forEach((m) => {
|
|
110
|
-
if (m.name)
|
|
111
|
-
setMetaTag("name", m.name, m.content);
|
|
112
|
-
if (m.property)
|
|
113
|
-
setMetaTag("property", m.property, m.content);
|
|
114
|
-
if (m.httpEquiv)
|
|
115
|
-
setMetaTag("httpEquiv", m.httpEquiv, m.content);
|
|
116
|
-
if (m.charset)
|
|
117
|
-
setMetaTag("charset", m.charset);
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
// Custom Link Tags
|
|
121
|
-
if (options.link && Array.isArray(options.link)) {
|
|
122
|
-
options.link.forEach((link) => {
|
|
123
|
-
const attrs = { rel: link.rel, href: link.href };
|
|
124
|
-
if (link.type)
|
|
125
|
-
attrs.type = link.type;
|
|
126
|
-
if (link.sizes)
|
|
127
|
-
attrs.sizes = link.sizes;
|
|
128
|
-
if (link.media)
|
|
129
|
-
attrs.media = link.media;
|
|
130
|
-
if (link.crossOrigin)
|
|
131
|
-
attrs.crossorigin = link.crossOrigin;
|
|
132
|
-
if (link.as)
|
|
133
|
-
attrs.as = link.as;
|
|
134
|
-
setLinkTag(attrs);
|
|
135
|
-
});
|
|
136
|
-
}
|
|
137
|
-
// Open Graph
|
|
138
|
-
setOpenGraph(options.openGraph);
|
|
139
|
-
// Twitter Card
|
|
140
|
-
setTwitterCard(options.twitter, options.openGraph);
|
|
151
|
+
}
|
|
152
|
+
// Custom Link Tags
|
|
153
|
+
if (options.link && Array.isArray(options.link)) {
|
|
154
|
+
options.link.forEach((link) => {
|
|
155
|
+
const attrs = { rel: link.rel, href: link.href };
|
|
156
|
+
if (link.type)
|
|
157
|
+
attrs.type = link.type;
|
|
158
|
+
if (link.sizes)
|
|
159
|
+
attrs.sizes = link.sizes;
|
|
160
|
+
if (link.media)
|
|
161
|
+
attrs.media = link.media;
|
|
162
|
+
if (link.crossOrigin)
|
|
163
|
+
attrs.crossorigin = link.crossOrigin;
|
|
164
|
+
if (link.as)
|
|
165
|
+
attrs.as = link.as;
|
|
166
|
+
setLinkTag(attrs);
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
// Open Graph
|
|
170
|
+
setOpenGraph(options.openGraph);
|
|
171
|
+
// Twitter Card
|
|
172
|
+
setTwitterCard(options.twitter, options.openGraph);
|
|
173
|
+
});
|
|
141
174
|
};
|
|
142
175
|
/**
|
|
143
176
|
* Remove all SEO meta tags added by this library
|
|
@@ -5,6 +5,10 @@
|
|
|
5
5
|
* Helps catch common SEO issues before they affect search rankings.
|
|
6
6
|
*/
|
|
7
7
|
import type { SEOptions, SEOValidationResult, JsonLdTypes } from "../../types/index.js";
|
|
8
|
+
/**
|
|
9
|
+
* Clear validation cache (useful for testing or when options change externally)
|
|
10
|
+
*/
|
|
11
|
+
export declare const clearValidationCache: () => void;
|
|
8
12
|
export declare const isValidUrl: (url: string) => boolean;
|
|
9
13
|
export declare const validateUrl: (url: string, fieldName?: string) => string | null;
|
|
10
14
|
export declare const TITLE_MIN_LENGTH = 30;
|
|
@@ -23,8 +27,8 @@ export declare const validateHreflangEntry: (entry: {
|
|
|
23
27
|
url: string;
|
|
24
28
|
}) => string | null;
|
|
25
29
|
export declare const validateJsonLd: (jsonLd: JsonLdTypes) => string[];
|
|
26
|
-
export declare const validateSEO: (options: SEOptions) => SEOValidationResult;
|
|
27
|
-
export declare const auditSEO: (options: SEOptions) => SEOValidationResult;
|
|
30
|
+
export declare const validateSEO: (options: SEOptions, useCache?: boolean) => SEOValidationResult;
|
|
31
|
+
export declare const auditSEO: (options: SEOptions, enableConsole?: boolean) => SEOValidationResult;
|
|
28
32
|
/**
|
|
29
33
|
* Truncate text to fit within SEO best practices
|
|
30
34
|
*/
|
|
@@ -5,6 +5,36 @@
|
|
|
5
5
|
* Helps catch common SEO issues before they affect search rankings.
|
|
6
6
|
*/
|
|
7
7
|
// ============================================================================
|
|
8
|
+
// Validation Cache
|
|
9
|
+
// ============================================================================
|
|
10
|
+
// Cache validation results to avoid redundant validations
|
|
11
|
+
const validationCache = new WeakMap();
|
|
12
|
+
const validationHashCache = new Map();
|
|
13
|
+
/**
|
|
14
|
+
* Generate a simple hash for caching purposes
|
|
15
|
+
*/
|
|
16
|
+
const generateValidationHash = (options) => {
|
|
17
|
+
return JSON.stringify({
|
|
18
|
+
t: options.title,
|
|
19
|
+
d: options.description,
|
|
20
|
+
l: options.lang,
|
|
21
|
+
c: options.canonical,
|
|
22
|
+
m: options.meta?.length,
|
|
23
|
+
og: options.openGraph?.title || options.openGraph?.description,
|
|
24
|
+
tw: options.twitter?.cardType || options.twitter?.title,
|
|
25
|
+
jl: options.jsonLd?.length,
|
|
26
|
+
hl: options.hreflang?.length,
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* Clear validation cache (useful for testing or when options change externally)
|
|
31
|
+
*/
|
|
32
|
+
export const clearValidationCache = () => {
|
|
33
|
+
// WeakMap doesn't have clear method, we need to create a new one
|
|
34
|
+
// validationCache = new WeakMap<SEOptions, SEOValidationResult>();
|
|
35
|
+
validationHashCache.clear();
|
|
36
|
+
};
|
|
37
|
+
// ============================================================================
|
|
8
38
|
// URL Validation
|
|
9
39
|
// ============================================================================
|
|
10
40
|
export const isValidUrl = (url) => {
|
|
@@ -179,7 +209,17 @@ export const validateJsonLd = (jsonLd) => {
|
|
|
179
209
|
// ============================================================================
|
|
180
210
|
// Comprehensive SEO Validation
|
|
181
211
|
// ============================================================================
|
|
182
|
-
export const validateSEO = (options) => {
|
|
212
|
+
export const validateSEO = (options, useCache = true) => {
|
|
213
|
+
// Check cache first if enabled
|
|
214
|
+
if (useCache) {
|
|
215
|
+
if (validationCache.has(options)) {
|
|
216
|
+
return validationCache.get(options);
|
|
217
|
+
}
|
|
218
|
+
const hash = generateValidationHash(options);
|
|
219
|
+
if (validationHashCache.has(hash)) {
|
|
220
|
+
return validationHashCache.get(hash);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
183
223
|
const errors = [];
|
|
184
224
|
const warnings = [];
|
|
185
225
|
const suggestions = [];
|
|
@@ -287,20 +327,27 @@ export const validateSEO = (options) => {
|
|
|
287
327
|
if (!options.preconnect || options.preconnect.length === 0) {
|
|
288
328
|
suggestions.push("Add preconnect hints for external resources to improve performance");
|
|
289
329
|
}
|
|
290
|
-
|
|
330
|
+
const result = {
|
|
291
331
|
valid: errors.length === 0,
|
|
292
332
|
errors,
|
|
293
333
|
warnings,
|
|
294
334
|
suggestions,
|
|
295
335
|
};
|
|
336
|
+
// Cache the result if caching is enabled
|
|
337
|
+
if (useCache) {
|
|
338
|
+
validationCache.set(options, result);
|
|
339
|
+
const hash = generateValidationHash(options);
|
|
340
|
+
validationHashCache.set(hash, result);
|
|
341
|
+
}
|
|
342
|
+
return result;
|
|
296
343
|
};
|
|
297
344
|
// ============================================================================
|
|
298
345
|
// SEO Audit
|
|
299
346
|
// ============================================================================
|
|
300
|
-
export const auditSEO = (options) => {
|
|
347
|
+
export const auditSEO = (options, enableConsole = true) => {
|
|
301
348
|
const result = validateSEO(options);
|
|
302
|
-
// Console output in development
|
|
303
|
-
if (typeof console !== "undefined") {
|
|
349
|
+
// Console output in development only (or when explicitly enabled)
|
|
350
|
+
if (enableConsole && typeof console !== "undefined" && process.env.NODE_ENV !== "production") {
|
|
304
351
|
const style = {
|
|
305
352
|
error: "color: #d32f2f; font-weight: bold;",
|
|
306
353
|
warning: "color: #f57c00; font-weight: bold;",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umituz/web-seo",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.16",
|
|
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",
|