You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
description: Control when scripts load with Nuxt Scripts' flexible trigger system.
3
4
---
4
5
5
-
Nuxt Scripts provides several ways to trigger the loading of scripts.
6
+
Nuxt Scripts provides a flexible trigger system to control when scripts load, helping you optimize performance by loading scripts at the right moment for your users.
6
7
7
-
::code-group
8
+
## Default: onNuxtReady
8
9
9
-
```ts [useScript - Ref]
10
-
import { useTimeout } from'@vueuse/core'
10
+
By default, scripts use the `onNuxtReady` trigger, providing "idle loading" behavior where scripts load only after the page is fully interactive. This minimizes impact on Core Web Vitals and user experience.
11
11
12
-
const { ready } =useTimeout(3000)
13
-
useScript({
14
-
src: 'https://example.com/script.js',
15
-
}, {
16
-
// load however you like!
17
-
trigger: ready, // refs supported
12
+
The `onNuxtReady` trigger ensures scripts load:
13
+
- After Nuxt hydration is complete
14
+
- When the browser is idle and the main thread is available
15
+
- Without blocking critical page rendering or user interactions
By default, scripts are loaded when Nuxt is fully hydrated using the `onNuxtReady` trigger. This provides an "idle loading" behavior where scripts load only after the page is fully interactive, minimizing impact on Core Web Vitals and user experience.
61
-
62
-
The `onNuxtReady` trigger ensures scripts load:
63
-
- After Nuxt hydration is complete
64
-
- When the browser is idle and the main thread is available
65
-
- Without blocking critical page rendering or user interactions
66
-
67
-
This is more effective than using `defer` or `fetchpriority="low"` attributes alone, as it waits for the application to be fully ready rather than just the HTML parsing to complete.
68
-
69
-
You can change this default by modifying the [defaultScriptOptions](/docs/api/nuxt-config#defaultscriptoptions).
62
+
### User Interaction
70
63
71
-
## Idle Loading with onNuxtReady
64
+
Use [useScriptTriggerInteraction](/docs/api/use-script-trigger-interaction) to load scripts when users interact with your site:
72
65
73
-
The `onNuxtReady` trigger is perfect for non-critical scripts like chat widgets, analytics, or marketing tools that should load with minimal performance impact:
66
+
::code-group
74
67
75
-
```ts
76
-
// Chat widget - loads after page is fully interactive
This approach ensures your Core Web Vitals remain optimal while still loading necessary third-party scripts.
88
+
::
91
89
92
-
## Element Event Triggers
90
+
###Element Events
93
91
94
-
The[useScriptTriggerElement](/docs/api/use-script-trigger-element)composable allows you to hook into element events as a way to load script. This is useful for loading scripts when a user interacts with a specific element.
92
+
Use[useScriptTriggerElement](/docs/api/use-script-trigger-element) to trigger scripts based on specific element interactions:
95
93
96
94
```ts
97
-
const somethingEl =ref<HTMLElement>()
98
-
const script =useScript({
99
-
src: 'https://example.com/script.js',
100
-
}, {
95
+
const buttonEl =ref<HTMLElement>()
96
+
97
+
useScript('https://example.com/feature.js', {
101
98
trigger: useScriptTriggerElement({
102
-
trigger: 'hover',
103
-
el: somethingEl,
99
+
trigger: 'visible', // or 'hover', 'click', etc.
100
+
el: buttonEl,
104
101
})
105
102
})
106
103
```
107
104
108
-
It has support for the following triggers:
109
-
-`visible` - Triggered when the element becomes visible in the viewport.
110
-
-`mouseover` - Triggered when the element is hovered over.
105
+
## Basic Triggers
111
106
112
-
## Manual Trigger
107
+
###Manual Control
113
108
114
-
The `manual` trigger allows you to manually trigger the loading of a script. This gives you complete
115
-
control over when the script is loaded.
109
+
Use `manual` trigger for complete control over script loading:
description: API documentation for the useScriptTriggerIdleTimeout function.
4
+
links:
5
+
- label: Source
6
+
icon: i-simple-icons-github
7
+
to: https://github.com/nuxt/scripts/blob/main/src/runtime/composables/useScriptTriggerIdleTimeout.ts
8
+
size: xs
9
+
---
10
+
11
+
Create a trigger that loads a script after an idle timeout once Nuxt is ready. This is useful for non-critical scripts that should load with a delay to further minimize impact on initial page performance.
12
+
13
+
## Signature
14
+
15
+
```ts
16
+
function useScriptTriggerIdleTimeout(options:IdleTimeoutScriptTriggerOptions):Promise<boolean>
0 commit comments