@@ -21,7 +21,10 @@ export function useScript<T extends Record<symbol | string, any> = Record<symbol
21
21
// browser hint optimizations
22
22
const id = String ( resolveScriptKey ( input ) as keyof typeof nuxtApp . _scripts )
23
23
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
+ }
25
28
nuxtApp . $scripts = nuxtApp . $scripts ! || reactive ( { } )
26
29
const exists = ! ! ( nuxtApp . $scripts as Record < string , any > ) ?. [ id ]
27
30
@@ -61,7 +64,7 @@ export function useScript<T extends Record<symbol | string, any> = Record<symbol
61
64
}
62
65
63
66
if ( ! nuxtApp . _scripts [ instance . id ] ) {
64
- head . hooks . hook ( 'script:updated' , ( ctx ) => {
67
+ options . head . hooks . hook ( 'script:updated' , ( ctx ) => {
65
68
if ( ctx . script . id !== instance . id )
66
69
return
67
70
// convert the status to a timestamp
@@ -74,7 +77,7 @@ export function useScript<T extends Record<symbol | string, any> = Record<symbol
74
77
syncScripts ( )
75
78
} )
76
79
// @ts -expect-error untyped
77
- head . hooks . hook ( 'script:instance-fn' , ( ctx ) => {
80
+ options . head . hooks . hook ( 'script:instance-fn' , ( ctx ) => {
78
81
if ( ctx . script . id !== instance . id || String ( ctx . fn ) . startsWith ( '__v_' ) )
79
82
return
80
83
// log all events
0 commit comments