unhead 1.9.7 → 1.9.8

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,7 @@ function useScript(_input, _options) {
468
468
  _fn?.(e);
469
469
  };
470
470
  });
471
+ const proxy = { value: !head.ssr && options?.use?.() || {} };
471
472
  const loadPromise = new Promise((resolve, reject) => {
472
473
  const _ = head.hooks.hook("script:updated", ({ script: script2 }) => {
473
474
  if (script2.id === id && (script2.status === "loaded" || script2.status === "error")) {
@@ -480,7 +481,7 @@ function useScript(_input, _options) {
480
481
  _();
481
482
  }
482
483
  });
483
- });
484
+ }).then((api) => proxy.value = api);
484
485
  const script = {
485
486
  id,
486
487
  status: "awaitingLoad",
@@ -518,28 +519,20 @@ function useScript(_input, _options) {
518
519
  trigger.then(script.load);
519
520
  else if (typeof trigger === "function")
520
521
  trigger(script.load);
521
- const instance = new Proxy({}, {
522
- get(_, fn) {
522
+ const instance = new Proxy(proxy, {
523
+ get({ value: _ }, k) {
523
524
  const $script = Object.assign(loadPromise, script);
524
- const stub = options.stub?.({ script: $script, fn });
525
+ const stub = options.stub?.({ script: $script, fn: k });
525
526
  if (stub)
526
527
  return stub;
527
- if (fn === "$script")
528
+ if (k === "$script")
528
529
  return $script;
529
- const attempt = (api, args) => {
530
- if (head.ssr)
531
- return;
532
- api = api || options.use?.();
533
- const exists = !!(api && fn in api);
534
- const hookCtx2 = { script, fn, args, exists };
535
- head.hooks.callHook("script:instance-fn", hookCtx2);
536
- if (exists) {
537
- if (typeof api[fn] === "function")
538
- return api[fn](...args || []);
539
- return api[fn];
540
- }
541
- };
542
- return attempt() || ((...args) => loadPromise.then((api) => attempt(api, args)));
530
+ const exists = k in _;
531
+ head.hooks.callHook("script:instance-fn", { script, fn: k, exists: k in _ });
532
+ return exists ? Reflect.get(_, k) : (...args) => loadPromise.then((api) => {
533
+ const _k = Reflect.get(api, k);
534
+ return typeof _k === "function" ? Reflect.apply(api[k], api, args) : _k;
535
+ });
543
536
  }
544
537
  });
545
538
  head._scripts = Object.assign(
package/dist/index.mjs CHANGED
@@ -467,6 +467,7 @@ function useScript(_input, _options) {
467
467
  _fn?.(e);
468
468
  };
469
469
  });
470
+ const proxy = { value: !head.ssr && options?.use?.() || {} };
470
471
  const loadPromise = new Promise((resolve, reject) => {
471
472
  const _ = head.hooks.hook("script:updated", ({ script: script2 }) => {
472
473
  if (script2.id === id && (script2.status === "loaded" || script2.status === "error")) {
@@ -479,7 +480,7 @@ function useScript(_input, _options) {
479
480
  _();
480
481
  }
481
482
  });
482
- });
483
+ }).then((api) => proxy.value = api);
483
484
  const script = {
484
485
  id,
485
486
  status: "awaitingLoad",
@@ -517,28 +518,20 @@ function useScript(_input, _options) {
517
518
  trigger.then(script.load);
518
519
  else if (typeof trigger === "function")
519
520
  trigger(script.load);
520
- const instance = new Proxy({}, {
521
- get(_, fn) {
521
+ const instance = new Proxy(proxy, {
522
+ get({ value: _ }, k) {
522
523
  const $script = Object.assign(loadPromise, script);
523
- const stub = options.stub?.({ script: $script, fn });
524
+ const stub = options.stub?.({ script: $script, fn: k });
524
525
  if (stub)
525
526
  return stub;
526
- if (fn === "$script")
527
+ if (k === "$script")
527
528
  return $script;
528
- const attempt = (api, args) => {
529
- if (head.ssr)
530
- return;
531
- api = api || options.use?.();
532
- const exists = !!(api && fn in api);
533
- const hookCtx2 = { script, fn, args, exists };
534
- head.hooks.callHook("script:instance-fn", hookCtx2);
535
- if (exists) {
536
- if (typeof api[fn] === "function")
537
- return api[fn](...args || []);
538
- return api[fn];
539
- }
540
- };
541
- return attempt() || ((...args) => loadPromise.then((api) => attempt(api, args)));
529
+ const exists = k in _;
530
+ head.hooks.callHook("script:instance-fn", { script, fn: k, exists: k in _ });
531
+ return exists ? Reflect.get(_, k) : (...args) => loadPromise.then((api) => {
532
+ const _k = Reflect.get(api, k);
533
+ return typeof _k === "function" ? Reflect.apply(api[k], api, args) : _k;
534
+ });
542
535
  }
543
536
  });
544
537
  head._scripts = Object.assign(
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "unhead",
3
3
  "type": "module",
4
- "version": "1.9.7",
4
+ "version": "1.9.8",
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.9.7",
38
- "@unhead/dom": "1.9.7",
39
- "@unhead/shared": "1.9.7"
37
+ "@unhead/dom": "1.9.8",
38
+ "@unhead/schema": "1.9.8",
39
+ "@unhead/shared": "1.9.8"
40
40
  },
41
41
  "scripts": {
42
42
  "build": "unbuild .",