@@ -3,7 +3,7 @@ import type { UseScriptOptions, UseFunctionType, AsAsyncFunctionValues } from '@
3
3
import { resolveScriptKey } from 'unhead'
4
4
import { defu } from 'defu'
5
5
import { useScript as _useScript } from '@unhead/vue'
6
- import { injectHead , onNuxtReady , useNuxtApp , useRuntimeConfig , reactive } from '#imports'
6
+ import { injectHead , onNuxtReady , useHead , useNuxtApp , useRuntimeConfig , reactive } from '#imports'
7
7
import type { NuxtDevToolsScriptInstance , NuxtUseScriptOptions } from '#nuxt-scripts'
8
8
9
9
function useNuxtScriptRuntimeConfig ( ) {
@@ -25,11 +25,28 @@ export type UseScriptContext<T extends Record<symbol | string, any>> =
25
25
export function useScript < T extends Record < symbol | string , any > = Record < symbol | string , any > , U = Record < symbol | string , any > > ( input : UseScriptInput , options ?: NuxtUseScriptOptions < T , U > ) : UseScriptContext < UseFunctionType < NuxtUseScriptOptions < T , U > , T > > {
26
26
input = typeof input === 'string' ? { src : input } : input
27
27
options = defu ( options , useNuxtScriptRuntimeConfig ( ) ?. defaultScriptOptions ) as NuxtUseScriptOptions < T , U >
28
-
29
- if ( options . trigger === 'onNuxtReady' )
28
+ // browser hint optimizations
29
+ const rel = options . trigger === 'onNuxtReady' ? 'preload' : 'preconnect'
30
+ const id = resolveScriptKey ( input ) as keyof typeof nuxtApp . _scripts
31
+ if ( options . trigger !== 'server' && ( rel === 'preload' || ! input . src . startsWith ( '/' ) ) ) {
32
+ useHead ( {
33
+ link : [
34
+ {
35
+ rel,
36
+ as : rel === 'preload' ? 'script' : undefined ,
37
+ href : input . src ,
38
+ crossorigin : typeof input . crossorigin !== 'undefined' ? input . crossorigin : 'anonymous' ,
39
+ key : `nuxt-script-${ id } ` ,
40
+ tagPriority : rel === 'preload' ? 'high' : 0 ,
41
+ fetchpriority : 'low' ,
42
+ } ,
43
+ ] ,
44
+ } )
45
+ }
46
+ if ( options . trigger === 'onNuxtReady' ) {
30
47
options . trigger = onNuxtReady
48
+ }
31
49
const nuxtApp = useNuxtApp ( )
32
- const id = resolveScriptKey ( input ) as keyof typeof nuxtApp . _scripts
33
50
nuxtApp . $scripts = nuxtApp . $scripts ! || reactive ( { } )
34
51
const exists = ! ! ( nuxtApp . $scripts as Record < string , any > ) ?. [ id ]
35
52
if ( import . meta. client ) {
0 commit comments