unhead 1.9.1 → 1.9.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.
package/dist/index.cjs CHANGED
@@ -100,12 +100,10 @@ const EventHandlersPlugin = shared.defineHeadPlugin((head) => ({
100
100
  for (const tag of ctx.tags.filter((t) => ValidEventTags.includes(t.tag))) {
101
101
  Object.entries(tag.props).forEach(([key, value]) => {
102
102
  if (key.startsWith("on") && typeof value === "function") {
103
- if (head.ssr && shared.NetworkEvents.includes(key)) {
104
- tag.props[key] = `this.dataset.${key} = true`;
105
- tag.props["data-unhead-events"] = "";
106
- } else {
103
+ if (head.ssr && shared.NetworkEvents.includes(key))
104
+ tag.props[key] = `this.dataset.${key}fired = true`;
105
+ else
107
106
  delete tag.props[key];
108
- }
109
107
  tag._eventHandlers = tag._eventHandlers || {};
110
108
  tag._eventHandlers[key] = value;
111
109
  }
@@ -114,19 +112,10 @@ const EventHandlersPlugin = shared.defineHeadPlugin((head) => ({
114
112
  tag.key = tag.key || shared.hashCode(tag.props.src || tag.props.href);
115
113
  }
116
114
  },
117
- "dom:renderTag": function(ctx) {
118
- const $el = ctx.$el;
119
- if (!$el?.dataset || !("unheadEvents" in $el.dataset))
120
- return;
121
- delete $el.dataset.unheadEvents;
122
- const handler = (k) => ctx.tag._eventHandlers?.[k]?.call(ctx.$el, new Event(k.replace("on", "")));
123
- for (const k of Object.keys($el.dataset).filter((k2) => shared.NetworkEvents.includes(k2)))
124
- handler(k);
125
- if (typeof MutationObserver !== "undefined") {
126
- const observer = new MutationObserver((e) => {
127
- e.filter((m) => m.attributeName && shared.NetworkEvents.includes(m.attributeName.replace("data-", ""))).map((m) => m.attributeName.replace("data-", "")).map(handler);
128
- });
129
- observer.observe(ctx.$el, { attributes: true });
115
+ "dom:renderTag": function({ $el, tag }) {
116
+ for (const k of Object.keys($el?.dataset || {}).filter((k2) => shared.NetworkEvents.some((e) => `${e}fired` === k2))) {
117
+ const ek = k.replace("fired", "");
118
+ tag._eventHandlers?.[ek]?.call($el, new Event(ek.replace("on", "")));
130
119
  }
131
120
  }
132
121
  }
@@ -471,6 +460,25 @@ function useScript(_input, _options) {
471
460
  script.status = s;
472
461
  head.hooks.callHook(`script:updated`, hookCtx);
473
462
  };
463
+ const trigger = options.trigger;
464
+ shared.ScriptNetworkEvents.forEach((fn) => {
465
+ const _fn = typeof input[fn] === "function" ? input[fn].bind(options.eventContext) : null;
466
+ input[fn] = (e) => {
467
+ syncStatus(fn === "onload" ? "loaded" : fn === "onerror" ? "error" : "loading");
468
+ _fn?.(e);
469
+ };
470
+ });
471
+ const loadPromise = new Promise((resolve, reject) => {
472
+ const cleanUp = head.hooks.hook("script:updated", ({ script: script2 }) => {
473
+ if (script2.id === id && (script2.status === "loaded" || script2.status === "error")) {
474
+ if (script2.status === "loaded")
475
+ resolve(options.use?.());
476
+ else if (script2.status === "error")
477
+ reject(new Error(`Failed to load script: ${input.src}`));
478
+ cleanUp();
479
+ }
480
+ });
481
+ });
474
482
  const script = {
475
483
  id,
476
484
  status: "awaitingLoad",
@@ -491,31 +499,18 @@ function useScript(_input, _options) {
491
499
  script: [{ defer: true, fetchpriority: "low", ...input, key }]
492
500
  }, options);
493
501
  }
494
- return script.loadPromise;
502
+ return loadPromise;
495
503
  }
496
504
  };
497
- script.loadPromise = new Promise((resolve, reject) => {
498
- const removeHook2 = head.hooks.hook("script:updated", ({ script: script2 }) => {
499
- if (script2.id === id && (script2.status === "loaded" || script2.status === "error")) {
500
- script2.status === "loaded" && resolve(options.use?.());
501
- script2.status === "error" && reject(new Error(`Failed to load script: ${input.src}`));
502
- removeHook2();
503
- }
504
- });
505
- });
506
505
  const hookCtx = { script };
507
- shared.ScriptNetworkEvents.forEach((fn) => {
508
- const _fn = typeof input[fn] === "function" ? input[fn].bind(options.eventContext) : null;
509
- input[fn] = (e) => {
510
- syncStatus(fn === "onload" ? "loaded" : fn === "onerror" ? "error" : "loading");
511
- _fn?.(e);
512
- };
513
- });
514
- const trigger = options.trigger;
515
- if (options.trigger)
516
- trigger instanceof Promise && trigger.then(script.load);
517
- else
506
+ if (trigger) {
507
+ if (trigger instanceof Promise)
508
+ trigger.then(script.load);
509
+ else if (typeof trigger === "function")
510
+ trigger(script.load);
511
+ } else {
518
512
  script.load();
513
+ }
519
514
  const removeHook = head.hooks.hook("dom:renderTag", (ctx) => {
520
515
  if (ctx.tag.key !== key)
521
516
  return;
@@ -533,7 +528,7 @@ function useScript(_input, _options) {
533
528
  });
534
529
  const instance = new Proxy({}, {
535
530
  get(_, fn) {
536
- const $script = Object.assign(script.loadPromise, script);
531
+ const $script = Object.assign(loadPromise, script);
537
532
  const stub = options.stub?.({ script: $script, fn });
538
533
  if (stub)
539
534
  return stub;
@@ -544,7 +539,7 @@ function useScript(_input, _options) {
544
539
  head.hooks.callHook("script:instance-fn", hookCtx2);
545
540
  if (head.ssr || !options.use)
546
541
  return;
547
- return script.status === "loaded" ? options.use()[fn](...args) : script.loadPromise.then((api) => api[fn](...args));
542
+ return script.status === "loaded" ? options.use()[fn]?.(...args) : loadPromise.then((api) => api[fn]?.(...args));
548
543
  };
549
544
  }
550
545
  });
package/dist/index.mjs CHANGED
@@ -99,12 +99,10 @@ const EventHandlersPlugin = defineHeadPlugin((head) => ({
99
99
  for (const tag of ctx.tags.filter((t) => ValidEventTags.includes(t.tag))) {
100
100
  Object.entries(tag.props).forEach(([key, value]) => {
101
101
  if (key.startsWith("on") && typeof value === "function") {
102
- if (head.ssr && NetworkEvents.includes(key)) {
103
- tag.props[key] = `this.dataset.${key} = true`;
104
- tag.props["data-unhead-events"] = "";
105
- } else {
102
+ if (head.ssr && NetworkEvents.includes(key))
103
+ tag.props[key] = `this.dataset.${key}fired = true`;
104
+ else
106
105
  delete tag.props[key];
107
- }
108
106
  tag._eventHandlers = tag._eventHandlers || {};
109
107
  tag._eventHandlers[key] = value;
110
108
  }
@@ -113,19 +111,10 @@ const EventHandlersPlugin = defineHeadPlugin((head) => ({
113
111
  tag.key = tag.key || hashCode(tag.props.src || tag.props.href);
114
112
  }
115
113
  },
116
- "dom:renderTag": function(ctx) {
117
- const $el = ctx.$el;
118
- if (!$el?.dataset || !("unheadEvents" in $el.dataset))
119
- return;
120
- delete $el.dataset.unheadEvents;
121
- const handler = (k) => ctx.tag._eventHandlers?.[k]?.call(ctx.$el, new Event(k.replace("on", "")));
122
- for (const k of Object.keys($el.dataset).filter((k2) => NetworkEvents.includes(k2)))
123
- handler(k);
124
- if (typeof MutationObserver !== "undefined") {
125
- const observer = new MutationObserver((e) => {
126
- e.filter((m) => m.attributeName && NetworkEvents.includes(m.attributeName.replace("data-", ""))).map((m) => m.attributeName.replace("data-", "")).map(handler);
127
- });
128
- observer.observe(ctx.$el, { attributes: true });
114
+ "dom:renderTag": function({ $el, tag }) {
115
+ for (const k of Object.keys($el?.dataset || {}).filter((k2) => NetworkEvents.some((e) => `${e}fired` === k2))) {
116
+ const ek = k.replace("fired", "");
117
+ tag._eventHandlers?.[ek]?.call($el, new Event(ek.replace("on", "")));
129
118
  }
130
119
  }
131
120
  }
@@ -470,6 +459,25 @@ function useScript(_input, _options) {
470
459
  script.status = s;
471
460
  head.hooks.callHook(`script:updated`, hookCtx);
472
461
  };
462
+ const trigger = options.trigger;
463
+ ScriptNetworkEvents.forEach((fn) => {
464
+ const _fn = typeof input[fn] === "function" ? input[fn].bind(options.eventContext) : null;
465
+ input[fn] = (e) => {
466
+ syncStatus(fn === "onload" ? "loaded" : fn === "onerror" ? "error" : "loading");
467
+ _fn?.(e);
468
+ };
469
+ });
470
+ const loadPromise = new Promise((resolve, reject) => {
471
+ const cleanUp = head.hooks.hook("script:updated", ({ script: script2 }) => {
472
+ if (script2.id === id && (script2.status === "loaded" || script2.status === "error")) {
473
+ if (script2.status === "loaded")
474
+ resolve(options.use?.());
475
+ else if (script2.status === "error")
476
+ reject(new Error(`Failed to load script: ${input.src}`));
477
+ cleanUp();
478
+ }
479
+ });
480
+ });
473
481
  const script = {
474
482
  id,
475
483
  status: "awaitingLoad",
@@ -490,31 +498,18 @@ function useScript(_input, _options) {
490
498
  script: [{ defer: true, fetchpriority: "low", ...input, key }]
491
499
  }, options);
492
500
  }
493
- return script.loadPromise;
501
+ return loadPromise;
494
502
  }
495
503
  };
496
- script.loadPromise = new Promise((resolve, reject) => {
497
- const removeHook2 = head.hooks.hook("script:updated", ({ script: script2 }) => {
498
- if (script2.id === id && (script2.status === "loaded" || script2.status === "error")) {
499
- script2.status === "loaded" && resolve(options.use?.());
500
- script2.status === "error" && reject(new Error(`Failed to load script: ${input.src}`));
501
- removeHook2();
502
- }
503
- });
504
- });
505
504
  const hookCtx = { script };
506
- ScriptNetworkEvents.forEach((fn) => {
507
- const _fn = typeof input[fn] === "function" ? input[fn].bind(options.eventContext) : null;
508
- input[fn] = (e) => {
509
- syncStatus(fn === "onload" ? "loaded" : fn === "onerror" ? "error" : "loading");
510
- _fn?.(e);
511
- };
512
- });
513
- const trigger = options.trigger;
514
- if (options.trigger)
515
- trigger instanceof Promise && trigger.then(script.load);
516
- else
505
+ if (trigger) {
506
+ if (trigger instanceof Promise)
507
+ trigger.then(script.load);
508
+ else if (typeof trigger === "function")
509
+ trigger(script.load);
510
+ } else {
517
511
  script.load();
512
+ }
518
513
  const removeHook = head.hooks.hook("dom:renderTag", (ctx) => {
519
514
  if (ctx.tag.key !== key)
520
515
  return;
@@ -532,7 +527,7 @@ function useScript(_input, _options) {
532
527
  });
533
528
  const instance = new Proxy({}, {
534
529
  get(_, fn) {
535
- const $script = Object.assign(script.loadPromise, script);
530
+ const $script = Object.assign(loadPromise, script);
536
531
  const stub = options.stub?.({ script: $script, fn });
537
532
  if (stub)
538
533
  return stub;
@@ -543,7 +538,7 @@ function useScript(_input, _options) {
543
538
  head.hooks.callHook("script:instance-fn", hookCtx2);
544
539
  if (head.ssr || !options.use)
545
540
  return;
546
- return script.status === "loaded" ? options.use()[fn](...args) : script.loadPromise.then((api) => api[fn](...args));
541
+ return script.status === "loaded" ? options.use()[fn]?.(...args) : loadPromise.then((api) => api[fn]?.(...args));
547
542
  };
548
543
  }
549
544
  });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "unhead",
3
3
  "type": "module",
4
- "version": "1.9.1",
4
+ "version": "1.9.3",
5
5
  "author": {
6
6
  "name": "Harlan Wilton",
7
7
  "email": "harlan@harlanzw.com",
@@ -34,9 +34,9 @@
34
34
  ],
35
35
  "dependencies": {
36
36
  "hookable": "^5.5.3",
37
- "@unhead/dom": "1.9.1",
38
- "@unhead/schema": "1.9.1",
39
- "@unhead/shared": "1.9.1"
37
+ "@unhead/dom": "1.9.3",
38
+ "@unhead/schema": "1.9.3",
39
+ "@unhead/shared": "1.9.3"
40
40
  },
41
41
  "scripts": {
42
42
  "build": "unbuild .",