unhead 1.9.2 → 1.9.4

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
@@ -460,7 +460,7 @@ function useScript(_input, _options) {
460
460
  script.status = s;
461
461
  head.hooks.callHook(`script:updated`, hookCtx);
462
462
  };
463
- const trigger = options.trigger;
463
+ const trigger = typeof options.trigger !== "undefined" ? options.trigger : "client";
464
464
  shared.ScriptNetworkEvents.forEach((fn) => {
465
465
  const _fn = typeof input[fn] === "function" ? input[fn].bind(options.eventContext) : null;
466
466
  input[fn] = (e) => {
@@ -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,29 +499,16 @@ 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
- if (trigger) {
507
- if (trigger instanceof Promise)
508
- trigger.then(script.load);
509
- else if (typeof trigger === "function")
510
- trigger(script.load);
511
- } else {
506
+ if (trigger === "client" && !head.ssr || trigger === "server" && head.ssr)
512
507
  script.load();
513
- }
508
+ else if (trigger instanceof Promise)
509
+ trigger.then(script.load);
510
+ else if (typeof trigger === "function")
511
+ trigger(script.load);
514
512
  const removeHook = head.hooks.hook("dom:renderTag", (ctx) => {
515
513
  if (ctx.tag.key !== key)
516
514
  return;
@@ -528,7 +526,7 @@ function useScript(_input, _options) {
528
526
  });
529
527
  const instance = new Proxy({}, {
530
528
  get(_, fn) {
531
- const $script = Object.assign(script.loadPromise, script);
529
+ const $script = Object.assign(loadPromise, script);
532
530
  const stub = options.stub?.({ script: $script, fn });
533
531
  if (stub)
534
532
  return stub;
@@ -539,7 +537,7 @@ function useScript(_input, _options) {
539
537
  head.hooks.callHook("script:instance-fn", hookCtx2);
540
538
  if (head.ssr || !options.use)
541
539
  return;
542
- return script.status === "loaded" ? options.use()[fn](...args) : script.loadPromise.then((api) => api[fn](...args));
540
+ return script.status === "loaded" ? options.use()[fn]?.(...args) : loadPromise.then((api) => api[fn]?.(...args));
543
541
  };
544
542
  }
545
543
  });
package/dist/index.mjs CHANGED
@@ -459,7 +459,7 @@ function useScript(_input, _options) {
459
459
  script.status = s;
460
460
  head.hooks.callHook(`script:updated`, hookCtx);
461
461
  };
462
- const trigger = options.trigger;
462
+ const trigger = typeof options.trigger !== "undefined" ? options.trigger : "client";
463
463
  ScriptNetworkEvents.forEach((fn) => {
464
464
  const _fn = typeof input[fn] === "function" ? input[fn].bind(options.eventContext) : null;
465
465
  input[fn] = (e) => {
@@ -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,29 +498,16 @@ 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
- if (trigger) {
506
- if (trigger instanceof Promise)
507
- trigger.then(script.load);
508
- else if (typeof trigger === "function")
509
- trigger(script.load);
510
- } else {
505
+ if (trigger === "client" && !head.ssr || trigger === "server" && head.ssr)
511
506
  script.load();
512
- }
507
+ else if (trigger instanceof Promise)
508
+ trigger.then(script.load);
509
+ else if (typeof trigger === "function")
510
+ trigger(script.load);
513
511
  const removeHook = head.hooks.hook("dom:renderTag", (ctx) => {
514
512
  if (ctx.tag.key !== key)
515
513
  return;
@@ -527,7 +525,7 @@ function useScript(_input, _options) {
527
525
  });
528
526
  const instance = new Proxy({}, {
529
527
  get(_, fn) {
530
- const $script = Object.assign(script.loadPromise, script);
528
+ const $script = Object.assign(loadPromise, script);
531
529
  const stub = options.stub?.({ script: $script, fn });
532
530
  if (stub)
533
531
  return stub;
@@ -538,7 +536,7 @@ function useScript(_input, _options) {
538
536
  head.hooks.callHook("script:instance-fn", hookCtx2);
539
537
  if (head.ssr || !options.use)
540
538
  return;
541
- return script.status === "loaded" ? options.use()[fn](...args) : script.loadPromise.then((api) => api[fn](...args));
539
+ return script.status === "loaded" ? options.use()[fn]?.(...args) : loadPromise.then((api) => api[fn]?.(...args));
542
540
  };
543
541
  }
544
542
  });
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.4",
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.4",
38
+ "@unhead/schema": "1.9.4",
39
+ "@unhead/shared": "1.9.4"
40
40
  },
41
41
  "scripts": {
42
42
  "build": "unbuild .",