@unhead/react 3.0.0-beta.6 → 3.0.0-beta.8

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 CHANGED
@@ -22,20 +22,41 @@ function useUnhead() {
22
22
  function withSideEffects(input, options, fn) {
23
23
  const unhead = options.head || useUnhead();
24
24
  const entryRef = useRef(null);
25
- if (!entryRef.current) {
25
+ const inputRef = useRef(input);
26
+ inputRef.current = input;
27
+ if (unhead.ssr && !entryRef.current) {
26
28
  entryRef.current = fn(unhead, input, options);
27
29
  }
28
- const entry = entryRef.current;
29
- useEffect(() => {
30
- entry?.patch(input);
31
- }, [input, entry]);
32
30
  useEffect(() => {
31
+ const entry = fn(unhead, inputRef.current, options);
32
+ entryRef.current = entry;
33
33
  return () => {
34
- entry?.dispose();
34
+ entry.dispose();
35
35
  entryRef.current = null;
36
36
  };
37
- }, [entry]);
38
- return entry;
37
+ }, [unhead]);
38
+ useEffect(() => {
39
+ entryRef.current?.patch(input);
40
+ }, [input]);
41
+ if (unhead.ssr) {
42
+ return entryRef.current;
43
+ }
44
+ const proxyRef = useRef(null);
45
+ if (!proxyRef.current) {
46
+ proxyRef.current = {
47
+ patch: (newInput) => {
48
+ entryRef.current?.patch(newInput);
49
+ },
50
+ dispose: () => {
51
+ entryRef.current?.dispose();
52
+ entryRef.current = null;
53
+ },
54
+ _poll: (rm) => {
55
+ entryRef.current?._poll(rm);
56
+ }
57
+ };
58
+ }
59
+ return proxyRef.current;
39
60
  }
