unhead 1.9.2 → 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
@@ -468,6 +468,17 @@ function useScript(_input, _options) {
468
468
  _fn?.(e);
469
469
  };
470
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
+ });
471
482
  const script = {
472
483
  id,
473
484
  status: "awaitingLoad",
@@ -488,20 +499,9 @@ function useScript(_input, _options) {
488
499
  script: [{ defer: true, fetchpriority: "low", ...input, key }]
489
500
  }, options);
490
501
  }
491
- return script.loadPromise;
502
+ return loadPromise;
492
503
  }
493
504
  };
494
- script.loadPromise = new Promise((resolve, reject) => {
495
- const removeHook2 = head.hooks.hook("script:updated", ({ script: script2 }) => {
496
- if (script2.id === id && (script2.status === "loaded" || script2.status === "error")) {
497
- if (script2.status === "loaded")
498
- resolve(options.use?.());
499
- else if (script2.status === "error")
500
- reject(new Error(`Failed to load script: ${input.src}`));
501
- removeHook2();
502
- }
503
- });
504
- });
505
505
  const hookCtx = { script };
506
506
  if (trigger) {
507
507
  if (trigger instanceof Promise)
@@ -528,7 +528,7 @@ function useScript(_input, _options) {
528
528
  });
529
529
  const instance = new Proxy({}, {
530
530
  get(_, fn) {
531
- const $script = Object.assign(script.loadPromise, script);
531
+ const $script = Object.assign(loadPromise, script);
532
532
  const stub = options.stub?.({ script: $script, fn });
533
533
  if (stub)
534
534
  return stub;
@@ -539,7 +539,7 @@ function useScript(_input, _options) {
539
539
  head.hooks.callHook("script:instance-fn", hookCtx2);
540
540
  if (head.ssr || !options.use)
541
541
  return;
542
- 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));
543
543
  };
544
544
  }
545
545
  });
package/dist/index.mjs CHANGED
@@ -467,6 +467,17 @@ function useScript(_input, _options) {
467
467
  _fn?.(e);
468
468
  };
469
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
+ });
470
481
  const script = {
471
482
  id,
472
483
  status: "awaitingLoad",
@@ -487,20 +498,9 @@ function useScript(_input, _options) {
487
498
  script: [{ defer: true, fetchpriority: "low", ...input, key }]
488
499
  }, options);
489
500
  }
490
- return script.loadPromise;
501
+ return loadPromise;
491
502
  }
492
503
  };
493
- script.loadPromise = new Promise((resolve, reject) => {
494
- const removeHook2 = head.hooks.hook("script:updated", ({ script: script2 }) => {
495
- if (script2.id === id && (script2.status === "loaded" || script2.status === "error")) {
496
- if (script2.status === "loaded")
497
- resolve(options.use?.());
498
- else if (script2.status === "error")
499
- reject(new Error(`Failed to load script: ${input.src}`));
500
- removeHook2();
501
- }
502
- });
503
- });
504
504
  const hookCtx = { script };
505
505
  if (trigger) {
506
506
  if (trigger instanceof Promise)
@@ -527,7 +527,7 @@ function useScript(_input, _options) {
527
527
  });
528
528
  const instance = new Proxy({}, {
529
529
  get(_, fn) {
530
- const $script = Object.assign(script.loadPromise, script);
530
+ const $script = Object.assign(loadPromise, script);
531
531
  const stub = options.stub?.({ script: $script, fn });
532
532
  if (stub)
533
533
  return stub;
@@ -538,7 +538,7 @@ function useScript(_input, _options) {
538
538
  head.hooks.callHook("script:instance-fn", hookCtx2);
539
539
  if (head.ssr || !options.use)
540
540
  return;
541
- 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));
542
542
  };
543
543
  }
544
544
  });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "unhead",
3
3
  "type": "module",
4
- "version": "1.9.2",
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/shared": "1.9.2",
38
- "@unhead/schema": "1.9.2",
39
- "@unhead/dom": "1.9.2"
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 .",