Skip to content

Commit 843e00c

Browse files
committed
fix(useScript): avoid calling proxy functions before loaded
1 parent d5520c8 commit 843e00c

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

packages/unhead/src/composables/useScript.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ export function useScript<T extends Record<symbol | string, any> = Record<symbol
190190
// script is ready
191191
loadPromise
192192
.then((api) => {
193-
if (api) {
193+
if (api !== false) {
194194
script.instance = api
195195
_cbs.loaded?.forEach(cb => cb(api))
196196
_cbs.loaded = null
@@ -237,10 +237,11 @@ export function useScript<T extends Record<symbol | string, any> = Record<symbol
237237
}
238238
let fn = access(script.instance)
239239
if (!fn) {
240-
const instance = await loadPromise
241-
if (instance !== false) {
242-
fn = access(instance)
243-
}
240+
fn = await (new Promise<T | undefined>((resolve) => {
241+
script.onLoaded(api => {
242+
resolve(access(api))
243+
})
244+
}))
244245
}
245246
return typeof fn === 'function' ? Reflect.apply(fn, instance, args) : fn
246247
},

0 commit comments

Comments
 (0)