@unhead/dom 0.3.1 → 0.4.2

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.cjs CHANGED
@@ -2,15 +2,6 @@
2
2
 
3
3
  const TagsWithInnerContent = ["script", "style", "noscript"];
4
4
 
5
- const createElement = (tag, document) => {
6
- const $el = document.createElement(tag.tag);
7
- for (const k in tag.props)
8
- $el.setAttribute(k, String(tag.props[k]));
9
- if (tag.children)
10
- $el.innerHTML = tag.children;
11
- return $el;
12
- };
13
-
14
5
  async function renderDOMHead(head, options = {}) {
15
6
  const dom = options.document || window.document;
16
7
  const tags = await head.resolveTags();
@@ -18,65 +9,64 @@ async function renderDOMHead(head, options = {}) {
18
9
  await head.hooks.callHook("dom:beforeRender", ctx);
19
10
  if (!ctx.shouldRender)
20
11
  return;
21
- const queuedSideEffects = head._popSideEffectQueue();
12
+ Object.values(head._popSideEffectQueue()).forEach((fn) => fn());
13
+ const staleSideEffects = {};
22
14
  head.headEntries().map((entry) => entry._sde).forEach((sde) => {
23
15
  Object.entries(sde).forEach(([key, fn]) => {
24
- queuedSideEffects[key] = fn;
16
+ staleSideEffects[key] = fn;
25
17
  });
26
18
  });
27
19
  const renderTag = (tag, entry) => {
28
20
  if (tag.tag === "title" && tag.children) {
29
21
  dom.title = tag.children;
30
- return;
22
+ return dom.head.querySelector("title");
31
23
  }
32
24
  const markSideEffect = (key, fn) => {
33
- key = `${tag._s || tag._p}:${key}`;
25
+ key = `${tag._p}:${key}`;
34
26
  entry._sde[key] = fn;
35
- delete queuedSideEffects[key];
27
+ delete staleSideEffects[key];
36
28
  };
37
- const setAttrs = ($el) => {
29
+ const setAttrs = ($el, sideEffects = true) => {
38
30
  Object.entries(tag.props).forEach(([k, value]) => {
39
31
  value = String(value);
40
32
  const attrSdeKey = `attr:${k}`;
41
33
  if (k === "class") {
42
34
  for (const c of value.split(" ")) {
43
35
  const classSdeKey = `${attrSdeKey}:${c}`;
44
- markSideEffect(classSdeKey, () => $el.classList.remove(c));
36
+ sideEffects && markSideEffect(classSdeKey, () => $el.classList.remove(c));
45
37
  if (!$el.classList.contains(c))
46
38
  $el.classList.add(c);
47
39
  }
48
40
  return;
49
41
  }
50
- if (!k.startsWith("data-h-"))
42
+ if (sideEffects && !k.startsWith("data-h-"))
51
43
  markSideEffect(attrSdeKey, () => $el.removeAttribute(k));
52
44
  if ($el.getAttribute(k) !== value)
53
45
  $el.setAttribute(k, value);
54
46
  });
47
+ if (TagsWithInnerContent.includes(tag.tag))
48
+ $el.innerHTML = tag.children || "";
55
49
  };
56
50
  if (tag.tag === "htmlAttrs" || tag.tag === "bodyAttrs") {
57
- setAttrs(dom[tag.tag === "htmlAttrs" ? "documentElement" : "body"]);
58
- return;
51
+ const $el = dom[tag.tag === "htmlAttrs" ? "documentElement" : "body"];
52
+ setAttrs($el);
53
+ return $el;
59
54
  }
60
- let $newEl = createElement(tag, dom);
55
+ let $newEl = dom.createElement(tag.tag);
56
+ setAttrs($newEl, false);
61
57
  let $previousEl;
62
58
  for (const $el of dom[tag.tagPosition?.startsWith("body") ? "body" : "head"].children) {
63
- if ($el.hasAttribute(`${tag._s}`) || $el.isEqualNode($newEl)) {
59
+ if (tag._s && $el.hasAttribute(`${tag._s}`) || $el.isEqualNode($newEl)) {
64
60
  $previousEl = $el;
65
61
  break;
66
62
  }
67
63
  }
68
64
  if ($previousEl) {
69
- markSideEffect("el", () => $previousEl?.remove());
70
- if (Object.keys(tag.props).length === 0) {
71
- $previousEl.remove();
72
- return;
73
- }
74
- if ($newEl.isEqualNode($previousEl))
75
- return;
76
- setAttrs($previousEl);
77
- if (TagsWithInnerContent.includes(tag.tag))
78
- $previousEl.innerHTML = tag.children || "";
79
- return;
65
+ markSideEffect("el", () => {
66
+ $previousEl?.remove();
67
+ });
68
+ setAttrs($previousEl, false);
69
+ return $previousEl;
80
70
  }
81
71
  switch (tag.tagPosition) {
82
72
  case "bodyClose":
@@ -91,15 +81,18 @@ async function renderDOMHead(head, options = {}) {
91
81
  break;
92
82
  }
93
83
  markSideEffect("el", () => $newEl?.remove());
84
+ return $newEl;
94
85
  };
95
86
  for (const tag of ctx.tags) {
96
- const renderCtx = { shouldRender: true, tag };
97
- await head.hooks.callHook("dom:renderTag", renderCtx);
87
+ const entry = head.headEntries().find((e) => e._i === Number(tag._e));
88
+ const renderCtx = { $el: null, shouldRender: true, tag, entry, queuedSideEffects: staleSideEffects };
89
+ await head.hooks.callHook("dom:beforeRenderTag", renderCtx);
98
90
  if (!renderCtx.shouldRender)
99
91
  continue;
100
- renderTag(tag, head.headEntries().find((e) => e._i === Number(tag._e)));
92
+ renderCtx.$el = renderTag(renderCtx.tag, renderCtx.entry);
93
+ await head.hooks.callHook("dom:renderTag", renderCtx);
101
94
  }
102
- Object.values(queuedSideEffects).forEach((fn) => fn());
95
+ Object.values(staleSideEffects).forEach((fn) => fn());
103
96
  }
104
97
  exports.domUpdatePromise = null;
105
98
  async function debouncedRenderDOMHead(head, options = {}) {
package/dist/index.mjs CHANGED
@@ -1,14 +1,5 @@
1
1
  const TagsWithInnerContent = ["script", "style", "noscript"];
2
2
 
3
- const createElement = (tag, document) => {
4
- const $el = document.createElement(tag.tag);
5
- for (const k in tag.props)
6
- $el.setAttribute(k, String(tag.props[k]));
7
- if (tag.children)
8
- $el.innerHTML = tag.children;
9
- return $el;
10
- };
11
-
12
3
  async function renderDOMHead(head, options = {}) {
13
4
  const dom = options.document || window.document;
14
5
  const tags = await head.resolveTags();
@@ -16,65 +7,64 @@ async function renderDOMHead(head, options = {}) {
16
7
  await head.hooks.callHook("dom:beforeRender", ctx);
17
8
  if (!ctx.shouldRender)
18
9
  return;
19
- const queuedSideEffects = head._popSideEffectQueue();
10
+ Object.values(head._popSideEffectQueue()).forEach((fn) => fn());
11
+ const staleSideEffects = {};
20
12
  head.headEntries().map((entry) => entry._sde).forEach((sde) => {
21
13
  Object.entries(sde).forEach(([key, fn]) => {
22
- queuedSideEffects[key] = fn;
14
+ staleSideEffects[key] = fn;
23
15
  });
24
16
  });
25
17
  const renderTag = (tag, entry) => {
26
18
  if (tag.tag === "title" && tag.children) {
27
19
  dom.title = tag.children;
28
- return;
20
+ return dom.head.querySelector("title");
29
21
  }
30
22
  const markSideEffect = (key, fn) => {
31
- key = `${tag._s || tag._p}:${key}`;
23
+ key = `${tag._p}:${key}`;
32
24
  entry._sde[key] = fn;
33
- delete queuedSideEffects[key];
25
+ delete staleSideEffects[key];
34
26
  };
35
- const setAttrs = ($el) => {
27
+ const setAttrs = ($el, sideEffects = true) => {
36
28
  Object.entries(tag.props).forEach(([k, value]) => {
37
29
  value = String(value);
38
30
  const attrSdeKey = `attr:${k}`;
39
31
  if (k === "class") {
40
32
  for (const c of value.split(" ")) {
41
33
  const classSdeKey = `${attrSdeKey}:${c}`;
42
- markSideEffect(classSdeKey, () => $el.classList.remove(c));
34
+ sideEffects && markSideEffect(classSdeKey, () => $el.classList.remove(c));
43
35
  if (!$el.classList.contains(c))
44
36
  $el.classList.add(c);
45
37
  }
46
38
  return;
47
39
  }
48
- if (!k.startsWith("data-h-"))
40
+ if (sideEffects && !k.startsWith("data-h-"))
49
41
  markSideEffect(attrSdeKey, () => $el.removeAttribute(k));
50
42
  if ($el.getAttribute(k) !== value)
51
43
  $el.setAttribute(k, value);
52
44
  });
45
+ if (TagsWithInnerContent.includes(tag.tag))
46
+ $el.innerHTML = tag.children || "";
53
47
  };
54
48
  if (tag.tag === "htmlAttrs" || tag.tag === "bodyAttrs") {
55
- setAttrs(dom[tag.tag === "htmlAttrs" ? "documentElement" : "body"]);
56
- return;
49
+ const $el = dom[tag.tag === "htmlAttrs" ? "documentElement" : "body"];
50
+ setAttrs($el);
51
+ return $el;
57
52
  }
58
- let $newEl = createElement(tag, dom);
53
+ let $newEl = dom.createElement(tag.tag);
54
+ setAttrs($newEl, false);
59
55
  let $previousEl;
60
56
  for (const $el of dom[tag.tagPosition?.startsWith("body") ? "body" : "head"].children) {
61
- if ($el.hasAttribute(`${tag._s}`) || $el.isEqualNode($newEl)) {
57
+ if (tag._s && $el.hasAttribute(`${tag._s}`) || $el.isEqualNode($newEl)) {
62
58
  $previousEl = $el;
63
59
  break;
64
60
  }
65
61
  }
66
62
  if ($previousEl) {
67
- markSideEffect("el", () => $previousEl?.remove());
68
- if (Object.keys(tag.props).length === 0) {
69
- $previousEl.remove();
70
- return;
71
- }
72
- if ($newEl.isEqualNode($previousEl))
73
- return;
74
- setAttrs($previousEl);
75
- if (TagsWithInnerContent.includes(tag.tag))
76
- $previousEl.innerHTML = tag.children || "";
77
- return;
63
+ markSideEffect("el", () => {
64
+ $previousEl?.remove();
65
+ });
66
+ setAttrs($previousEl, false);
67
+ return $previousEl;
78
68
  }
79
69
  switch (tag.tagPosition) {
80
70
  case "bodyClose":
@@ -89,15 +79,18 @@ async function renderDOMHead(head, options = {}) {
89
79
  break;
90
80
  }
91
81
  markSideEffect("el", () => $newEl?.remove());
82
+ return $newEl;
92
83
  };
93
84
  for (const tag of ctx.tags) {
94
- const renderCtx = { shouldRender: true, tag };
95
- await head.hooks.callHook("dom:renderTag", renderCtx);
85
+ const entry = head.headEntries().find((e) => e._i === Number(tag._e));
86
+ const renderCtx = { $el: null, shouldRender: true, tag, entry, queuedSideEffects: staleSideEffects };
87
+ await head.hooks.callHook("dom:beforeRenderTag", renderCtx);
96
88
  if (!renderCtx.shouldRender)
97
89
  continue;
98
- renderTag(tag, head.headEntries().find((e) => e._i === Number(tag._e)));
90
+ renderCtx.$el = renderTag(renderCtx.tag, renderCtx.entry);
91
+ await head.hooks.callHook("dom:renderTag", renderCtx);
99
92
  }
100
- Object.values(queuedSideEffects).forEach((fn) => fn());
93
+ Object.values(staleSideEffects).forEach((fn) => fn());
101
94
  }
102
95
  let domUpdatePromise = null;
103
96
  async function debouncedRenderDOMHead(head, options = {}) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@unhead/dom",
3
3
  "type": "module",
4
- "version": "0.3.1",
4
+ "version": "0.4.2",
5
5
  "packageManager": "pnpm@7.14.0",
6
6
  "author": "Harlan Wilton <harlan@harlanzw.com>",
7
7
  "license": "MIT",
@@ -30,10 +30,10 @@
30
30
  "dist"
31
31
  ],
32
32
  "dependencies": {
33
- "@unhead/schema": "0.3.1"
33
+ "@unhead/schema": "0.4.2"
34
34
  },
35
35
  "devDependencies": {
36
- "zhead": "1.0.0-beta.10"
36
+ "zhead": "1.0.0-beta.11"
37
37
  },
38
38
  "scripts": {
39
39
  "build": "unbuild .",