Skip to content

Commit

Permalink
fix(scripts): avoid vue overriding status with ref
Browse files Browse the repository at this point in the history
  • Loading branch information
harlan-zw committed Aug 20, 2024
1 parent 6efcfa1 commit ecc7e11
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
3 changes: 1 addition & 2 deletions packages/unhead/src/composables/useScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ export function useScript<T extends Record<symbol | string, any> = Record<symbol
const emit = (api: T) => requestAnimationFrame(() => resolve(api))
const _ = head.hooks.hook('script:updated', ({ script }) => {
// vue augmentation... not ideal
// @ts-expect-error runtime augmentation
const status = typeof script.status === 'object' ? script.status.value : script.status
const status = script.status
if (script.id === id && (status === 'loaded' || status === 'error')) {
if (status === 'loaded') {
if (typeof options.use === 'function') {
Expand Down
10 changes: 8 additions & 2 deletions packages/vue/src/composables/useScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ export function useScript<T extends Record<symbol | string, any> = Record<symbol
})
script = _useScript(input as BaseUseScriptInput, options) as any as UseScriptContext<T>
// Note: we don't remove scripts on unmount as it's not a common use case and reloading the script may be expensive
script.status = status
if (scope) {
const _registerCb = (key: 'loaded' | 'error', cb: any) => {
if (!script._cbs[key]) {
Expand All @@ -83,5 +82,12 @@ export function useScript<T extends Record<symbol | string, any> = Record<symbol
script.onLoaded = (cb: (instance: T) => void | Promise<void>) => _registerCb('loaded', cb)
script.onError = (cb: (err?: Error) => void | Promise<void>) => _registerCb('error', cb)
}
return script
return new Proxy(script, {
get(_, key, a) {
// we can't override status as there's a race condition
if (key === 'status')
return status
return Reflect.get(_, key, a)
},
})
}

0 comments on commit ecc7e11

Please sign in to comment.