@umituz/web-seo 2.0.12 → 2.0.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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: React.FC<JsonLdProps>;
15
+ export declare const JsonLd: import("react").MemoExoticComponent<({ data, strategy, id }: JsonLdProps) => null>;
16
16
  export default JsonLd;
@@ -4,17 +4,61 @@
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 } from "react";
8
- export const JsonLd = ({ data, strategy = "afterInteractive", id }) => {
7
+ import { useEffect, useRef, useMemo, memo } from "react";
8
+ // Optimized hash generation with caching using cyrb53 algorithm
9
+ const hashCache = new WeakMap();
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)
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
+ };
47
+ const JsonLdComponent = ({ data, strategy = "afterInteractive", id }) => {
9
48
  const scriptRef = useRef(null);
49
+ // Generate stable IDs for data items
50
+ const dataIds = useMemo(() => {
51
+ const dataArray = Array.isArray(data) ? data : [data];
52
+ return dataArray.map((item) => id ? `${id}-${dataArray.indexOf(item)}` : generateDataId(item));
53
+ }, [data, id]);
10
54
  useEffect(() => {
11
55
  if (typeof document === "undefined")
12
56
  return;
13
57
  // Convert single object to array for uniform handling
14
58
  const dataArray = Array.isArray(data) ? data : [data];
15
- // Remove existing scripts with the same data to avoid duplicates
16
- dataArray.forEach((jsonData) => {
17
- const existingScript = document.querySelector(`script[type="application/ld+json"][data-seo-id="${id || JSON.stringify(jsonData)}"]`);
59
+ // Remove existing scripts with matching IDs
60
+ dataIds.forEach((dataId) => {
61
+ const existingScript = document.querySelector(`script[type="application/ld+json"][data-seo-id="${dataId}"]`);
18
62
  if (existingScript) {
19
63
  existingScript.remove();
20
64
  }
@@ -25,12 +69,7 @@ export const JsonLd = ({ data, strategy = "afterInteractive", id }) => {
25
69
  script.type = "application/ld+json";
26
70
  script.textContent = JSON.stringify(jsonData);
27
71
  // Add identifier for duplicate detection and debugging
28
- if (id) {
29
- script.setAttribute("data-seo-id", `${id}-${index}`);
30
- }
31
- else {
32
- script.setAttribute("data-seo-id", JSON.stringify(jsonData));
33
- }
72
+ script.setAttribute("data-seo-id", dataIds[index]);
34
73
  script.setAttribute("data-seo-strategy", strategy);
35
74
  document.head.appendChild(script);
36
75
  if (index === 0) {
@@ -39,14 +78,20 @@ export const JsonLd = ({ data, strategy = "afterInteractive", id }) => {
39
78
  });
40
79
  // Cleanup function
41
80
  return () => {
42
- dataArray.forEach((jsonData) => {
43
- const script = document.querySelector(`script[type="application/ld+json"][data-seo-id="${id || JSON.stringify(jsonData)}"]`);
81
+ dataIds.forEach((dataId) => {
82
+ const script = document.querySelector(`script[type="application/ld+json"][data-seo-id="${dataId}"]`);
44
83
  if (script) {
45
84
  script.remove();
46
85
  }
47
86
  });
48
87
  };
49
- }, [data, strategy, id]);
88
+ }, [data, strategy, dataIds]);
50
89
  return null;
51
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
+ });
52
97
  export default JsonLd;
@@ -1,3 +1,2 @@
1
- import React from "react";
2
1
  import type { SEOptions } from "../types/index.js";
3
- export declare const SEO: React.FC<SEOptions>;
2
+ export declare const SEO: import("react").MemoExoticComponent<(props: SEOptions) => null>;
@@ -1,48 +1,44 @@
1
- import { useEffect, useMemo } from "react";
1
+ import { useEffect, useRef, memo } from "react";
2
2
  import { updateSEO } from "../utils/dom.js";
3
- export const SEO = (props) => {
4
- // Memoize dependencies for better performance
5
- const dependencies = useMemo(() => [
6
- props.title,
7
- props.description,
8
- props.lang,
9
- props.canonical,
10
- props.themeColor,
11
- props.favicon,
12
- JSON.stringify(props.meta),
13
- JSON.stringify(props.link),
14
- JSON.stringify(props.openGraph),
15
- JSON.stringify(props.twitter),
16
- JSON.stringify(props.jsonLd),
17
- JSON.stringify(props.hreflang),
18
- JSON.stringify(props.preload),
19
- JSON.stringify(props.preconnect),
20
- JSON.stringify(props.dnsPrefetch),
21
- JSON.stringify(props.robots),
22
- JSON.stringify(props.breadcrumbs),
23
- props.viewport,
24
- ], [
25
- props.title,
26
- props.description,
27
- props.lang,
28
- props.canonical,
29
- props.themeColor,
30
- props.favicon,
31
- props.meta,
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
- updateSEO(props);
46
- }, dependencies);
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
+ });
@@ -2,6 +2,7 @@ interface I18nInstance {
2
2
  language?: string;
3
3
  t: (key: string) => string;
4
4
  on: (event: string, callback: () => void) => void;
5
+ off?: (event: string, callback: () => void) => void;
5
6
  }
6
7
  interface SEOConfig {
7
8
  i18n?: I18nInstance;
@@ -11,5 +12,5 @@ interface SEOConfig {
11
12
  canonicalPrefix?: string;
12
13
  twitterHandle?: string;
13
14
  }
14
- export declare const initSEO: (config: SEOConfig) => void;
15
+ export declare const initSEO: (config: SEOConfig) => (() => void);
15
16
  export {};
@@ -1,9 +1,9 @@
1
1
  import { updateSEO } from "../utils/dom.js";
2
2
  export const initSEO = (config) => {
3
3
  if (typeof window === "undefined")
4
- return;
4
+ return () => { };
5
5
  const update = () => {
6
- const lang = config.i18n?.language ? config.i18n.language.split("-")[0] || "en" : "en";
6
+ const lang = config.i18n?.language ? config.i18n.language.split("-")[0] : "en";
7
7
  const title = config.i18n ? config.i18n.t(config.titleKey || "app.title") : document.title;
8
8
  const description = config.i18n ? config.i18n.t(config.descriptionKey || "app.description") : "";
9
9
  const canonical = config.canonicalPrefix ? `${config.canonicalPrefix}/${lang}` : undefined;
@@ -31,4 +31,10 @@ export const initSEO = (config) => {
31
31
  if (config.i18n && typeof config.i18n.on === "function") {
32
32
  config.i18n.on("languageChanged", update);
33
33
  }
34
+ // Return cleanup function
35
+ return () => {
36
+ if (config.i18n && typeof config.i18n.off === "function") {
37
+ config.i18n.off("languageChanged", update);
38
+ }
39
+ };
34
40
  };
@@ -4,16 +4,49 @@
4
4
  * React hook for managing JSON-LD structured data.
5
5
  * Provides a programmatic way to add/update/remove structured data.
6
6
  */
7
- import { useEffect, useCallback } from "react";
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
+ };
8
33
  export const useJsonLd = (data, options) => {
9
34
  const { strategy = "afterInteractive", id } = options || {};
35
+ // Generate stable IDs for data items
36
+ const dataIds = useMemo(() => {
37
+ const dataArray = Array.isArray(data) ? data : [data];
38
+ return dataArray.map((item) => id ? `${id}-${dataArray.indexOf(item)}` : generateDataId(item));
39
+ }, [data, id]);
10
40
  const injectJsonLd = useCallback((jsonData) => {
11
41
  if (typeof document === "undefined")
12
42
  return;
13
43
  const dataArray = Array.isArray(jsonData) ? jsonData : [jsonData];
44
+ const ids = Array.isArray(jsonData) && !id
45
+ ? dataArray.map(generateDataId)
46
+ : dataIds;
14
47
  dataArray.forEach((item, index) => {
15
48
  // Remove existing script if present
16
- const existingScript = document.querySelector(`script[type="application/ld+json"][data-seo-id="${id || JSON.stringify(item)}"]`);
49
+ const existingScript = document.querySelector(`script[type="application/ld+json"][data-seo-id="${ids[index]}"]`);
17
50
  if (existingScript) {
18
51
  existingScript.remove();
19
52
  }
@@ -21,22 +54,21 @@ export const useJsonLd = (data, options) => {
21
54
  const script = document.createElement("script");
22
55
  script.type = "application/ld+json";
23
56
  script.textContent = JSON.stringify(item);
24
- if (id) {
25
- script.setAttribute("data-seo-id", `${id}-${index}`);
26
- }
27
- else {
28
- script.setAttribute("data-seo-id", JSON.stringify(item));
29
- }
57
+ script.setAttribute("data-seo-id", ids[index]);
30
58
  script.setAttribute("data-seo-strategy", strategy);
31
59
  document.head.appendChild(script);
32
60
  });
33
- }, [id, strategy]);
61
+ }, [id, strategy, dataIds]);
34
62
  const removeJsonLd = useCallback(() => {
35
63
  if (typeof document === "undefined")
36
64
  return;
37
- const scripts = document.querySelectorAll(`script[type="application/ld+json"][data-seo-id*="${id || ''}"]`);
38
- scripts.forEach((script) => script.remove());
39
- }, [id]);
65
+ dataIds.forEach((dataId) => {
66
+ const script = document.querySelector(`script[type="application/ld+json"][data-seo-id="${dataId}"]`);
67
+ if (script) {
68
+ script.remove();
69
+ }
70
+ });
71
+ }, [dataIds]);
40
72
  useEffect(() => {
41
73
  if (data) {
42
74
  injectJsonLd(data);
@@ -1,47 +1,40 @@
1
- import { useEffect, useMemo } from "react";
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
- // Memoize dependencies for better performance
5
- const dependencies = useMemo(() => [
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
- updateSEO(options);
46
- }, dependencies);
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
  */