@umituz/web-seo 2.0.20 → 2.1.0
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 +7 -6
- package/dist/domains/seo/components/JsonLd.js +26 -77
- package/dist/domains/seo/components/SEO.d.ts +7 -0
- package/dist/domains/seo/components/SEO.js +10 -33
- package/dist/domains/seo/core/constants.d.ts +15 -0
- package/dist/domains/seo/core/constants.js +15 -0
- package/dist/domains/seo/core/deep-equal.d.ts +8 -0
- package/dist/domains/seo/core/deep-equal.js +28 -0
- package/dist/domains/seo/core/dom-queue.d.ts +10 -0
- package/dist/domains/seo/core/dom-queue.js +37 -0
- package/dist/domains/seo/core/escape.d.ts +12 -0
- package/dist/domains/seo/core/escape.js +41 -0
- package/dist/domains/seo/core/hash.d.ts +9 -0
- package/dist/domains/seo/core/hash.js +29 -0
- package/dist/domains/seo/core/init.d.ts +15 -2
- package/dist/domains/seo/core/init.js +38 -18
- package/dist/domains/seo/core/seo-strategies.d.ts +29 -0
- package/dist/domains/seo/core/seo-strategies.js +694 -0
- package/dist/domains/seo/core/seo-strategy.d.ts +37 -0
- package/dist/domains/seo/core/seo-strategy.js +14 -0
- package/dist/domains/seo/hooks/useJsonLd.d.ts +6 -7
- package/dist/domains/seo/hooks/useJsonLd.js +27 -61
- package/dist/domains/seo/hooks/useSEO.d.ts +6 -0
- package/dist/domains/seo/hooks/useSEO.js +7 -29
- package/dist/domains/seo/ssr/helmet.d.ts +25 -0
- package/dist/domains/seo/ssr/helmet.js +23 -0
- package/dist/domains/seo/ssr/index.d.ts +10 -0
- package/dist/domains/seo/ssr/index.js +8 -0
- package/dist/domains/seo/ssr/renderer.d.ts +11 -36
- package/dist/domains/seo/ssr/renderer.js +51 -285
- package/dist/domains/seo/ssr/serializer.d.ts +5 -60
- package/dist/domains/seo/ssr/serializer.js +26 -166
- package/dist/domains/seo/types/index.d.ts +0 -4
- package/dist/domains/seo/types/schema.d.ts +22 -1
- package/dist/domains/seo/utils/dom/dom-context.d.ts +13 -0
- package/dist/domains/seo/utils/dom/dom-context.js +62 -0
- package/dist/domains/seo/utils/dom/dom-mutator.d.ts +31 -0
- package/dist/domains/seo/utils/dom/dom-mutator.js +89 -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 +3 -4
- package/dist/domains/seo/utils/index.js +3 -5
- package/dist/domains/seo/utils/schema-generators/base.d.ts +25 -0
- package/dist/domains/seo/utils/schema-generators/base.js +50 -0
- package/dist/domains/seo/utils/schema-generators/factory.d.ts +27 -0
- package/dist/domains/seo/utils/schema-generators/factory.js +49 -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 +15 -2
- package/dist/domains/seo/utils/schema-generators/index.js +13 -2
- package/dist/domains/seo/utils/schema-generators/types.d.ts +142 -0
- package/dist/domains/seo/utils/schema-generators/types.js +5 -0
- package/dist/domains/seo/utils/validation/hreflang-validator.d.ts +7 -0
- package/dist/domains/seo/utils/validation/hreflang-validator.js +13 -0
- package/dist/domains/seo/utils/validation/image-validator.d.ts +7 -0
- package/dist/domains/seo/utils/validation/image-validator.js +24 -0
- package/dist/domains/seo/utils/validation/index.d.ts +10 -2
- package/dist/domains/seo/utils/validation/index.js +10 -2
- package/dist/domains/seo/utils/validation/jsonld-validator.d.ts +9 -0
- package/dist/domains/seo/utils/validation/jsonld-validator.js +74 -0
- package/dist/domains/seo/utils/validation/language-code.d.ts +7 -0
- package/dist/domains/seo/utils/validation/language-code.js +8 -0
- package/dist/domains/seo/utils/validation/length-validator.d.ts +10 -0
- package/dist/domains/seo/utils/validation/length-validator.js +40 -0
- package/dist/domains/seo/utils/validation/seo-auditor.d.ts +17 -0
- package/dist/domains/seo/utils/validation/seo-auditor.js +144 -0
- package/dist/domains/seo/utils/validation/text-helpers.d.ts +7 -0
- package/dist/domains/seo/utils/validation/text-helpers.js +12 -0
- package/dist/domains/seo/utils/validation/url-validator.d.ts +8 -0
- package/dist/domains/seo/utils/validation/url-validator.js +26 -0
- package/dist/domains/seo/vite/index.d.ts +6 -4
- package/dist/domains/seo/vite/index.js +5 -3
- package/dist/domains/seo/vite/plugin.d.ts +3 -55
- package/dist/domains/seo/vite/plugin.js +28 -186
- package/dist/domains/seo/vite/presets.d.ts +14 -0
- package/dist/domains/seo/vite/presets.js +33 -0
- package/dist/domains/seo/vite/robots-generator.d.ts +9 -0
- package/dist/domains/seo/vite/robots-generator.js +36 -0
- package/dist/domains/seo/vite/sitemap-generator.d.ts +15 -0
- package/dist/domains/seo/vite/sitemap-generator.js +56 -0
- package/dist/domains/seo/vite/types.d.ts +29 -0
- package/dist/domains/seo/vite/types.js +4 -0
- package/dist/index.d.ts +8 -7
- package/dist/index.js +7 -6
- package/package.json +1 -1
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* JsonLd
|
|
2
|
+
* `<JsonLd>` component.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
* Injects one or more JSON-LD <script> tags into the document head.
|
|
5
|
+
* Each payload gets a stable id (cyrb53 hash) so re-renders with the
|
|
6
|
+
* same content re-use the existing node instead of appending a new one.
|
|
6
7
|
*/
|
|
7
8
|
import type { JsonLdTypes } from "../types/schema.js";
|
|
8
9
|
interface JsonLdProps {
|
|
9
10
|
data: JsonLdTypes | JsonLdTypes[];
|
|
10
|
-
/** Strategy
|
|
11
|
+
/** Strategy label, attached as `data-seo-strategy` for debugging. */
|
|
11
12
|
strategy?: "afterInteractive" | "beforeInteractive" | "idle";
|
|
12
|
-
/**
|
|
13
|
+
/** Optional explicit id; otherwise one is derived from the payload. */
|
|
13
14
|
id?: string;
|
|
14
15
|
}
|
|
15
16
|
export declare const JsonLd: import("react").MemoExoticComponent<({ data, strategy, id }: JsonLdProps) => null>;
|
|
16
|
-
export
|
|
17
|
+
export {};
|
|
@@ -1,97 +1,46 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* JsonLd
|
|
2
|
+
* `<JsonLd>` component.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
* Injects one or more JSON-LD <script> tags into the document head.
|
|
5
|
+
* Each payload gets a stable id (cyrb53 hash) so re-renders with the
|
|
6
|
+
* same content re-use the existing node instead of appending a new one.
|
|
6
7
|
*/
|
|
7
|
-
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const
|
|
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)
|
|
16
|
-
const str = JSON.stringify(data);
|
|
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);
|
|
44
|
-
}
|
|
45
|
-
return false;
|
|
46
|
-
};
|
|
8
|
+
import { memo, useEffect, useMemo, useRef } from "react";
|
|
9
|
+
import { deepEqual } from "../core/deep-equal.js";
|
|
10
|
+
import { generateDataId } from "../core/hash.js";
|
|
11
|
+
const SCRIPT_TYPE = "application/ld+json";
|
|
47
12
|
const JsonLdComponent = ({ data, strategy = "afterInteractive", id }) => {
|
|
48
13
|
const scriptRef = useRef(null);
|
|
49
|
-
// Generate stable IDs for data items
|
|
50
14
|
const dataIds = useMemo(() => {
|
|
51
|
-
const
|
|
52
|
-
return
|
|
15
|
+
const items = Array.isArray(data) ? data : [data];
|
|
16
|
+
return items.map((item, index) => id ? `${id}-${index}` : generateDataId(item));
|
|
53
17
|
}, [data, id]);
|
|
54
18
|
useEffect(() => {
|
|
55
19
|
if (typeof document === "undefined")
|
|
56
20
|
return;
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
const existingScript = document.querySelector(`script[type="application/ld+json"][data-seo-id="${dataId}"]`);
|
|
62
|
-
if (existingScript) {
|
|
63
|
-
existingScript.remove();
|
|
64
|
-
}
|
|
65
|
-
});
|
|
66
|
-
// Create and inject new script(s)
|
|
67
|
-
dataArray.forEach((jsonData, index) => {
|
|
21
|
+
const items = Array.isArray(data) ? data : [data];
|
|
22
|
+
for (let index = 0; index < items.length; index++) {
|
|
23
|
+
const existing = document.querySelector(`script[type="${SCRIPT_TYPE}"][data-seo-id="${dataIds[index]}"]`);
|
|
24
|
+
existing?.remove();
|
|
68
25
|
const script = document.createElement("script");
|
|
69
|
-
script.type =
|
|
70
|
-
script.textContent = JSON.stringify(
|
|
71
|
-
// Add identifier for duplicate detection and debugging
|
|
26
|
+
script.type = SCRIPT_TYPE;
|
|
27
|
+
script.textContent = JSON.stringify(items[index]);
|
|
72
28
|
script.setAttribute("data-seo-id", dataIds[index]);
|
|
73
29
|
script.setAttribute("data-seo-strategy", strategy);
|
|
74
30
|
document.head.appendChild(script);
|
|
75
|
-
if (index === 0)
|
|
31
|
+
if (index === 0)
|
|
76
32
|
scriptRef.current = script;
|
|
77
|
-
|
|
78
|
-
});
|
|
79
|
-
// Cleanup function
|
|
33
|
+
}
|
|
80
34
|
return () => {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
});
|
|
35
|
+
for (const dataId of dataIds) {
|
|
36
|
+
document
|
|
37
|
+
.querySelector(`script[type="${SCRIPT_TYPE}"][data-seo-id="${dataId}"]`)
|
|
38
|
+
?.remove();
|
|
39
|
+
}
|
|
87
40
|
};
|
|
88
41
|
}, [data, strategy, dataIds]);
|
|
89
42
|
return null;
|
|
90
43
|
};
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
prevProps.id === nextProps.id &&
|
|
95
|
-
deepEqual(prevProps.data, nextProps.data));
|
|
96
|
-
});
|
|
97
|
-
export default JsonLd;
|
|
44
|
+
export const JsonLd = memo(JsonLdComponent, (prev, next) => prev.strategy === next.strategy &&
|
|
45
|
+
prev.id === next.id &&
|
|
46
|
+
deepEqual(prev.data, next.data));
|
|
@@ -1,2 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `<SEO>` component.
|
|
3
|
+
*
|
|
4
|
+
* Applies SEO options to the document head on mount and whenever the
|
|
5
|
+
* props change. The deep-compare gate prevents re-running the DOM
|
|
6
|
+
* mutator when a parent re-renders with a structurally-equal object.
|
|
7
|
+
*/
|
|
1
8
|
import type { SEOptions } from "../types/index.js";
|
|
2
9
|
export declare const SEO: import("react").MemoExoticComponent<(props: SEOptions) => null>;
|
|
@@ -1,44 +1,21 @@
|
|
|
1
|
-
import { useEffect, useRef, memo } from "react";
|
|
2
|
-
import { updateSEO } from "../utils/dom.js";
|
|
3
1
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
2
|
+
* `<SEO>` component.
|
|
3
|
+
*
|
|
4
|
+
* Applies SEO options to the document head on mount and whenever the
|
|
5
|
+
* props change. The deep-compare gate prevents re-running the DOM
|
|
6
|
+
* mutator when a parent re-renders with a structurally-equal object.
|
|
6
7
|
*/
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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
|
-
};
|
|
8
|
+
import { memo, useEffect, useRef } from "react";
|
|
9
|
+
import { deepEqual } from "../core/deep-equal.js";
|
|
10
|
+
import { updateSEO } from "../utils/dom/index.js";
|
|
30
11
|
const SEOComponent = (props) => {
|
|
31
12
|
const prevPropsRef = useRef(null);
|
|
32
13
|
useEffect(() => {
|
|
33
|
-
// Only update if props have actually changed using deep comparison
|
|
34
14
|
if (!prevPropsRef.current || !deepEqual(prevPropsRef.current, props)) {
|
|
35
15
|
prevPropsRef.current = props;
|
|
36
16
|
updateSEO(props);
|
|
37
17
|
}
|
|
38
18
|
}, [props]);
|
|
39
|
-
return null;
|
|
19
|
+
return null;
|
|
40
20
|
};
|
|
41
|
-
|
|
42
|
-
export const SEO = memo(SEOComponent, (prevProps, nextProps) => {
|
|
43
|
-
return deepEqual(prevProps, nextProps);
|
|
44
|
-
});
|
|
21
|
+
export const SEO = memo(SEOComponent, (prev, next) => deepEqual(prev, next));
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SEO Constants
|
|
3
|
+
*
|
|
4
|
+
* Centralized length and size thresholds for SEO validation.
|
|
5
|
+
* Used by both the validator and the generator modules.
|
|
6
|
+
*/
|
|
7
|
+
export declare const TITLE_MIN_LENGTH = 30;
|
|
8
|
+
export declare const TITLE_MAX_LENGTH = 60;
|
|
9
|
+
export declare const DESCRIPTION_MIN_LENGTH = 120;
|
|
10
|
+
export declare const DESCRIPTION_MAX_LENGTH = 160;
|
|
11
|
+
export declare const OG_IMAGE_MIN_WIDTH = 1200;
|
|
12
|
+
export declare const OG_IMAGE_MIN_HEIGHT = 630;
|
|
13
|
+
export declare const OG_IMAGE_MAX_SIZE: number;
|
|
14
|
+
export declare const SCHEMA_CONTEXT = "https://schema.org";
|
|
15
|
+
export declare const DEFAULT_CACHE_LIMIT = 1000;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SEO Constants
|
|
3
|
+
*
|
|
4
|
+
* Centralized length and size thresholds for SEO validation.
|
|
5
|
+
* Used by both the validator and the generator modules.
|
|
6
|
+
*/
|
|
7
|
+
export const TITLE_MIN_LENGTH = 30;
|
|
8
|
+
export const TITLE_MAX_LENGTH = 60;
|
|
9
|
+
export const DESCRIPTION_MIN_LENGTH = 120;
|
|
10
|
+
export const DESCRIPTION_MAX_LENGTH = 160;
|
|
11
|
+
export const OG_IMAGE_MIN_WIDTH = 1200;
|
|
12
|
+
export const OG_IMAGE_MIN_HEIGHT = 630;
|
|
13
|
+
export const OG_IMAGE_MAX_SIZE = 8 * 1024 * 1024; // 8MB
|
|
14
|
+
export const SCHEMA_CONTEXT = "https://schema.org";
|
|
15
|
+
export const DEFAULT_CACHE_LIMIT = 1000;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deep equality for SEO options and JSON-LD payloads.
|
|
3
|
+
*
|
|
4
|
+
* Used by React component memoization, the useSEO hook, and the JsonLd hook
|
|
5
|
+
* to avoid re-running DOM mutations or re-injecting <script> tags when the
|
|
6
|
+
* incoming object reference changes but the content is structurally equal.
|
|
7
|
+
*/
|
|
8
|
+
export declare const deepEqual: (a: unknown, b: unknown) => boolean;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deep equality for SEO options and JSON-LD payloads.
|
|
3
|
+
*
|
|
4
|
+
* Used by React component memoization, the useSEO hook, and the JsonLd hook
|
|
5
|
+
* to avoid re-running DOM mutations or re-injecting <script> tags when the
|
|
6
|
+
* incoming object reference changes but the content is structurally equal.
|
|
7
|
+
*/
|
|
8
|
+
export const deepEqual = (a, b) => {
|
|
9
|
+
if (a === b)
|
|
10
|
+
return true;
|
|
11
|
+
if (a == null || b == null)
|
|
12
|
+
return a === b;
|
|
13
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
14
|
+
if (a.length !== b.length)
|
|
15
|
+
return false;
|
|
16
|
+
return a.every((item, index) => deepEqual(item, b[index]));
|
|
17
|
+
}
|
|
18
|
+
if (typeof a === "object" && typeof b === "object") {
|
|
19
|
+
const recordA = a;
|
|
20
|
+
const recordB = b;
|
|
21
|
+
const keysA = Object.keys(recordA);
|
|
22
|
+
const keysB = Object.keys(recordB);
|
|
23
|
+
if (keysA.length !== keysB.length)
|
|
24
|
+
return false;
|
|
25
|
+
return keysA.every((key) => keysB.includes(key) && deepEqual(recordA[key], recordB[key]));
|
|
26
|
+
}
|
|
27
|
+
return false;
|
|
28
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Batched DOM update queue.
|
|
3
|
+
*
|
|
4
|
+
* Coalesces many small SEO mutations into a single requestAnimationFrame
|
|
5
|
+
* pass, preventing layout thrashing and giving us a single try/catch
|
|
6
|
+
* boundary for all queued work.
|
|
7
|
+
*/
|
|
8
|
+
type DOMUpdate = () => void;
|
|
9
|
+
export declare const queueDOMUpdate: (update: DOMUpdate) => void;
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Batched DOM update queue.
|
|
3
|
+
*
|
|
4
|
+
* Coalesces many small SEO mutations into a single requestAnimationFrame
|
|
5
|
+
* pass, preventing layout thrashing and giving us a single try/catch
|
|
6
|
+
* boundary for all queued work.
|
|
7
|
+
*/
|
|
8
|
+
const updateQueue = [];
|
|
9
|
+
let scheduled = false;
|
|
10
|
+
const flush = () => {
|
|
11
|
+
const updates = updateQueue.splice(0, updateQueue.length);
|
|
12
|
+
scheduled = false;
|
|
13
|
+
for (const update of updates) {
|
|
14
|
+
try {
|
|
15
|
+
update();
|
|
16
|
+
}
|
|
17
|
+
catch (error) {
|
|
18
|
+
// Surface the error: silent failure here would leave the DOM
|
|
19
|
+
// silently half-updated and impossible to debug.
|
|
20
|
+
console.error("[SEO] DOM update failed:", error);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
// Re-entries during this flush get rescheduled.
|
|
24
|
+
if (updateQueue.length > 0) {
|
|
25
|
+
schedule();
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
const schedule = () => {
|
|
29
|
+
if (scheduled)
|
|
30
|
+
return;
|
|
31
|
+
scheduled = true;
|
|
32
|
+
requestAnimationFrame(flush);
|
|
33
|
+
};
|
|
34
|
+
export const queueDOMUpdate = (update) => {
|
|
35
|
+
updateQueue.push(update);
|
|
36
|
+
schedule();
|
|
37
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTML / JSON escaping for safe SSR output.
|
|
3
|
+
*
|
|
4
|
+
* Bounded caches prevent unbounded growth when the renderer is fed many
|
|
5
|
+
* long-lived pages on a Node server.
|
|
6
|
+
*/
|
|
7
|
+
export declare const escapeHTML: (str: string) => string;
|
|
8
|
+
/**
|
|
9
|
+
* Escape JSON for inclusion inside a <script> tag.
|
|
10
|
+
* Prevents `</script>` inside user JSON from breaking out of the script.
|
|
11
|
+
*/
|
|
12
|
+
export declare const escapeJSONForScript: (json: string) => string;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HTML / JSON escaping for safe SSR output.
|
|
3
|
+
*
|
|
4
|
+
* Bounded caches prevent unbounded growth when the renderer is fed many
|
|
5
|
+
* long-lived pages on a Node server.
|
|
6
|
+
*/
|
|
7
|
+
import { DEFAULT_CACHE_LIMIT } from "./constants.js";
|
|
8
|
+
const escapeCache = new Map();
|
|
9
|
+
const pushCached = (key, value) => {
|
|
10
|
+
if (escapeCache.size < DEFAULT_CACHE_LIMIT) {
|
|
11
|
+
escapeCache.set(key, value);
|
|
12
|
+
}
|
|
13
|
+
return value;
|
|
14
|
+
};
|
|
15
|
+
export const escapeHTML = (str) => {
|
|
16
|
+
if (!str)
|
|
17
|
+
return "";
|
|
18
|
+
if (escapeCache.has(str))
|
|
19
|
+
return escapeCache.get(str);
|
|
20
|
+
const escaped = str.replace(/[&<>"']/g, (char) => {
|
|
21
|
+
switch (char) {
|
|
22
|
+
case "&": return "&";
|
|
23
|
+
case "<": return "<";
|
|
24
|
+
case ">": return ">";
|
|
25
|
+
case '"': return """;
|
|
26
|
+
case "'": return "'";
|
|
27
|
+
default: return char;
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
return pushCached(str, escaped);
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Escape JSON for inclusion inside a <script> tag.
|
|
34
|
+
* Prevents `</script>` inside user JSON from breaking out of the script.
|
|
35
|
+
*/
|
|
36
|
+
export const escapeJSONForScript = (json) => {
|
|
37
|
+
const cacheKey = `json:${json}`;
|
|
38
|
+
if (escapeCache.has(cacheKey))
|
|
39
|
+
return escapeCache.get(cacheKey);
|
|
40
|
+
return pushCached(cacheKey, json.replace(/<\/script>/gi, "<\\/script>"));
|
|
41
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* cyrb53 content hash with WeakMap memoization.
|
|
3
|
+
*
|
|
4
|
+
* Produces a stable, URL-safe id from any JSON-LD payload. Used by both
|
|
5
|
+
* the JsonLd component and the useJsonLd hook to give each schema instance
|
|
6
|
+
* a deterministic script-tag id, so the same payload re-uses the same <script>
|
|
7
|
+
* node and the DOM stays clean.
|
|
8
|
+
*/
|
|
9
|
+
export declare const generateDataId: (data: object) => string;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* cyrb53 content hash with WeakMap memoization.
|
|
3
|
+
*
|
|
4
|
+
* Produces a stable, URL-safe id from any JSON-LD payload. Used by both
|
|
5
|
+
* the JsonLd component and the useJsonLd hook to give each schema instance
|
|
6
|
+
* a deterministic script-tag id, so the same payload re-uses the same <script>
|
|
7
|
+
* node and the DOM stays clean.
|
|
8
|
+
*/
|
|
9
|
+
const hashCache = new WeakMap();
|
|
10
|
+
const cyrb53 = (str) => {
|
|
11
|
+
let h1 = 0xdeadbeef;
|
|
12
|
+
let h2 = 0x41c6ce57;
|
|
13
|
+
for (let i = 0; i < str.length; i++) {
|
|
14
|
+
const ch = str.charCodeAt(i);
|
|
15
|
+
h1 = Math.imul(h1 ^ ch, 2654435761);
|
|
16
|
+
h2 = Math.imul(h2 ^ ch, 1597334677);
|
|
17
|
+
}
|
|
18
|
+
h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^ Math.imul(h2 ^ (h2 >>> 13), 3266489909);
|
|
19
|
+
h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^ Math.imul(h1 ^ (h1 >>> 13), 3266489909);
|
|
20
|
+
return (4294967296 * (2097151 & h2) + (h1 >>> 0)).toString(36);
|
|
21
|
+
};
|
|
22
|
+
export const generateDataId = (data) => {
|
|
23
|
+
if (hashCache.has(data)) {
|
|
24
|
+
return hashCache.get(data);
|
|
25
|
+
}
|
|
26
|
+
const id = `jsonld-${cyrb53(JSON.stringify(data))}`;
|
|
27
|
+
hashCache.set(data, id);
|
|
28
|
+
return id;
|
|
29
|
+
};
|
|
@@ -1,16 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `initSEO(config)` — application-level SEO bootstrap.
|
|
3
|
+
*
|
|
4
|
+
* Sets default SEO options on first call and re-applies them whenever
|
|
5
|
+
* the i18n instance reports a language change. Returns a cleanup
|
|
6
|
+
* function that detaches the listener.
|
|
7
|
+
*
|
|
8
|
+
* i18n is modeled as a Strategy via the `I18nInstance` interface; any
|
|
9
|
+
* adapter exposing `t`, `on`, and `language` works (i18next, react-intl,
|
|
10
|
+
* etc.) without a hard dependency.
|
|
11
|
+
*/
|
|
12
|
+
import type { SEOConfig } from "../types/index.js";
|
|
1
13
|
interface I18nInstance {
|
|
2
14
|
language?: string;
|
|
3
15
|
t: (key: string) => string;
|
|
4
16
|
on: (event: string, callback: () => void) => void;
|
|
5
17
|
off?: (event: string, callback: () => void) => void;
|
|
6
18
|
}
|
|
7
|
-
interface
|
|
19
|
+
interface InitConfig {
|
|
8
20
|
i18n?: I18nInstance;
|
|
9
21
|
titleKey?: string;
|
|
10
22
|
descriptionKey?: string;
|
|
11
23
|
defaultImage?: string;
|
|
12
24
|
canonicalPrefix?: string;
|
|
13
25
|
twitterHandle?: string;
|
|
26
|
+
defaults?: Partial<SEOConfig>;
|
|
14
27
|
}
|
|
15
|
-
export declare const initSEO: (config:
|
|
28
|
+
export declare const initSEO: (config: InitConfig) => (() => void);
|
|
16
29
|
export {};
|
|
@@ -1,12 +1,33 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* `initSEO(config)` — application-level SEO bootstrap.
|
|
3
|
+
*
|
|
4
|
+
* Sets default SEO options on first call and re-applies them whenever
|
|
5
|
+
* the i18n instance reports a language change. Returns a cleanup
|
|
6
|
+
* function that detaches the listener.
|
|
7
|
+
*
|
|
8
|
+
* i18n is modeled as a Strategy via the `I18nInstance` interface; any
|
|
9
|
+
* adapter exposing `t`, `on`, and `language` works (i18next, react-intl,
|
|
10
|
+
* etc.) without a hard dependency.
|
|
11
|
+
*/
|
|
12
|
+
import { updateSEO } from "../utils/dom/index.js";
|
|
2
13
|
export const initSEO = (config) => {
|
|
3
14
|
if (typeof window === "undefined")
|
|
4
|
-
return () =>
|
|
5
|
-
const
|
|
6
|
-
const lang = config.i18n?.language
|
|
7
|
-
const title = config.i18n
|
|
8
|
-
|
|
9
|
-
|
|
15
|
+
return () => undefined;
|
|
16
|
+
const resolve = () => {
|
|
17
|
+
const lang = config.i18n?.language?.split("-")[0] ?? "en";
|
|
18
|
+
const title = config.i18n
|
|
19
|
+
? config.i18n.t(config.titleKey ?? "app.title")
|
|
20
|
+
: document.title;
|
|
21
|
+
const description = config.i18n
|
|
22
|
+
? config.i18n.t(config.descriptionKey ?? "app.description")
|
|
23
|
+
: "";
|
|
24
|
+
const canonical = config.canonicalPrefix
|
|
25
|
+
? `${config.canonicalPrefix}/${lang}`
|
|
26
|
+
: undefined;
|
|
27
|
+
return { title, description, lang, canonical };
|
|
28
|
+
};
|
|
29
|
+
const apply = () => {
|
|
30
|
+
const { title, description, lang, canonical } = resolve();
|
|
10
31
|
updateSEO({
|
|
11
32
|
title,
|
|
12
33
|
description,
|
|
@@ -22,19 +43,18 @@ export const initSEO = (config) => {
|
|
|
22
43
|
twitter: {
|
|
23
44
|
cardType: "summary_large_image",
|
|
24
45
|
site: config.twitterHandle,
|
|
25
|
-
}
|
|
46
|
+
},
|
|
26
47
|
});
|
|
27
48
|
};
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
// Listen for language changes if i18next is provided
|
|
49
|
+
apply();
|
|
50
|
+
let cleanup;
|
|
31
51
|
if (config.i18n && typeof config.i18n.on === "function") {
|
|
32
|
-
config.i18n.on("languageChanged",
|
|
52
|
+
config.i18n.on("languageChanged", apply);
|
|
53
|
+
cleanup = () => {
|
|
54
|
+
if (typeof config.i18n?.off === "function") {
|
|
55
|
+
config.i18n.off("languageChanged", apply);
|
|
56
|
+
}
|
|
57
|
+
};
|
|
33
58
|
}
|
|
34
|
-
|
|
35
|
-
return () => {
|
|
36
|
-
if (config.i18n && typeof config.i18n.off === "function") {
|
|
37
|
-
config.i18n.off("languageChanged", update);
|
|
38
|
-
}
|
|
39
|
-
};
|
|
59
|
+
return cleanup ?? (() => undefined);
|
|
40
60
|
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SEO tag strategies (Strategy pattern).
|
|
3
|
+
*
|
|
4
|
+
* One strategy per concern. The same strategy is consumed by:
|
|
5
|
+
* - `dom/dom-mutator.ts` – applies the change to the live DOM
|
|
6
|
+
* - `ssr/renderer.ts` – serializes the change to an HTML string
|
|
7
|
+
*
|
|
8
|
+
* Replaces the historical god-functions in `dom.ts` and `renderer.ts` and
|
|
9
|
+
* fixes the divergence where SSR silently dropped OG image width/height.
|
|
10
|
+
*/
|
|
11
|
+
import type { Strategy } from "./seo-strategy.js";
|
|
12
|
+
export declare const titleStrategy: Strategy;
|
|
13
|
+
export declare const langStrategy: Strategy;
|
|
14
|
+
export declare const descriptionStrategy: Strategy;
|
|
15
|
+
export declare const canonicalStrategy: Strategy;
|
|
16
|
+
export declare const robotsStrategy: Strategy;
|
|
17
|
+
export declare const viewportStrategy: Strategy;
|
|
18
|
+
export declare const themeColorStrategy: Strategy;
|
|
19
|
+
export declare const faviconStrategy: Strategy;
|
|
20
|
+
export declare const hreflangStrategy: Strategy;
|
|
21
|
+
export declare const preloadStrategy: Strategy;
|
|
22
|
+
export declare const preconnectStrategy: Strategy;
|
|
23
|
+
export declare const dnsPrefetchStrategy: Strategy;
|
|
24
|
+
export declare const customMetaStrategy: Strategy;
|
|
25
|
+
export declare const customLinkStrategy: Strategy;
|
|
26
|
+
export declare const openGraphStrategy: Strategy;
|
|
27
|
+
export declare const twitterStrategy: Strategy;
|
|
28
|
+
export declare const jsonLdStrategy: Strategy;
|
|
29
|
+
export declare const SEO_STRATEGIES: readonly Strategy[];
|