@unhead/svelte 2.0.0-alpha.17 → 2.0.0-alpha.19

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.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { UseHeadInput, HeadEntryOptions, ActiveHeadEntry, HeadSafe, UseSeoMetaInput, Unhead, Head } from 'unhead/types';
1
+ import { UseHeadInput, HeadEntryOptions, ActiveHeadEntry, HeadSafe, UseScriptInput, UseScriptOptions, UseScriptReturn, UseSeoMetaInput, Unhead } from 'unhead/types';
2
2
  export { UnheadContextKey } from './server.mjs';
3
3
  import 'unhead/server';
4
4
 
@@ -6,9 +6,10 @@ declare const autoImports: {
6
6
  '@unhead/svelte': string[];
7
7
  };
8
8
 
9
- declare function useUnhead(): Unhead<Head>;
9
+ declare function useUnhead(): Unhead;
10
10
  declare function useHead(input?: UseHeadInput, options?: HeadEntryOptions): ActiveHeadEntry<UseHeadInput>;
11
11
  declare function useHeadSafe(input?: HeadSafe, options?: HeadEntryOptions): ActiveHeadEntry<HeadSafe>;
12
12
  declare function useSeoMeta(input?: UseSeoMetaInput, options?: HeadEntryOptions): ActiveHeadEntry<UseSeoMetaInput>;
13
+ declare function useScript<T extends Record<symbol | string, any> = Record<symbol | string, any>>(_input: UseScriptInput, _options?: UseScriptOptions<T>): UseScriptReturn<T>;
13
14
 
14
- export { autoImports, useHead, useHeadSafe, useSeoMeta, useUnhead };
15
+ export { autoImports, useHead, useHeadSafe, useScript, useSeoMeta, useUnhead };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { UseHeadInput, HeadEntryOptions, ActiveHeadEntry, HeadSafe, UseSeoMetaInput, Unhead, Head } from 'unhead/types';
1
+ import { UseHeadInput, HeadEntryOptions, ActiveHeadEntry, HeadSafe, UseScriptInput, UseScriptOptions, UseScriptReturn, UseSeoMetaInput, Unhead } from 'unhead/types';
2
2
  export { UnheadContextKey } from './server.js';
3
3
  import 'unhead/server';
4
4
 
@@ -6,9 +6,10 @@ declare const autoImports: {
6
6
  '@unhead/svelte': string[];
7
7
  };
8
8
 
9
- declare function useUnhead(): Unhead<Head>;
9
+ declare function useUnhead(): Unhead;
10
10
  declare function useHead(input?: UseHeadInput, options?: HeadEntryOptions): ActiveHeadEntry<UseHeadInput>;
11
11
  declare function useHeadSafe(input?: HeadSafe, options?: HeadEntryOptions): ActiveHeadEntry<HeadSafe>;
12
12
  declare function useSeoMeta(input?: UseSeoMetaInput, options?: HeadEntryOptions): ActiveHeadEntry<UseSeoMetaInput>;
13
+ declare function useScript<T extends Record<symbol | string, any> = Record<symbol | string, any>>(_input: UseScriptInput, _options?: UseScriptOptions<T>): UseScriptReturn<T>;
13
14
 
14
- export { autoImports, useHead, useHeadSafe, useSeoMeta, useUnhead };
15
+ export { autoImports, useHead, useHeadSafe, useScript, useSeoMeta, useUnhead };
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
- import { getContext, onDestroy } from 'svelte';
2
- import { useHead as useHead$1, useHeadSafe as useHeadSafe$1, useSeoMeta as useSeoMeta$1 } from 'unhead';
1
+ import { onMount, onDestroy, getContext } from 'svelte';
2
+ import { useHead as useHead$1, useHeadSafe as useHeadSafe$1, useScript as useScript$1, useSeoMeta as useSeoMeta$1 } from 'unhead';
3
3
  import { U as UnheadContextKey } from './shared/svelte.Cczd8v9v.mjs';
4
4
 
5
5
  const autoImports = {
@@ -34,5 +34,39 @@ function useHeadSafe(input = {}, options = {}) {
34
34
  function useSeoMeta(input = {}, options = {}) {
35
35
  return withSideEffects(useSeoMeta$1(options.head || useUnhead(), input, options));
36
36
  }
37
+ function useScript(_input, _options) {
38
+ const input = typeof _input === "string" ? { src: _input } : _input;
39
+ const options = _options || {};
40
+ const head = options?.head || useUnhead();
41
+ options.head = head;
42
+ if (typeof options.trigger === "undefined") {
43
+ options.trigger = onMount;
44
+ }
45
+ const script = useScript$1(head, input, options);
46
+ const sideEffects = [];
47
+ const _registerCb = (key, cb) => {
48
+ if (!script._cbs[key]) {
49
+ cb(script.instance);
50
+ return () => {
51
+ };
52
+ }
53
+ let i = script._cbs[key].push(cb);
54
+ const destroy = () => {
55
+ if (i) {
56
+ script._cbs[key]?.splice(i - 1, 1);
57
+ i = null;
58
+ }
59
+ };
60
+ sideEffects.push(destroy);
61
+ return destroy;
62
+ };
63
+ script.onLoaded = (cb) => _registerCb("loaded", cb);
64
+ script.onError = (cb) => _registerCb("error", cb);
65
+ onDestroy(() => {
66
+ script._triggerAbortController?.abort();
67
+ sideEffects.forEach((i) => i());
68
+ });
69
+ return script;
70
+ }
37
71
 
38
- export { UnheadContextKey, autoImports, useHead, useHeadSafe, useSeoMeta, useUnhead };
72
+ export { UnheadContextKey, autoImports, useHead, useHeadSafe, useScript, useSeoMeta, useUnhead };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@unhead/svelte",
3
3
  "type": "module",
4
- "version": "2.0.0-alpha.17",
4
+ "version": "2.0.0-alpha.19",
5
5
  "author": "Harlan Wilton <harlan@harlanzw.com>",
6
6
  "license": "MIT",
7
7
  "funding": "https://github.com/sponsors/harlan-zw",
@@ -64,7 +64,7 @@
64
64
  ]
65
65
  },
66
66
  "dependencies": {
67
- "unhead": "2.0.0-alpha.17"
67
+ "unhead": "2.0.0-alpha.19"
68
68
  },
69
69
  "devDependencies": {
70
70
  "@testing-library/svelte": "^5.2.7",