Skip to content

Commit

Permalink
fix(useScript): don't throw exceptions on failures
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Oct 14, 2024
1 parent e776256 commit 8fd2a67
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions packages/unhead/src/composables/useScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export function useScript<T extends Record<symbol | string, any> = Record<symbol
cb(script.instance)
return () => {}
}
const loadPromise = new Promise<T>((resolve, reject) => {
const loadPromise = new Promise<T | false>((resolve) => {
// promise never resolves
if (head.ssr)
return
Expand All @@ -91,7 +91,7 @@ export function useScript<T extends Record<symbol | string, any> = Record<symbol
}
}
else if (status === 'error') {
reject(new Error(`Failed to load script: ${input.src}`))
resolve(false) // failed to load
}
_()
}
Expand Down Expand Up @@ -176,15 +176,15 @@ export function useScript<T extends Record<symbol | string, any> = Record<symbol
// script is ready
loadPromise
.then((api) => {
script.instance = api
if (_cbs.loaded)
_cbs.loaded.forEach(cb => cb(api))
_cbs.loaded = null
})
.catch((err) => {
if (_cbs.error)
_cbs.error.forEach(cb => cb(err))
_cbs.error = null
if (api) {
script.instance = api
_cbs.loaded?.forEach(cb => cb(api))
_cbs.loaded = null
}
else {
_cbs.error?.forEach(cb => cb())
_cbs.error = null
}
})
const hookCtx = { script }

Expand Down Expand Up @@ -221,7 +221,13 @@ export function useScript<T extends Record<symbol | string, any> = Record<symbol
}
return fn
}
const fn = access(script.instance) || access(await loadPromise)
let fn = access(script.instance)
if (!fn) {
const instance = await loadPromise
if (instance !== false) {
fn = access(instance)
}
}
return typeof fn === 'function' ? Reflect.apply(fn, instance, args) : fn
},
})
Expand Down

0 comments on commit 8fd2a67

Please sign in to comment.