unhead 1.9.4 → 1.9.6
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 +25 -28
- package/dist/index.d.cts +2 -2
- package/dist/index.d.mts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.mjs +25 -28
- package/package.json +4 -4
package/dist/index.cjs
CHANGED
|
@@ -453,9 +453,9 @@ function useScript(_input, _options) {
|
|
|
453
453
|
if (!head)
|
|
454
454
|
throw new Error("Missing Unhead context.");
|
|
455
455
|
const id = input.key || shared.hashCode(input.src || (typeof input.innerHTML === "string" ? input.innerHTML : ""));
|
|
456
|
-
const key = `use-script.${id}`;
|
|
457
456
|
if (head._scripts?.[id])
|
|
458
457
|
return head._scripts[id];
|
|
458
|
+
options.beforeInit?.();
|
|
459
459
|
const syncStatus = (s) => {
|
|
460
460
|
script.status = s;
|
|
461
461
|
head.hooks.callHook(`script:updated`, hookCtx);
|
|
@@ -469,20 +469,21 @@ function useScript(_input, _options) {
|
|
|
469
469
|
};
|
|
470
470
|
});
|
|
471
471
|
const loadPromise = new Promise((resolve, reject) => {
|
|
472
|
-
const
|
|
472
|
+
const _ = head.hooks.hook("script:updated", ({ script: script2 }) => {
|
|
473
473
|
if (script2.id === id && (script2.status === "loaded" || script2.status === "error")) {
|
|
474
|
-
if (script2.status === "loaded")
|
|
475
|
-
|
|
476
|
-
|
|
474
|
+
if (script2.status === "loaded") {
|
|
475
|
+
const api = options.use?.();
|
|
476
|
+
api && resolve(api);
|
|
477
|
+
} else if (script2.status === "error") {
|
|
477
478
|
reject(new Error(`Failed to load script: ${input.src}`));
|
|
478
|
-
|
|
479
|
+
}
|
|
480
|
+
_();
|
|
479
481
|
}
|
|
480
482
|
});
|
|
481
483
|
});
|
|
482
484
|
const script = {
|
|
483
485
|
id,
|
|
484
486
|
status: "awaitingLoad",
|
|
485
|
-
loaded: false,
|
|
486
487
|
remove() {
|
|
487
488
|
if (script.entry) {
|
|
488
489
|
script.entry.dispose();
|
|
@@ -495,8 +496,16 @@ function useScript(_input, _options) {
|
|
|
495
496
|
load() {
|
|
496
497
|
if (!script.entry) {
|
|
497
498
|
syncStatus("loading");
|
|
499
|
+
const defaults = {
|
|
500
|
+
defer: true,
|
|
501
|
+
fetchpriority: "low"
|
|
502
|
+
};
|
|
503
|
+
if (input.src && (input.src.startsWith("http") || input.src.startsWith("//"))) {
|
|
504
|
+
defaults.crossorigin = "anonymous";
|
|
505
|
+
defaults.referrerpolicy = "no-referrer";
|
|
506
|
+
}
|
|
498
507
|
script.entry = head.push({
|
|
499
|
-
script: [{
|
|
508
|
+
script: [{ ...defaults, ...input, key: `script.${id}` }]
|
|
500
509
|
}, options);
|
|
501
510
|
}
|
|
502
511
|
return loadPromise;
|
|
@@ -509,21 +518,6 @@ function useScript(_input, _options) {
|
|
|
509
518
|
trigger.then(script.load);
|
|
510
519
|
else if (typeof trigger === "function")
|
|
511
520
|
trigger(script.load);
|
|
512
|
-
const removeHook = head.hooks.hook("dom:renderTag", (ctx) => {
|
|
513
|
-
if (ctx.tag.key !== key)
|
|
514
|
-
return;
|
|
515
|
-
if (ctx.tag.innerHTML) {
|
|
516
|
-
setTimeout(
|
|
517
|
-
() => {
|
|
518
|
-
syncStatus("loaded");
|
|
519
|
-
typeof input.onload === "function" && input.onload.call(options.eventContext, new Event("load"));
|
|
520
|
-
},
|
|
521
|
-
5
|
|
522
|
-
/* give inline script a chance to run */
|
|
523
|
-
);
|
|
524
|
-
}
|
|
525
|
-
removeHook();
|
|
526
|
-
});
|
|
527
521
|
const instance = new Proxy({}, {
|
|
528
522
|
get(_, fn) {
|
|
529
523
|
const $script = Object.assign(loadPromise, script);
|
|
@@ -532,13 +526,16 @@ function useScript(_input, _options) {
|
|
|
532
526
|
return stub;
|
|
533
527
|
if (fn === "$script")
|
|
534
528
|
return $script;
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
head.hooks.callHook("script:instance-fn", hookCtx2);
|
|
538
|
-
if (head.ssr || !options.use)
|
|
529
|
+
const attempt = (args) => {
|
|
530
|
+
if (head.ssr)
|
|
539
531
|
return;
|
|
540
|
-
|
|
532
|
+
const 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
|
+
return exists && api[fn];
|
|
541
537
|
};
|
|
538
|
+
return attempt() || ((...args) => loadPromise.then(() => attempt(args)(...args)));
|
|
542
539
|
}
|
|
543
540
|
});
|
|
544
541
|
head._scripts = Object.assign(
|
package/dist/index.d.cts
CHANGED
|
@@ -47,8 +47,8 @@ declare function useServerSeoMeta(input: UseSeoMetaInput, options?: HeadEntryOpt
|
|
|
47
47
|
* @experimental
|
|
48
48
|
* @see https://unhead.unjs.io/usage/composables/use-script
|
|
49
49
|
*/
|
|
50
|
-
declare function useScript<T
|
|
51
|
-
$script: ScriptInstance<T>;
|
|
50
|
+
declare function useScript<T extends Record<symbol | string, any>>(_input: UseScriptInput, _options?: UseScriptOptions<T>): T & {
|
|
51
|
+
$script: Promise<T> & ScriptInstance<T>;
|
|
52
52
|
};
|
|
53
53
|
|
|
54
54
|
export { CapoPlugin, HashHydrationPlugin, type UseHeadInput, createHead, createHeadCore, createServerHead, getActiveHead, unheadComposablesImports, useHead, useHeadSafe, useScript, useSeoMeta, useServerHead, useServerHeadSafe, useServerSeoMeta };
|
package/dist/index.d.mts
CHANGED
|
@@ -47,8 +47,8 @@ declare function useServerSeoMeta(input: UseSeoMetaInput, options?: HeadEntryOpt
|
|
|
47
47
|
* @experimental
|
|
48
48
|
* @see https://unhead.unjs.io/usage/composables/use-script
|
|
49
49
|
*/
|
|
50
|
-
declare function useScript<T
|
|
51
|
-
$script: ScriptInstance<T>;
|
|
50
|
+
declare function useScript<T extends Record<symbol | string, any>>(_input: UseScriptInput, _options?: UseScriptOptions<T>): T & {
|
|
51
|
+
$script: Promise<T> & ScriptInstance<T>;
|
|
52
52
|
};
|
|
53
53
|
|
|
54
54
|
export { CapoPlugin, HashHydrationPlugin, type UseHeadInput, createHead, createHeadCore, createServerHead, getActiveHead, unheadComposablesImports, useHead, useHeadSafe, useScript, useSeoMeta, useServerHead, useServerHeadSafe, useServerSeoMeta };
|
package/dist/index.d.ts
CHANGED
|
@@ -47,8 +47,8 @@ declare function useServerSeoMeta(input: UseSeoMetaInput, options?: HeadEntryOpt
|
|
|
47
47
|
* @experimental
|
|
48
48
|
* @see https://unhead.unjs.io/usage/composables/use-script
|
|
49
49
|
*/
|
|
50
|
-
declare function useScript<T
|
|
51
|
-
$script: ScriptInstance<T>;
|
|
50
|
+
declare function useScript<T extends Record<symbol | string, any>>(_input: UseScriptInput, _options?: UseScriptOptions<T>): T & {
|
|
51
|
+
$script: Promise<T> & ScriptInstance<T>;
|
|
52
52
|
};
|
|
53
53
|
|
|
54
54
|
export { CapoPlugin, HashHydrationPlugin, type UseHeadInput, createHead, createHeadCore, createServerHead, getActiveHead, unheadComposablesImports, useHead, useHeadSafe, useScript, useSeoMeta, useServerHead, useServerHeadSafe, useServerSeoMeta };
|
package/dist/index.mjs
CHANGED
|
@@ -452,9 +452,9 @@ function useScript(_input, _options) {
|
|
|
452
452
|
if (!head)
|
|
453
453
|
throw new Error("Missing Unhead context.");
|
|
454
454
|
const id = input.key || hashCode(input.src || (typeof input.innerHTML === "string" ? input.innerHTML : ""));
|
|
455
|
-
const key = `use-script.${id}`;
|
|
456
455
|
if (head._scripts?.[id])
|
|
457
456
|
return head._scripts[id];
|
|
457
|
+
options.beforeInit?.();
|
|
458
458
|
const syncStatus = (s) => {
|
|
459
459
|
script.status = s;
|
|
460
460
|
head.hooks.callHook(`script:updated`, hookCtx);
|
|
@@ -468,20 +468,21 @@ function useScript(_input, _options) {
|
|
|
468
468
|
};
|
|
469
469
|
});
|
|
470
470
|
const loadPromise = new Promise((resolve, reject) => {
|
|
471
|
-
const
|
|
471
|
+
const _ = head.hooks.hook("script:updated", ({ script: script2 }) => {
|
|
472
472
|
if (script2.id === id && (script2.status === "loaded" || script2.status === "error")) {
|
|
473
|
-
if (script2.status === "loaded")
|
|
474
|
-
|
|
475
|
-
|
|
473
|
+
if (script2.status === "loaded") {
|
|
474
|
+
const api = options.use?.();
|
|
475
|
+
api && resolve(api);
|
|
476
|
+
} else if (script2.status === "error") {
|
|
476
477
|
reject(new Error(`Failed to load script: ${input.src}`));
|
|
477
|
-
|
|
478
|
+
}
|
|
479
|
+
_();
|
|
478
480
|
}
|
|
479
481
|
});
|
|
480
482
|
});
|
|
481
483
|
const script = {
|
|
482
484
|
id,
|
|
483
485
|
status: "awaitingLoad",
|
|
484
|
-
loaded: false,
|
|
485
486
|
remove() {
|
|
486
487
|
if (script.entry) {
|
|
487
488
|
script.entry.dispose();
|
|
@@ -494,8 +495,16 @@ function useScript(_input, _options) {
|
|
|
494
495
|
load() {
|
|
495
496
|
if (!script.entry) {
|
|
496
497
|
syncStatus("loading");
|
|
498
|
+
const defaults = {
|
|
499
|
+
defer: true,
|
|
500
|
+
fetchpriority: "low"
|
|
501
|
+
};
|
|
502
|
+
if (input.src && (input.src.startsWith("http") || input.src.startsWith("//"))) {
|
|
503
|
+
defaults.crossorigin = "anonymous";
|
|
504
|
+
defaults.referrerpolicy = "no-referrer";
|
|
505
|
+
}
|
|
497
506
|
script.entry = head.push({
|
|
498
|
-
script: [{
|
|
507
|
+
script: [{ ...defaults, ...input, key: `script.${id}` }]
|
|
499
508
|
}, options);
|
|
500
509
|
}
|
|
501
510
|
return loadPromise;
|
|
@@ -508,21 +517,6 @@ function useScript(_input, _options) {
|
|
|
508
517
|
trigger.then(script.load);
|
|
509
518
|
else if (typeof trigger === "function")
|
|
510
519
|
trigger(script.load);
|
|
511
|
-
const removeHook = head.hooks.hook("dom:renderTag", (ctx) => {
|
|
512
|
-
if (ctx.tag.key !== key)
|
|
513
|
-
return;
|
|
514
|
-
if (ctx.tag.innerHTML) {
|
|
515
|
-
setTimeout(
|
|
516
|
-
() => {
|
|
517
|
-
syncStatus("loaded");
|
|
518
|
-
typeof input.onload === "function" && input.onload.call(options.eventContext, new Event("load"));
|
|
519
|
-
},
|
|
520
|
-
5
|
|
521
|
-
/* give inline script a chance to run */
|
|
522
|
-
);
|
|
523
|
-
}
|
|
524
|
-
removeHook();
|
|
525
|
-
});
|
|
526
520
|
const instance = new Proxy({}, {
|
|
527
521
|
get(_, fn) {
|
|
528
522
|
const $script = Object.assign(loadPromise, script);
|
|
@@ -531,13 +525,16 @@ function useScript(_input, _options) {
|
|
|
531
525
|
return stub;
|
|
532
526
|
if (fn === "$script")
|
|
533
527
|
return $script;
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
head.hooks.callHook("script:instance-fn", hookCtx2);
|
|
537
|
-
if (head.ssr || !options.use)
|
|
528
|
+
const attempt = (args) => {
|
|
529
|
+
if (head.ssr)
|
|
538
530
|
return;
|
|
539
|
-
|
|
531
|
+
const 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
|
+
return exists && api[fn];
|
|
540
536
|
};
|
|
537
|
+
return attempt() || ((...args) => loadPromise.then(() => attempt(args)(...args)));
|
|
541
538
|
}
|
|
542
539
|
});
|
|
543
540
|
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.
|
|
4
|
+
"version": "1.9.6",
|
|
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.
|
|
38
|
-
"@unhead/schema": "1.9.
|
|
39
|
-
"@unhead/shared": "1.9.
|
|
37
|
+
"@unhead/dom": "1.9.6",
|
|
38
|
+
"@unhead/schema": "1.9.6",
|
|
39
|
+
"@unhead/shared": "1.9.6"
|
|
40
40
|
},
|
|
41
41
|
"scripts": {
|
|
42
42
|
"build": "unbuild .",
|