@@ -26,9 +26,10 @@ export interface UseScriptOptions<T extends Record<symbol | string, any> = Recor
2626 * - `Promise` - Load the script when the promise resolves, exists only on the client.
2727 * - `Function` - Register a callback function to load the script, exists only on the client.
2828 * - `server` - Have the script injected on the server.
29- * - `ref` - Load the script when the ref is true.
29+ * - `Ref<boolean>` - Load the script when the ref becomes true.
30+ * - `() => boolean` - Getter function, load the script when return value becomes true.
3031 */
31- trigger ?: BaseUseScriptOptions [ 'trigger' ] | Ref < boolean >
32+ trigger ?: BaseUseScriptOptions [ 'trigger' ] | Ref < boolean > | ( ( ) => boolean )
3233 /**
3334 * Unhead instance.
3435 */
@@ -78,11 +79,14 @@ export function useScript<T extends Record<symbol | string, any> = Record<symbol
7879 if ( scope && typeof options . trigger === 'undefined' ) {
7980 options . trigger = onMounted
8081 }
81- else if ( isRef ( options . trigger ) ) {
82- const refTrigger = options . trigger as Ref < boolean >
82+ else if ( isRef ( options . trigger ) || ( typeof options . trigger === 'function' && options . trigger . length === 0 ) ) {
83+ // Handle refs, computed refs, and getter functions (zero-arg functions that return boolean)
84+ // Getter functions like `() => shouldLoad.value` have length 0
85+ // Callback functions like `(load) => onMounted(load)` have length 1+
86+ const trigger = options . trigger as Ref < boolean > | ( ( ) => boolean )
8387 let off : WatchHandle
8488 options . trigger = new Promise < boolean > ( ( resolve ) => {
85- off = watch ( refTrigger , ( val ) => {
89+ off = watch ( trigger , ( val ) => {
8690 if ( val ) {
8791 resolve ( true )
8892 }
0 commit comments