@unhead/react 2.0.2 → 2.0.4
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/index.mjs +35 -28
- package/package.json +5 -5
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import React, { useEffect, useContext, useState, useRef } from 'react';
|
|
1
|
+
import React, { useEffect, useContext, useState, useMemo, useCallback, useRef } from 'react';
|
|
2
2
|
import { ValidHeadTags, TagsWithInnerContent, HasElementTags } from 'unhead/utils';
|
|
3
3
|
import { useScript as useScript$1, useHead as useHead$1, useHeadSafe as useHeadSafe$1, useSeoMeta as useSeoMeta$1 } from 'unhead';
|
|
4
4
|
import { U as UnheadContext } from './shared/react.DF9T1fqs.mjs';
|
|
@@ -97,44 +97,51 @@ function useScript(_input, _options) {
|
|
|
97
97
|
}
|
|
98
98
|
|
|
99
99
|
const Head = ({ children, titleTemplate }) => {
|
|
100
|
-
const headRef = useRef(null);
|
|
101
100
|
const head = useUnhead();
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
if (headRef.current?.dispose()) {
|
|
105
|
-
headRef.current.dispose();
|
|
106
|
-
}
|
|
107
|
-
headRef.current = null;
|
|
108
|
-
};
|
|
109
|
-
}, []);
|
|
110
|
-
useEffect(() => {
|
|
101
|
+
const processedElements = useMemo(() => React.Children.toArray(children).filter(React.isValidElement), [children]);
|
|
102
|
+
const getHeadChanges = useCallback(() => {
|
|
111
103
|
const input = {
|
|
112
104
|
titleTemplate
|
|
113
105
|
};
|
|
114
|
-
const
|
|
115
|
-
|
|
116
|
-
const { type, props } =
|
|
117
|
-
|
|
118
|
-
|
|
106
|
+
for (const element of processedElements) {
|
|
107
|
+
const reactElement = element;
|
|
108
|
+
const { type, props } = reactElement;
|
|
109
|
+
const tagName = String(type);
|
|
110
|
+
if (!ValidHeadTags.has(tagName)) {
|
|
111
|
+
continue;
|
|
119
112
|
}
|
|
120
113
|
const data = { ...typeof props === "object" ? props : {} };
|
|
121
|
-
if (TagsWithInnerContent.has(
|
|
122
|
-
|
|
114
|
+
if (TagsWithInnerContent.has(tagName) && data.children) {
|
|
115
|
+
const contentKey = tagName === "script" ? "innerHTML" : "textContent";
|
|
116
|
+
data[contentKey] = Array.isArray(data.children) ? data.children.map(String).join("") : String(data.children);
|
|
123
117
|
}
|
|
124
118
|
delete data.children;
|
|
125
|
-
if (HasElementTags.has(
|
|
126
|
-
|
|
127
|
-
input[
|
|
119
|
+
if (HasElementTags.has(tagName)) {
|
|
120
|
+
const key = tagName;
|
|
121
|
+
if (!Array.isArray(input[key])) {
|
|
122
|
+
input[key] = [];
|
|
123
|
+
}
|
|
124
|
+
input[key].push(data);
|
|
128
125
|
} else {
|
|
129
|
-
input[
|
|
126
|
+
input[tagName] = data;
|
|
130
127
|
}
|
|
131
|
-
});
|
|
132
|
-
if (!headRef.current) {
|
|
133
|
-
headRef.current = head.push(input);
|
|
134
|
-
} else {
|
|
135
|
-
headRef.current.patch(input);
|
|
136
128
|
}
|
|
137
|
-
|
|
129
|
+
return input;
|
|
130
|
+
}, [processedElements, titleTemplate]);
|
|
131
|
+
const headRef = useRef(
|
|
132
|
+
head.push(getHeadChanges())
|
|
133
|
+
);
|
|
134
|
+
useEffect(() => {
|
|
135
|
+
return () => {
|
|
136
|
+
if (headRef.current?.dispose) {
|
|
137
|
+
headRef.current.dispose();
|
|
138
|
+
}
|
|
139
|
+
headRef.current = null;
|
|
140
|
+
};
|
|
141
|
+
}, []);
|
|
142
|
+
useEffect(() => {
|
|
143
|
+
headRef.current?.patch(getHeadChanges());
|
|
144
|
+
}, [getHeadChanges]);
|
|
138
145
|
return null;
|
|
139
146
|
};
|
|
140
147
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unhead/react",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "2.0.
|
|
4
|
+
"version": "2.0.4",
|
|
5
5
|
"author": "Harlan Wilton <harlan@harlanzw.com>",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"funding": "https://github.com/sponsors/harlan-zw",
|
|
@@ -61,7 +61,7 @@
|
|
|
61
61
|
"dist"
|
|
62
62
|
],
|
|
63
63
|
"peerDependencies": {
|
|
64
|
-
"react": ">=
|
|
64
|
+
"react": ">=18"
|
|
65
65
|
},
|
|
66
66
|
"build": {
|
|
67
67
|
"external": [
|
|
@@ -69,12 +69,12 @@
|
|
|
69
69
|
]
|
|
70
70
|
},
|
|
71
71
|
"dependencies": {
|
|
72
|
-
"unhead": "2.0.
|
|
72
|
+
"unhead": "2.0.4"
|
|
73
73
|
},
|
|
74
74
|
"devDependencies": {
|
|
75
75
|
"@testing-library/react": "^16.2.0",
|
|
76
|
-
"react": "^19.
|
|
77
|
-
"react-dom": "^19.
|
|
76
|
+
"react": "^19.1.0",
|
|
77
|
+
"react-dom": "^19.1.0"
|
|
78
78
|
},
|
|
79
79
|
"scripts": {
|
|
80
80
|
"build": "unbuild",
|