@unhead/dom 0.4.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
@@ -9,10 +9,11 @@ async function renderDOMHead(head, options = {}) {
9
9
  await head.hooks.callHook("dom:beforeRender", ctx);
10
10
  if (!ctx.shouldRender)
11
11
  return;
12
- const queuedSideEffects = head._popSideEffectQueue();
12
+ Object.values(head._popSideEffectQueue()).forEach((fn) => fn());
13
+ const staleSideEffects = {};
13
14
  head.headEntries().map((entry) => entry._sde).forEach((sde) => {
14
15
  Object.entries(sde).forEach(([key, fn]) => {
15
- queuedSideEffects[key] = fn;
16
+ staleSideEffects[key] = fn;
16
17
  });
17
18
  });
18
19
  const renderTag = (tag, entry) => {
@@ -21,24 +22,24 @@ async function renderDOMHead(head, options = {}) {
21
22
  return dom.head.querySelector("title");
22
23
  }
23
24
  const markSideEffect = (key, fn) => {
24
- key = `${tag._s || tag._p}:${key}`;
25
+ key = `${tag._p}:${key}`;
25
26
  entry._sde[key] = fn;
26
- delete queuedSideEffects[key];
27
+ delete staleSideEffects[key];
27
28
  };
28
- const setAttrs = ($el) => {
29
+ const setAttrs = ($el, sideEffects = true) => {
29
30
  Object.entries(tag.props).forEach(([k, value]) => {
30
31
  value = String(value);
31
32
  const attrSdeKey = `attr:${k}`;
32
33
  if (k === "class") {
33
34
  for (const c of value.split(" ")) {
34
35
  const classSdeKey = `${attrSdeKey}:${c}`;
35
- markSideEffect(classSdeKey, () => $el.classList.remove(c));
36
+ sideEffects && markSideEffect(classSdeKey, () => $el.classList.remove(c));
36
37
  if (!$el.classList.contains(c))
37
38
  $el.classList.add(c);
38
39
  }
39
40
  return;
40
41
  }
41
- if (!k.startsWith("data-h-"))
42
+ if (sideEffects && !k.startsWith("data-h-"))
42
43
  markSideEffect(attrSdeKey, () => $el.removeAttribute(k));
43
44
  if ($el.getAttribute(k) !== value)
44
45
  $el.setAttribute(k, value);
@@ -52,21 +53,19 @@ async function renderDOMHead(head, options = {}) {
52
53
  return $el;
53
54
  }
54
55
  let $newEl = dom.createElement(tag.tag);
55
- setAttrs($newEl);
56
+ setAttrs($newEl, false);
56
57
  let $previousEl;
57
58
  for (const $el of dom[tag.tagPosition?.startsWith("body") ? "body" : "head"].children) {
58
- if ($el.hasAttribute(`${tag._s}`) || $el.isEqualNode($newEl)) {
59
+ if (tag._s && $el.hasAttribute(`${tag._s}`) || $el.isEqualNode($newEl)) {
59
60
  $previousEl = $el;
60
61
  break;
61
62
  }
62
63
  }
63
- if ($newEl.attributes.length === 0 && !$newEl.innerHTML) {
64
- $previousEl?.remove();
65
- return;
66
- }
67
64
  if ($previousEl) {
68
- markSideEffect("el", () => $previousEl?.remove());
69
- setAttrs($previousEl);
65
+ markSideEffect("el", () => {
66
+ $previousEl?.remove();
67
+ });
68
+ setAttrs($previousEl, false);
70
69
  return $previousEl;
71
70
  }
72
71
  switch (tag.tagPosition) {
@@ -86,14 +85,14 @@ async function renderDOMHead(head, options = {}) {
86
85
  };
87
86
  for (const tag of ctx.tags) {
88
87
  const entry = head.headEntries().find((e) => e._i === Number(tag._e));
89
- const renderCtx = { $el: null, shouldRender: true, tag, entry, queuedSideEffects };
88
+ const renderCtx = { $el: null, shouldRender: true, tag, entry, queuedSideEffects: staleSideEffects };
90
89
  await head.hooks.callHook("dom:beforeRenderTag", renderCtx);
91
90
  if (!renderCtx.shouldRender)
92
91
  continue;
93
92
  renderCtx.$el = renderTag(renderCtx.tag, renderCtx.entry);
94
93
  await head.hooks.callHook("dom:renderTag", renderCtx);
95
94
  }
96
- Object.values(queuedSideEffects).forEach((fn) => fn());
95
+ Object.values(staleSideEffects).forEach((fn) => fn());
97
96
  }
98
97
  exports.domUpdatePromise = null;
99
98
  async function debouncedRenderDOMHead(head, options = {}) {
package/dist/index.mjs CHANGED
@@ -7,10 +7,11 @@ async function renderDOMHead(head, options = {}) {
7
7
  await head.hooks.callHook("dom:beforeRender", ctx);
8
8
  if (!ctx.shouldRender)
9
9
  return;
10
- const queuedSideEffects = head._popSideEffectQueue();
10
+ Object.values(head._popSideEffectQueue()).forEach((fn) => fn());
11
+ const staleSideEffects = {};
11
12
  head.headEntries().map((entry) => entry._sde).forEach((sde) => {
12
13
  Object.entries(sde).forEach(([key, fn]) => {
13
- queuedSideEffects[key] = fn;
14
+ staleSideEffects[key] = fn;
14
15
  });
15
16
  });
16
17
  const renderTag = (tag, entry) => {
@@ -19,24 +20,24 @@ async function renderDOMHead(head, options = {}) {
19
20
  return dom.head.querySelector("title");
20
21
  }
21
22
  const markSideEffect = (key, fn) => {
22
- key = `${tag._s || tag._p}:${key}`;
23
+ key = `${tag._p}:${key}`;
23
24
  entry._sde[key] = fn;
24
- delete queuedSideEffects[key];
25
+ delete staleSideEffects[key];
25
26
  };
26
- const setAttrs = ($el) => {
27
+ const setAttrs = ($el, sideEffects = true) => {
27
28
  Object.entries(tag.props).forEach(([k, value]) => {
28
29
  value = String(value);
29
30
  const attrSdeKey = `attr:${k}`;
30
31
  if (k === "class") {
31
32
  for (const c of value.split(" ")) {
32
33
  const classSdeKey = `${attrSdeKey}:${c}`;
33
- markSideEffect(classSdeKey, () => $el.classList.remove(c));
34
+ sideEffects && markSideEffect(classSdeKey, () => $el.classList.remove(c));
34
35
  if (!$el.classList.contains(c))
35
36
  $el.classList.add(c);
36
37
  }
37
38
  return;
38
39
  }
39
- if (!k.startsWith("data-h-"))
40
+ if (sideEffects && !k.startsWith("data-h-"))
40
41
  markSideEffect(attrSdeKey, () => $el.removeAttribute(k));
41
42
  if ($el.getAttribute(k) !== value)
42
43
  $el.setAttribute(k, value);
@@ -50,21 +51,19 @@ async function renderDOMHead(head, options = {}) {
50
51
  return $el;
51
52
  }
52
53
  let $newEl = dom.createElement(tag.tag);
53
- setAttrs($newEl);
54
+ setAttrs($newEl, false);
54
55
  let $previousEl;
55
56
  for (const $el of dom[tag.tagPosition?.startsWith("body") ? "body" : "head"].children) {
56
- if ($el.hasAttribute(`${tag._s}`) || $el.isEqualNode($newEl)) {
57
+ if (tag._s && $el.hasAttribute(`${tag._s}`) || $el.isEqualNode($newEl)) {
57
58
  $previousEl = $el;
58
59
  break;
59
60
  }
60
61
  }
61
- if ($newEl.attributes.length === 0 && !$newEl.innerHTML) {
62
- $previousEl?.remove();
63
- return;
64
- }
65
62
  if ($previousEl) {
66
- markSideEffect("el", () => $previousEl?.remove());
67
- setAttrs($previousEl);
63
+ markSideEffect("el", () => {
64
+ $previousEl?.remove();
65
+ });
66
+ setAttrs($previousEl, false);
68
67
  return $previousEl;
69
68
  }
70
69
  switch (tag.tagPosition) {
@@ -84,14 +83,14 @@ async function renderDOMHead(head, options = {}) {
84
83
  };
85
84
  for (const tag of ctx.tags) {
86
85
  const entry = head.headEntries().find((e) => e._i === Number(tag._e));
87
- const renderCtx = { $el: null, shouldRender: true, tag, entry, queuedSideEffects };
86
+ const renderCtx = { $el: null, shouldRender: true, tag, entry, queuedSideEffects: staleSideEffects };
88
87
  await head.hooks.callHook("dom:beforeRenderTag", renderCtx);
89
88
  if (!renderCtx.shouldRender)
90
89
  continue;
91
90
  renderCtx.$el = renderTag(renderCtx.tag, renderCtx.entry);
92
91
  await head.hooks.callHook("dom:renderTag", renderCtx);
93
92
  }
94
- Object.values(queuedSideEffects).forEach((fn) => fn());
93
+ Object.values(staleSideEffects).forEach((fn) => fn());
95
94
  }
96
95
  let domUpdatePromise = null;
97
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.4.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,7 +30,7 @@
30
30
  "dist"
31
31
  ],
32
32
  "dependencies": {
33
- "@unhead/schema": "0.4.1"
33
+ "@unhead/schema": "0.4.2"
34
34
  },
35
35
  "devDependencies": {
36
36
  "zhead": "1.0.0-beta.11"