@unhead/react 2.0.2 → 2.0.3

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.
Files changed (2) hide show
  1. package/dist/index.mjs +35 -28
  2. 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
- useEffect(() => {
103
- return () => {
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 elements = React.Children.toArray(children).filter(React.isValidElement);
115
- elements.forEach((element) => {
116
- const { type, props } = element;
117
- if (!ValidHeadTags.has(type)) {
118
- return;
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(type) && data.children) {
122
- data[type === "script" ? "innerHTML" : "textContent"] = Array.isArray(data.children) ? data.children.map(String).join("") : data.children;
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(type)) {
126
- input[type] = input[type] || [];
127
- input[type].push(data);
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[type] = data;
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
- }, [children, titleTemplate]);
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.2",
4
+ "version": "2.0.3",
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": ">=19"
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.2"
72
+ "unhead": "2.0.3"
73
73
  },
74
74
  "devDependencies": {
75
75
  "@testing-library/react": "^16.2.0",
76
- "react": "^19.0.0",
77
- "react-dom": "^19.0.0"
76
+ "react": "^19.1.0",
77
+ "react-dom": "^19.1.0"
78
78
  },
79
79
  "scripts": {
80
80
  "build": "unbuild",