@umituz/web-seo 2.0.21 → 2.1.1
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 -9
- package/dist/domains/seo/components/JsonLd.js +22 -80
- package/dist/domains/seo/components/SEO.js +5 -35
- package/dist/domains/seo/components/index.d.ts +0 -5
- package/dist/domains/seo/components/index.js +0 -5
- package/dist/domains/seo/core/constants.d.ts +9 -0
- package/dist/domains/seo/core/constants.js +9 -0
- package/dist/domains/seo/core/deep-equal.d.ts +1 -0
- package/dist/domains/seo/core/deep-equal.js +21 -0
- package/dist/domains/seo/core/dom-queue.d.ts +3 -0
- package/dist/domains/seo/core/dom-queue.js +27 -0
- package/dist/domains/seo/core/escape.d.ts +2 -0
- package/dist/domains/seo/core/escape.js +31 -0
- package/dist/domains/seo/core/hash.d.ts +1 -0
- package/dist/domains/seo/core/hash.js +21 -0
- package/dist/domains/seo/core/init.d.ts +4 -2
- package/dist/domains/seo/core/init.js +27 -18
- package/dist/domains/seo/core/seo-strategies.d.ts +19 -0
- package/dist/domains/seo/core/seo-strategies.js +620 -0
- package/dist/domains/seo/core/seo-strategy.d.ts +16 -0
- package/dist/domains/seo/core/seo-strategy.js +1 -0
- package/dist/domains/seo/hooks/index.d.ts +0 -5
- package/dist/domains/seo/hooks/index.js +0 -5
- package/dist/domains/seo/hooks/useJsonLd.d.ts +2 -10
- package/dist/domains/seo/hooks/useJsonLd.js +23 -64
- package/dist/domains/seo/hooks/useSEO.js +2 -30
- package/dist/domains/seo/ssr/helmet.d.ts +18 -0
- package/dist/domains/seo/ssr/helmet.js +16 -0
- package/dist/domains/seo/ssr/index.d.ts +5 -0
- package/dist/domains/seo/ssr/index.js +3 -0
- package/dist/domains/seo/ssr/renderer.d.ts +1 -47
- package/dist/domains/seo/ssr/renderer.js +41 -296
- package/dist/domains/seo/ssr/serializer.d.ts +0 -66
- package/dist/domains/seo/ssr/serializer.js +21 -172
- package/dist/domains/seo/types/index.d.ts +0 -4
- package/dist/domains/seo/types/schema.d.ts +22 -3
- package/dist/domains/seo/types/schema.js +0 -3
- package/dist/domains/seo/utils/dom/dom-context.d.ts +2 -0
- package/dist/domains/seo/utils/dom/dom-context.js +48 -0
- package/dist/domains/seo/utils/dom/dom-mutator.d.ts +9 -0
- package/dist/domains/seo/utils/dom/dom-mutator.js +64 -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 +2 -8
- package/dist/domains/seo/utils/index.js +2 -12
- package/dist/domains/seo/utils/schema-generators/base.d.ts +14 -0
- package/dist/domains/seo/utils/schema-generators/base.js +39 -0
- package/dist/domains/seo/utils/schema-generators/factory.d.ts +20 -0
- package/dist/domains/seo/utils/schema-generators/factory.js +42 -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 +13 -6
- package/dist/domains/seo/utils/schema-generators/index.js +11 -6
- package/dist/domains/seo/utils/schema-generators/types.d.ts +138 -0
- package/dist/domains/seo/utils/schema-generators/types.js +1 -0
- package/dist/domains/seo/utils/validation/hreflang-validator.d.ts +4 -0
- package/dist/domains/seo/utils/validation/hreflang-validator.js +10 -0
- package/dist/domains/seo/utils/validation/image-validator.d.ts +1 -0
- package/dist/domains/seo/utils/validation/image-validator.js +18 -0
- package/dist/domains/seo/utils/validation/index.d.ts +8 -6
- package/dist/domains/seo/utils/validation/index.js +8 -6
- package/dist/domains/seo/utils/validation/jsonld-validator.d.ts +2 -0
- package/dist/domains/seo/utils/validation/jsonld-validator.js +67 -0
- package/dist/domains/seo/utils/validation/language-code.d.ts +1 -0
- package/dist/domains/seo/utils/validation/language-code.js +2 -0
- package/dist/domains/seo/utils/validation/length-validator.d.ts +4 -0
- package/dist/domains/seo/utils/validation/length-validator.js +34 -0
- package/dist/domains/seo/utils/validation/seo-auditor.d.ts +3 -0
- package/dist/domains/seo/utils/validation/seo-auditor.js +120 -0
- package/dist/domains/seo/utils/validation/text-helpers.d.ts +1 -0
- package/dist/domains/seo/utils/validation/text-helpers.js +6 -0
- package/dist/domains/seo/utils/validation/url-validator.d.ts +2 -0
- package/dist/domains/seo/utils/validation/url-validator.js +20 -0
- package/dist/domains/seo/vite/index.d.ts +3 -7
- package/dist/domains/seo/vite/index.js +2 -6
- package/dist/domains/seo/vite/plugin.d.ts +1 -59
- package/dist/domains/seo/vite/plugin.js +26 -190
- package/dist/domains/seo/vite/presets.d.ts +8 -0
- package/dist/domains/seo/vite/presets.js +28 -0
- package/dist/domains/seo/vite/robots-generator.d.ts +6 -0
- package/dist/domains/seo/vite/robots-generator.js +33 -0
- package/dist/domains/seo/vite/sitemap-generator.d.ts +8 -0
- package/dist/domains/seo/vite/sitemap-generator.js +49 -0
- package/dist/domains/seo/vite/types.d.ts +20 -0
- package/dist/domains/seo/vite/types.js +1 -0
- package/dist/index.d.ts +8 -7
- package/dist/index.js +4 -12
- package/package.json +1 -1
|
@@ -1,58 +1,25 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
* React hook for managing JSON-LD structured data.
|
|
5
|
-
* Provides a programmatic way to add/update/remove structured data.
|
|
6
|
-
*/
|
|
7
|
-
import { useEffect, useCallback, useMemo } from "react";
|
|
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();
|
|
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
|
|
17
|
-
const str = JSON.stringify(data);
|
|
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);
|
|
24
|
-
}
|
|
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;
|
|
32
|
-
};
|
|
1
|
+
import { useCallback, useEffect, useMemo } from "react";
|
|
2
|
+
import { generateDataId } from "../core/hash.js";
|
|
3
|
+
const SCRIPT_TYPE = "application/ld+json";
|
|
33
4
|
export const useJsonLd = (data, options) => {
|
|
34
|
-
const { strategy = "afterInteractive", id } = options
|
|
35
|
-
// Generate stable IDs for data items
|
|
5
|
+
const { strategy = "afterInteractive", id } = options ?? {};
|
|
36
6
|
const dataIds = useMemo(() => {
|
|
37
|
-
const
|
|
38
|
-
return
|
|
7
|
+
const items = Array.isArray(data) ? data : [data];
|
|
8
|
+
return items.map((item, index) => id ? `${id}-${index}` : generateDataId(item));
|
|
39
9
|
}, [data, id]);
|
|
40
|
-
const injectJsonLd = useCallback((
|
|
10
|
+
const injectJsonLd = useCallback((payload) => {
|
|
41
11
|
if (typeof document === "undefined")
|
|
42
12
|
return;
|
|
43
|
-
const
|
|
44
|
-
const ids = Array.isArray(
|
|
45
|
-
?
|
|
13
|
+
const items = Array.isArray(payload) ? payload : [payload];
|
|
14
|
+
const ids = Array.isArray(payload) && !id
|
|
15
|
+
? items.map(generateDataId)
|
|
46
16
|
: dataIds;
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
existingScript.remove();
|
|
52
|
-
}
|
|
53
|
-
// Create and inject new script
|
|
17
|
+
items.forEach((item, index) => {
|
|
18
|
+
document
|
|
19
|
+
.querySelector(`script[type="${SCRIPT_TYPE}"][data-seo-id="${ids[index]}"]`)
|
|
20
|
+
?.remove();
|
|
54
21
|
const script = document.createElement("script");
|
|
55
|
-
script.type =
|
|
22
|
+
script.type = SCRIPT_TYPE;
|
|
56
23
|
script.textContent = JSON.stringify(item);
|
|
57
24
|
script.setAttribute("data-seo-id", ids[index]);
|
|
58
25
|
script.setAttribute("data-seo-strategy", strategy);
|
|
@@ -62,24 +29,16 @@ export const useJsonLd = (data, options) => {
|
|
|
62
29
|
const removeJsonLd = useCallback(() => {
|
|
63
30
|
if (typeof document === "undefined")
|
|
64
31
|
return;
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
});
|
|
32
|
+
for (const dataId of dataIds) {
|
|
33
|
+
document
|
|
34
|
+
.querySelector(`script[type="${SCRIPT_TYPE}"][data-seo-id="${dataId}"]`)
|
|
35
|
+
?.remove();
|
|
36
|
+
}
|
|
71
37
|
}, [dataIds]);
|
|
72
38
|
useEffect(() => {
|
|
73
|
-
if (data)
|
|
39
|
+
if (data)
|
|
74
40
|
injectJsonLd(data);
|
|
75
|
-
|
|
76
|
-
return () => {
|
|
77
|
-
removeJsonLd();
|
|
78
|
-
};
|
|
41
|
+
return () => removeJsonLd();
|
|
79
42
|
}, [data, injectJsonLd, removeJsonLd]);
|
|
80
|
-
return {
|
|
81
|
-
injectJsonLd,
|
|
82
|
-
removeJsonLd,
|
|
83
|
-
};
|
|
43
|
+
return { injectJsonLd, removeJsonLd };
|
|
84
44
|
};
|
|
85
|
-
export default useJsonLd;
|
|
@@ -1,37 +1,9 @@
|
|
|
1
1
|
import { useEffect, useRef } from "react";
|
|
2
|
-
import {
|
|
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
|
-
};
|
|
2
|
+
import { deepEqual } from "../core/deep-equal.js";
|
|
3
|
+
import { updateSEO } from "../utils/dom/index.js";
|
|
30
4
|
export const useSEO = (options) => {
|
|
31
|
-
// Use ref to store the previous options for deep comparison
|
|
32
5
|
const prevOptionsRef = useRef(null);
|
|
33
6
|
useEffect(() => {
|
|
34
|
-
// Only update if options have actually changed
|
|
35
7
|
if (!prevOptionsRef.current || !deepEqual(prevOptionsRef.current, options)) {
|
|
36
8
|
prevOptionsRef.current = options;
|
|
37
9
|
updateSEO(options);
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { SEOptions } from "../types/index.js";
|
|
2
|
+
export interface RenderedMetaTag {
|
|
3
|
+
toString(): string;
|
|
4
|
+
toComponent(): unknown;
|
|
5
|
+
}
|
|
6
|
+
export interface HelmetData {
|
|
7
|
+
base: RenderedMetaTag[];
|
|
8
|
+
bodyAttributes: Record<string, string>;
|
|
9
|
+
htmlAttributes: Record<string, string>;
|
|
10
|
+
link: RenderedMetaTag[];
|
|
11
|
+
meta: RenderedMetaTag[];
|
|
12
|
+
noscript: RenderedMetaTag[];
|
|
13
|
+
script: RenderedMetaTag[];
|
|
14
|
+
style: RenderedMetaTag[];
|
|
15
|
+
title: string;
|
|
16
|
+
titleAttributes: Record<string, string>;
|
|
17
|
+
}
|
|
18
|
+
export declare const renderAsHelmet: (options: SEOptions) => HelmetData;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { renderSEO } from "./renderer.js";
|
|
2
|
+
export const renderAsHelmet = (options) => {
|
|
3
|
+
const rendered = renderSEO(options);
|
|
4
|
+
return {
|
|
5
|
+
base: [],
|
|
6
|
+
bodyAttributes: {},
|
|
7
|
+
htmlAttributes: options.lang ? { lang: options.lang } : {},
|
|
8
|
+
link: [],
|
|
9
|
+
meta: [],
|
|
10
|
+
noscript: [],
|
|
11
|
+
script: [],
|
|
12
|
+
style: [],
|
|
13
|
+
title: rendered.title,
|
|
14
|
+
titleAttributes: {},
|
|
15
|
+
};
|
|
16
|
+
};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { renderSEO, generateHead, extractSEOData, hydrateSEO } from "./renderer.js";
|
|
2
|
+
export { renderAsHelmet } from "./helmet.js";
|
|
3
|
+
export type { HelmetData, RenderedMetaTag } from "./helmet.js";
|
|
4
|
+
export type { RenderedSEO } from "./renderer.js";
|
|
5
|
+
export { serializeSEO, deserializeSEO, serializeSEOToBase64, deserializeSEOFromBase64, safeDeserializeSEO, } from "./serializer.js";
|
|
@@ -1,59 +1,13 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* SEO SSR Renderer
|
|
3
|
-
*
|
|
4
|
-
* Server-side rendering utilities for SEO.
|
|
5
|
-
* Generates HTML strings for meta tags and structured data.
|
|
6
|
-
*/
|
|
7
1
|
import type { SEOptions } from "../types/index.js";
|
|
8
2
|
export interface RenderedSEO {
|
|
9
|
-
/** Title tag */
|
|
10
3
|
title: string;
|
|
11
|
-
/** All meta tags as HTML */
|
|
12
4
|
meta: string;
|
|
13
|
-
/** All link tags as HTML */
|
|
14
5
|
link: string;
|
|
15
|
-
/** All JSON-LD scripts as HTML */
|
|
16
6
|
jsonLd: string;
|
|
17
|
-
/** Complete HTML head content */
|
|
18
7
|
toHTML(): string;
|
|
19
|
-
/** Convert to string (same as toHTML) */
|
|
20
8
|
toString(): string;
|
|
21
9
|
}
|
|
22
|
-
|
|
23
|
-
* Render SEO options to HTML strings for server-side rendering
|
|
24
|
-
* Optimized with caching and efficient string building
|
|
25
|
-
*/
|
|
26
|
-
export declare const renderSEO: (options: SEOptions, useCache?: boolean) => RenderedSEO;
|
|
27
|
-
/**
|
|
28
|
-
* Generate complete HTML head with SEO tags
|
|
29
|
-
*/
|
|
10
|
+
export declare const renderSEO: (options: SEOptions) => RenderedSEO;
|
|
30
11
|
export declare const generateHead: (options: SEOptions) => string;
|
|
31
|
-
/**
|
|
32
|
-
* Extract SEO data for hydration
|
|
33
|
-
*/
|
|
34
12
|
export declare const extractSEOData: (options: SEOptions) => string;
|
|
35
|
-
/**
|
|
36
|
-
* Hydrate SEO on client side
|
|
37
|
-
* @throws {SyntaxError} If serializedData is not valid JSON
|
|
38
|
-
*/
|
|
39
13
|
export declare const hydrateSEO: (serializedData: string) => void;
|
|
40
|
-
export interface RenderedMetaTag {
|
|
41
|
-
toString(): string;
|
|
42
|
-
toComponent(): unknown;
|
|
43
|
-
}
|
|
44
|
-
export interface HelmetData {
|
|
45
|
-
base: RenderedMetaTag[];
|
|
46
|
-
bodyAttributes: Record<string, string>;
|
|
47
|
-
htmlAttributes: Record<string, string>;
|
|
48
|
-
link: RenderedMetaTag[];
|
|
49
|
-
meta: RenderedMetaTag[];
|
|
50
|
-
noscript: RenderedMetaTag[];
|
|
51
|
-
script: RenderedMetaTag[];
|
|
52
|
-
style: RenderedMetaTag[];
|
|
53
|
-
title: string;
|
|
54
|
-
titleAttributes: Record<string, string>;
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Render SEO in Helmet-compatible format
|
|
58
|
-
*/
|
|
59
|
-
export declare const renderAsHelmet: (options: SEOptions) => HelmetData;
|
|
@@ -1,253 +1,34 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
* Optimized with caching and single-pass replacement
|
|
15
|
-
*/
|
|
16
|
-
const escapeHTML = (str) => {
|
|
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;
|
|
1
|
+
import { escapeHTML } from "../core/escape.js";
|
|
2
|
+
import { SEO_STRATEGIES } from "../core/seo-strategies.js";
|
|
3
|
+
import { updateSEO } from "../utils/dom/index.js";
|
|
4
|
+
const buildHTMLContext = () => ({ meta: [], link: [], jsonLd: [] });
|
|
5
|
+
const SEPARATOR = "\n ";
|
|
6
|
+
const joinTags = (ctx) => ({
|
|
7
|
+
meta: ctx.meta.join(SEPARATOR),
|
|
8
|
+
link: ctx.link.join(SEPARATOR),
|
|
9
|
+
jsonLd: ctx.jsonLd.join(SEPARATOR),
|
|
10
|
+
});
|
|
11
|
+
const findTitle = (meta) => {
|
|
12
|
+
const match = meta.match(/<title>([\s\S]*?)<\/title>/);
|
|
13
|
+
return match ? match[1] : "";
|
|
57
14
|
};
|
|
58
|
-
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
const metaTagsBuilder = [];
|
|
71
|
-
const linkTagsBuilder = [];
|
|
72
|
-
const jsonLdScriptsBuilder = [];
|
|
73
|
-
// Title
|
|
74
|
-
const title = options.titleTemplate
|
|
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 = {
|
|
15
|
+
const stripTitle = (meta) => meta.replace(/<title>[\s\S]*?<\/title>\n?/, "");
|
|
16
|
+
export const renderSEO = (options) => {
|
|
17
|
+
const ctx = buildHTMLContext();
|
|
18
|
+
for (const strategy of SEO_STRATEGIES) {
|
|
19
|
+
if (!strategy.applies(options))
|
|
20
|
+
continue;
|
|
21
|
+
strategy.applyHTML(options, ctx);
|
|
22
|
+
}
|
|
23
|
+
const joined = joinTags(ctx);
|
|
24
|
+
const title = findTitle(joined.meta);
|
|
25
|
+
const meta = stripTitle(joined.meta);
|
|
26
|
+
return {
|
|
245
27
|
title,
|
|
246
|
-
meta
|
|
247
|
-
link:
|
|
248
|
-
jsonLd:
|
|
28
|
+
meta,
|
|
29
|
+
link: joined.link,
|
|
30
|
+
jsonLd: joined.jsonLd,
|
|
249
31
|
toHTML() {
|
|
250
|
-
// Use template literals for better performance
|
|
251
32
|
const parts = [` <title>${this.title}</title>`];
|
|
252
33
|
if (this.meta)
|
|
253
34
|
parts.push(` ${this.meta}`);
|
|
@@ -261,34 +42,18 @@ export const renderSEO = (options, useCache = true) => {
|
|
|
261
42
|
return this.toHTML();
|
|
262
43
|
},
|
|
263
44
|
};
|
|
264
|
-
// Cache the result
|
|
265
|
-
if (useCache) {
|
|
266
|
-
renderCache.set(options, result);
|
|
267
|
-
}
|
|
268
|
-
return result;
|
|
269
45
|
};
|
|
270
|
-
// ============================================================================
|
|
271
|
-
// SSR SEO Utilities
|
|
272
|
-
// ============================================================================
|
|
273
|
-
/**
|
|
274
|
-
* Generate complete HTML head with SEO tags
|
|
275
|
-
*/
|
|
276
46
|
export const generateHead = (options) => {
|
|
277
47
|
const rendered = renderSEO(options);
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
export const extractSEOData = (options) => {
|
|
286
|
-
return JSON.stringify(options);
|
|
48
|
+
const langAttr = options.lang ? ` lang="${escapeHTML(options.lang)}"` : "";
|
|
49
|
+
const indented = rendered
|
|
50
|
+
.toHTML()
|
|
51
|
+
.split("\n")
|
|
52
|
+
.map((line) => ` ${line}`)
|
|
53
|
+
.join("\n");
|
|
54
|
+
return ` <head${langAttr}>\n${indented}\n </head>`;
|
|
287
55
|
};
|
|
288
|
-
|
|
289
|
-
* Hydrate SEO on client side
|
|
290
|
-
* @throws {SyntaxError} If serializedData is not valid JSON
|
|
291
|
-
*/
|
|
56
|
+
export const extractSEOData = (options) => JSON.stringify(options);
|
|
292
57
|
export const hydrateSEO = (serializedData) => {
|
|
293
58
|
if (typeof window === "undefined")
|
|
294
59
|
return;
|
|
@@ -300,30 +65,10 @@ export const hydrateSEO = (serializedData) => {
|
|
|
300
65
|
console.error("[SEO] Failed to hydrate SEO data:", error);
|
|
301
66
|
return;
|
|
302
67
|
}
|
|
303
|
-
|
|
304
|
-
import("../utils/dom.js")
|
|
305
|
-
.then(({ updateSEO }) => {
|
|
68
|
+
try {
|
|
306
69
|
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
|
-
};
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
console.error("[SEO] Failed to apply hydrated SEO data:", error);
|
|
73
|
+
}
|
|
329
74
|
};
|