unhead 1.10.0 → 1.10.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
@@ -535,14 +535,16 @@ function useScript(_input, _options) {
535
535
  if (!head)
536
536
  throw new Error("Missing Unhead context.");
537
537
  const id = resolveScriptKey(input);
538
- if (head._scripts?.[id])
539
- return head._scripts[id];
538
+ const prevScript = head._scripts?.[id];
539
+ if (prevScript) {
540
+ prevScript.updateTrigger(options.trigger);
541
+ return prevScript;
542
+ }
540
543
  options.beforeInit?.();
541
544
  const syncStatus = (s) => {
542
545
  script.status = s;
543
546
  head.hooks.callHook(`script:updated`, hookCtx);
544
547
  };
545
- const trigger = options.trigger !== void 0 ? options.trigger : "client";
546
548
  shared.ScriptNetworkEvents.forEach((fn) => {
547
549
  const _fn = typeof input[fn] === "function" ? input[fn].bind(options.eventContext) : null;
548
550
  input[fn] = (e) => {
@@ -622,6 +624,24 @@ function useScript(_input, _options) {
622
624
  onError(cb) {
623
625
  return _registerCb("error", cb);
624
626
  },
627
+ updateTrigger(trigger) {
628
+ script._triggerAbortController?.abort();
629
+ if ((typeof trigger === "undefined" || trigger === "client") && !head.ssr || trigger === "server") {
630
+ script.load();
631
+ } else if (trigger instanceof Promise) {
632
+ script._triggerAbortController = new AbortController();
633
+ Promise.race([
634
+ trigger.then(() => script.load),
635
+ new Promise((resolve) => {
636
+ script._triggerAbortController.signal.addEventListener("abort", () => resolve());
637
+ })
638
+ ]).then((res2) => {
639
+ res2?.();
640
+ });
641
+ } else if (typeof trigger === "function") {
642
+ trigger(script.load);
643
+ }
644
+ },
625
645
  _cbs
626
646
  });
627
647
  loadPromise.then((api) => {
@@ -635,12 +655,7 @@ function useScript(_input, _options) {
635
655
  _cbs.error = null;
636
656
  });
637
657
  const hookCtx = { script };
638
- if (trigger === "client" && !head.ssr || trigger === "server")
639
- script.load();
640
- else if (trigger instanceof Promise)
641
- trigger.then(script.load);
642
- else if (typeof trigger === "function")
643
- trigger(async () => script.load());
658
+ script.updateTrigger(options.trigger);
644
659
  script.$script = script;
645
660
  const proxyChain = (instance, accessor, accessors) => {
646
661
  return new Proxy((!accessor ? instance : instance?.[accessor]) || scriptProxy, {
package/dist/index.mjs CHANGED
@@ -534,14 +534,16 @@ function useScript(_input, _options) {
534
534
  if (!head)
535
535
  throw new Error("Missing Unhead context.");
536
536
  const id = resolveScriptKey(input);
537
- if (head._scripts?.[id])
538
- return head._scripts[id];
537
+ const prevScript = head._scripts?.[id];
538
+ if (prevScript) {
539
+ prevScript.updateTrigger(options.trigger);
540
+ return prevScript;
541
+ }
539
542
  options.beforeInit?.();
540
543
  const syncStatus = (s) => {
541
544
  script.status = s;
542
545
  head.hooks.callHook(`script:updated`, hookCtx);
543
546
  };
544
- const trigger = options.trigger !== void 0 ? options.trigger : "client";
545
547
  ScriptNetworkEvents.forEach((fn) => {
546
548
  const _fn = typeof input[fn] === "function" ? input[fn].bind(options.eventContext) : null;
547
549
  input[fn] = (e) => {
@@ -621,6 +623,24 @@ function useScript(_input, _options) {
621
623
  onError(cb) {
622
624
  return _registerCb("error", cb);
623
625
  },
626
+ updateTrigger(trigger) {
627
+ script._triggerAbortController?.abort();
628
+ if ((typeof trigger === "undefined" || trigger === "client") && !head.ssr || trigger === "server") {
629
+ script.load();
630
+ } else if (trigger instanceof Promise) {
631
+ script._triggerAbortController = new AbortController();
632
+ Promise.race([
633
+ trigger.then(() => script.load),
634
+ new Promise((resolve) => {
635
+ script._triggerAbortController.signal.addEventListener("abort", () => resolve());
636
+ })
637
+ ]).then((res2) => {
638
+ res2?.();
639
+ });
640
+ } else if (typeof trigger === "function") {
641
+ trigger(script.load);
642
+ }
643
+ },
624
644
  _cbs
625
645
  });
626
646
  loadPromise.then((api) => {
@@ -634,12 +654,7 @@ function useScript(_input, _options) {
634
654
  _cbs.error = null;
635
655
  });
636
656
  const hookCtx = { script };
637
- if (trigger === "client" && !head.ssr || trigger === "server")
638
- script.load();
639
- else if (trigger instanceof Promise)
640
- trigger.then(script.load);
641
- else if (typeof trigger === "function")
642
- trigger(async () => script.load());
657
+ script.updateTrigger(options.trigger);
643
658
  script.$script = script;
644
659
  const proxyChain = (instance, accessor, accessors) => {
645
660
  return new Proxy((!accessor ? instance : instance?.[accessor]) || scriptProxy, {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "unhead",
3
3
  "type": "module",
4
- "version": "1.10.0",
4
+ "version": "1.10.2",
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/schema": "1.10.0",
38
- "@unhead/shared": "1.10.0",
39
- "@unhead/dom": "1.10.0"
37
+ "@unhead/dom": "1.10.2",
38
+ "@unhead/shared": "1.10.2",
39
+ "@unhead/schema": "1.10.2"
40
40
  },
41
41
  "scripts": {
42
42
  "build": "unbuild .",