@@ -21,7 +21,10 @@ export function useScript<T extends Record<symbol | string, any> = Record<symbol
2121 // browser hint optimizations
2222 const id = String ( resolveScriptKey ( input ) as keyof typeof nuxtApp . _scripts )
2323 const nuxtApp = useNuxtApp ( )
24- const head = options . head || injectHead ( )
24+ options . head = options . head || injectHead ( )
25+ if ( ! options . head ) {
26+ throw new Error ( 'useScript() has been called without Nuxt context.' )
27+ }
2528 nuxtApp . $scripts = nuxtApp . $scripts ! || reactive ( { } )
2629 const exists = ! ! ( nuxtApp . $scripts as Record < string , any > ) ?. [ id ]
2730
@@ -61,7 +64,7 @@ export function useScript<T extends Record<symbol | string, any> = Record<symbol
6164 }
6265
6366 if ( ! nuxtApp . _scripts [ instance . id ] ) {
64- head . hooks . hook ( 'script:updated' , ( ctx ) => {
67+ options . head . hooks . hook ( 'script:updated' , ( ctx ) => {
6568 if ( ctx . script . id !== instance . id )
6669 return
6770 // convert the status to a timestamp
@@ -74,7 +77,7 @@ export function useScript<T extends Record<symbol | string, any> = Record<symbol
7477 syncScripts ( )
7578 } )
7679 // @ts -expect-error untyped
77- head . hooks . hook ( 'script:instance-fn' , ( ctx ) => {
80+ options . head . hooks . hook ( 'script:instance-fn' , ( ctx ) => {
7881 if ( ctx . script . id !== instance . id || String ( ctx . fn ) . startsWith ( '__v_' ) )
7982 return
8083 // log all events
0 commit comments