40
61
  function useHead(input = {}, options = {}) {
41
62
  return withSideEffects(input, options, useHead$1);
@@ -1,43 +1,64 @@
1
1
  import MagicString from 'magic-string';
2
- import { findStaticImports } from 'mlly';
3
- import { parseSync, Visitor } from 'oxc-parser';
2
+ import { parseAndWalk } from 'oxc-walker';
4
3
  import { createStreamingPlugin } from 'unhead/stream/vite';
5
4
 
6
5
  function transform(code, id, isSSR, s) {
7
6
  const lang = id.endsWith(".tsx") ? "tsx" : id.endsWith(".jsx") ? "jsx" : "tsx";
8
- const result = parseSync(id, code, { lang });
9
- if (result.errors.length > 0)
10
- return false;
11
7
  const returns = [];
12
- const visitor = new Visitor({
13
- FunctionDeclaration: (node) => processFunction(node),
14
- FunctionExpression: (node) => processFunction(node),
15
- ArrowFunctionExpression: (node) => processFunction(node)
16
- });
17
- function processFunction(node) {
18
- if (!node.body)
19
- return;
20
- const bodyCode = code.slice(node.body.start, node.body.end);
21
- if (!bodyCode.includes("useHead") && !bodyCode.includes("useSeoMeta") && !bodyCode.includes("useHeadSafe"))
22
- return;
23
- if (node.body.type === "JSXElement" || node.body.type === "JSXFragment") {
24
- returns.push({ jsxStart: node.body.start, jsxEnd: node.body.end });
25
- return;
26
- }
27
- const innerVisitor = new Visitor({
28
- ReturnStatement(innerNode) {
29
- if (!innerNode.argument)
8
+ let currentFnHasHead = false;
9
+ const fnStack = [];
10
+ const importPath = isSSR ? "@unhead/react/stream/server" : "@unhead/react/stream/client";
11
+ let existingImport = null;
12
+ let lastImportEnd = -1;
13
+ const result = parseAndWalk(code, id, {
14
+ parseOptions: { lang },
15
+ enter(node) {
16
+ if (node.type === "ImportDeclaration") {
17
+ if (node.source.value === importPath) {
18
+ existingImport = {
19
+ start: node.start,
20
+ end: node.end,
21
+ specifiers: node.specifiers?.map((spec) => spec.local?.name).filter(Boolean) || []
22
+ };
23
+ }
24
+ if (node.end > lastImportEnd)
25
+ lastImportEnd = node.end;
26
+ this.skip();
27
+ return;
28
+ }
29
+ const isFn = node.type === "FunctionDeclaration" || node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression";
30
+ if (isFn) {
31
+ fnStack.push(currentFnHasHead);
32
+ if (!node.body) {
33
+ currentFnHasHead = false;
34
+ return;
35
+ }
36
+ const bodyCode = code.slice(node.body.start, node.body.end);
37
+ currentFnHasHead = bodyCode.includes("useHead") || bodyCode.includes("useSeoMeta") || bodyCode.includes("useHeadSafe");
38
+ if (currentFnHasHead && (node.body.type === "JSXElement" || node.body.type === "JSXFragment")) {
39
+ returns.push({ jsxStart: node.body.start, jsxEnd: node.body.end });
40
+ }
41
+ return;
42
+ }
43
+ if (currentFnHasHead && node.type === "ReturnStatement") {
44
+ if (!node.argument)
30
45
  return;
31
- let arg = innerNode.argument;
46
+ let arg = node.argument;
32
47
  if (arg.type === "ParenthesizedExpression" && arg.expression)
33
48
  arg = arg.expression;
34
49
  if (arg.type === "JSXElement" || arg.type === "JSXFragment")
35
50
  returns.push({ jsxStart: arg.start, jsxEnd: arg.end });
36
51
  }
37
- });
38
- innerVisitor.visit(node.body);
39
- }
40
- visitor.visit(result.program);
52
+ },
53
+ leave(node) {
54
+ const isFn = node.type === "FunctionDeclaration" || node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression";
55
+ if (isFn) {
56
+ currentFnHasHead = fnStack.pop();
57
+ }
58
+ }
59
+ });
60
+ if (result.errors.length > 0)
61
+ return false;
41
62
  if (returns.length === 0)
42
63
  return false;
43
64
  returns.sort((a, b) => b.jsxStart - a.jsxStart);
@@ -45,22 +66,17 @@ function transform(code, id, isSSR, s) {
45
66
  const jsxCode = code.slice(ret.jsxStart, ret.jsxEnd);
46
67
  s.overwrite(ret.jsxStart, ret.jsxEnd, `<><HeadStream />${jsxCode}</>`);
47
68
  }
48
- const importPath = isSSR ? "@unhead/react/stream/server" : "@unhead/react/stream/client";
49
- const imports = findStaticImports(code);
50
- const existing = imports.find((i) => i.specifier === importPath);
51
- if (existing) {
52
- if (!existing.imports?.includes("HeadStream")) {
53
- const inner = existing.imports?.replace(/^\{\s*|\s*\}\s*$/g, "").trim() || "";
54
- s.overwrite(existing.start, existing.end, `import { ${inner ? `${inner}, ` : ""}HeadStream } from '${importPath}'
55
- `);
69
+ const foundImport = existingImport;
70
+ if (foundImport) {
71
+ if (!foundImport.specifiers.includes("HeadStream")) {
72
+ const inner = foundImport.specifiers.join(", ");
73
+ s.overwrite(foundImport.start, foundImport.end, `import { ${inner ? `${inner}, ` : ""}HeadStream } from '${importPath}'`);
56
74
  }
75
+ } else if (lastImportEnd > -1) {
76
+ s.appendLeft(lastImportEnd, `
77
+ import { HeadStream } from '${importPath}'`);
57
78
  } else {
58
- const last = imports[imports.length - 1];
59
- if (last)
60
- s.appendLeft(last.end, `import { HeadStream } from '${importPath}'
61
- `);
62
- else
63
- s.prepend(`import { HeadStream } from '${importPath}'
79
+ s.prepend(`import { HeadStream } from '${importPath}'
64
80
  `);
65
81
  }
66
82
  return true;
@@ -68,10 +84,9 @@ function transform(code, id, isSSR, s) {
68
84
  function unheadReactPlugin(options) {
69
85
  return createStreamingPlugin({
70
86
  framework: "@unhead/react",
87
+ filter: /\.[jt]sx$/,
71
88
  mode: options?.mode,
72
89
  transform(code, id, opts) {
73
- if (!/\.[jt]sx$/.test(id))
74
- return null;
75
90
  const s = new MagicString(code);
76
91
  if (!transform(code, id, opts?.ssr ?? false, s))
77
92
  return null;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@unhead/react",
3
3
  "type": "module",
4
- "version": "3.0.0-beta.6",
4
+ "version": "3.0.0-beta.8",
5
5
  "description": "Full-stack <head> manager built for React.",
6
6
  "author": "Harlan Wilton <harlan@harlanzw.com>",
7
7
  "license": "MIT",
@@ -98,9 +98,9 @@
98
98
  },
99
99
  "dependencies": {
100
100
  "magic-string": "^0.30.21",
101
- "mlly": "^1.8.0",
102
101
  "oxc-parser": "^0.106.0",
103
- "unhead": "3.0.0-beta.6"
102
+ "oxc-walker": "^0.7.0",
103
+ "unhead": "3.0.0-beta.8"
104
104
  },
105
105
  "devDependencies": {
106
106
  "@testing-library/react": "^16.3.1",
@@ -108,7 +108,7 @@
108
108
  "@types/react-dom": "^19.2.3",
109
109
  "react": "^19.2.3",
110
110
  "react-dom": "^19.2.3",
111
- "vite": "7.2.2"
111
+ "vite": "^7.2.2"
112
112
  },
113
113
  "scripts": {
114
114
  "build": "unbuild",