@unhead/vue 1.10.0 → 1.10.2

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
@@ -94,18 +94,48 @@ function useServerSeoMeta(input, options) {
94
94
  return useSeoMeta(input, { ...options, mode: "server" });
95
95
  }
96
96
 
97
+ function registerVueScopeHandlers(script, scope) {
98
+ if (!scope) {
99
+ return;
100
+ }
101
+ const _registerCb = (key, cb) => {
102
+ if (!script._cbs[key]) {
103
+ cb(script.instance);
104
+ return () => {
105
+ };
106
+ }
107
+ let i = script._cbs[key].push(cb);
108
+ const destroy = () => {
109
+ if (i) {
110
+ script._cbs[key]?.splice(i - 1, 1);
111
+ i = null;
112
+ }
113
+ };
114
+ vue.onScopeDispose(destroy);
115
+ return destroy;
116
+ };
117
+ vue.onScopeDispose(() => {
118
+ script._triggerAbortController?.abort();
119
+ });
120
+ script.onLoaded = (cb) => _registerCb("loaded", cb);
121
+ script.onError = (cb) => _registerCb("error", cb);
122
+ }
97
123
  function useScript(_input, _options) {
98
124
  const input = typeof _input === "string" ? { src: _input } : _input;
99
125
  const head = injectHead.injectHead();
100
126
  const options = _options || {};
101
127
  options.head = head;
102
- options.eventContext = vue.getCurrentInstance();
103
128
  const scope = vue.getCurrentInstance();
104
- if (scope && !options.trigger)
129
+ options.eventContext = scope;
130
+ if (scope && typeof options.trigger === "undefined")
105
131
  options.trigger = vue.onMounted;
106
- const key = unhead.resolveScriptKey(input);
107
- if (head._scripts?.[key])
108
- return head._scripts[key];
132
+ const id = unhead.resolveScriptKey(input);
133
+ const prevScript = head._scripts?.[id];
134
+ if (prevScript) {
135
+ prevScript.updateTrigger(options.trigger);
136
+ registerVueScopeHandlers(prevScript, scope);
137
+ return prevScript;
138
+ }
109
139
  let script;
110
140
  const status = vue.ref("awaitingLoad");
111
141
  const _ = head.hooks.hook("script:updated", ({ script: s }) => {
@@ -117,31 +147,12 @@ function useScript(_input, _options) {
117
147
  }
118
148
  });
119
149
  script = unhead.useScript(input, options);
120
- if (scope) {
121
- const _registerCb = (key2, cb) => {
122
- if (!script._cbs[key2]) {
123
- cb(script.instance);
124
- return () => {
125
- };
126
- }
127
- let i = script._cbs[key2].push(cb);
128
- const destroy = () => {
129
- if (i) {
130
- script._cbs[key2]?.splice(i - 1, 1);
131
- i = null;
132
- }
133
- };
134
- vue.onScopeDispose(destroy);
135
- return destroy;
136
- };
137
- script.onLoaded = (cb) => _registerCb("loaded", cb);
138
- script.onError = (cb) => _registerCb("error", cb);
139
- }
150
+ registerVueScopeHandlers(script, scope);
140
151
  return new Proxy(script, {
141
- get(_2, key2, a) {
142
- if (key2 === "status")
152
+ get(_2, key, a) {
153
+ if (key === "status")
143
154
  return status;
144
- return Reflect.get(_2, key2, a);
155
+ return Reflect.get(_2, key, a);
145
156
  }
146
157
  });
147
158
  }
package/dist/index.mjs CHANGED
@@ -94,18 +94,48 @@ function useServerSeoMeta(input, options) {
94
94
  return useSeoMeta(input, { ...options, mode: "server" });
95
95
  }
96
96
 
97
+ function registerVueScopeHandlers(script, scope) {
98
+ if (!scope) {
99
+ return;
100
+ }
101
+ const _registerCb = (key, cb) => {
102
+ if (!script._cbs[key]) {
103
+ cb(script.instance);
104
+ return () => {
105
+ };
106
+ }
107
+ let i = script._cbs[key].push(cb);
108
+ const destroy = () => {
109
+ if (i) {
110
+ script._cbs[key]?.splice(i - 1, 1);
111
+ i = null;
112
+ }
113
+ };
114
+ onScopeDispose(destroy);
115
+ return destroy;
116
+ };
117
+ onScopeDispose(() => {
118
+ script._triggerAbortController?.abort();
119
+ });
120
+ script.onLoaded = (cb) => _registerCb("loaded", cb);
121
+ script.onError = (cb) => _registerCb("error", cb);
122
+ }
97
123
  function useScript(_input, _options) {
98
124
  const input = typeof _input === "string" ? { src: _input } : _input;
99
125
  const head = injectHead();
100
126
  const options = _options || {};
101
127
  options.head = head;
102
- options.eventContext = getCurrentInstance();
103
128
  const scope = getCurrentInstance();
104
- if (scope && !options.trigger)
129
+ options.eventContext = scope;
130
+ if (scope && typeof options.trigger === "undefined")
105
131
  options.trigger = onMounted;
106
- const key = resolveScriptKey(input);
107
- if (head._scripts?.[key])
108
- return head._scripts[key];
132
+ const id = resolveScriptKey(input);
133
+ const prevScript = head._scripts?.[id];
134
+ if (prevScript) {
135
+ prevScript.updateTrigger(options.trigger);
136
+ registerVueScopeHandlers(prevScript, scope);
137
+ return prevScript;
138
+ }
109
139
  let script;
110
140
  const status = ref("awaitingLoad");
111
141
  const _ = head.hooks.hook("script:updated", ({ script: s }) => {
@@ -117,31 +147,12 @@ function useScript(_input, _options) {
117
147
  }
118
148
  });
119
149
  script = useScript$1(input, options);
120
- if (scope) {
121
- const _registerCb = (key2, cb) => {
122
- if (!script._cbs[key2]) {
123
- cb(script.instance);
124
- return () => {
125
- };
126
- }
127
- let i = script._cbs[key2].push(cb);
128
- const destroy = () => {
129
- if (i) {
130
- script._cbs[key2]?.splice(i - 1, 1);
131
- i = null;
132
- }
133
- };
134
- onScopeDispose(destroy);
135
- return destroy;
136
- };
137
- script.onLoaded = (cb) => _registerCb("loaded", cb);
138
- script.onError = (cb) => _registerCb("error", cb);
139
- }
150
+ registerVueScopeHandlers(script, scope);
140
151
  return new Proxy(script, {
141
- get(_2, key2, a) {
142
- if (key2 === "status")
152
+ get(_2, key, a) {
153
+ if (key === "status")
143
154
  return status;
144
- return Reflect.get(_2, key2, a);
155
+ return Reflect.get(_2, key, a);
145
156
  }
146
157
  });
147
158
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@unhead/vue",
3
3
  "type": "module",
4
- "version": "1.10.0",
4
+ "version": "1.10.2",
5
5
  "author": "Harlan Wilton <harlan@harlanzw.com>",
6
6
  "license": "MIT",
7
7
  "funding": "https://github.com/sponsors/harlan-zw",
@@ -66,9 +66,9 @@
66
66
  },
67
67
  "dependencies": {
68
68
  "hookable": "^5.5.3",
69
- "unhead": "1.10.0",
70
- "@unhead/schema": "1.10.0",
71
- "@unhead/shared": "1.10.0"
69
+ "@unhead/schema": "1.10.2",
70
+ "@unhead/shared": "1.10.2",
71
+ "unhead": "1.10.2"
72
72
  },
73
73
  "scripts": {
74
74
  "build": "unbuild .",