Click me to load the script
```
--------------------------------
### Run migration CLI
Source: https://scripts.nuxt.com/docs/migration-guide/v1-to-v2.md
Use the migration CLI to update the project. The --dry-run flag allows previewing changes before applying them.
```bash
npx @nuxt/scripts-cli migrate v2 --dry-run
npx @nuxt/scripts-cli migrate v2
```
--------------------------------
### Load js-confetti in a component with useScriptNpm
Source: https://scripts.nuxt.com/docs/getting-started/confetti-tutorial.md
Call useScriptNpm inside a Vue component's setup script to load the js-confetti browser file. After this, the browser's Network panel should show the script request.
```vue
```
--------------------------------
### Define a global script as a URL string
Source: https://scripts.nuxt.com/docs/guides/global.md
Configure a global script by mapping a key to its source URL.
```ts
export default defineNuxtConfig({
scripts: {
globals: {
myScript: 'https://analytics.com/tracker.js',
}
}
})
```
--------------------------------
### Migrate custom readiness callbacks to resolve
Source: https://scripts.nuxt.com/docs/migration-guide/v1-to-v2.md
Replace the deprecated use option with the resolve option using the waitFor helper to manage script lifecycle and readiness.
```diff
const script = useScript('https://example.com/sdk.js', {
- use: () => readyPromise.then(() => window.example),
+ resolve: ({ waitFor }) => waitFor((resolve) => {
+ window.onExampleReady = () => resolve(window.example)
+ return () => delete window.onExampleReady
+ }),
})
```
--------------------------------
### Bind third-party consent manager to trigger
Source: https://scripts.nuxt.com/docs/guides/first-party.md
Binds a third-party consent manager's signal (OneTrust example) to a reactive ref and passes it to `useScriptTriggerConsent()`. The Google Analytics script loads only when the consent signal becomes true.
```vue
```
--------------------------------
### Create custom composable for shared config (useFathomAnalytics)
Source: https://scripts.nuxt.com/docs/guides/registry-scripts.md
Keep shared configuration in your own composable to reuse the same script instance across pages.
```ts
export function useFathomAnalytics() {
return useScriptFathomAnalytics({
site: 'SITE_ID',
})
}
```
--------------------------------
### Error details hidden without crossorigin
Source: https://scripts.nuxt.com/docs/guides/cors.md
Without `crossorigin`, browsers hide error details from external scripts for security. This example shows that `window.onerror` receives only "Script error." instead of the actual error.
```js
window.onerror = msg => console.log(msg)
// Shows: "Script error." instead of actual error
```
--------------------------------
### Guard GTM initialization
Source: https://scripts.nuxt.com/docs/migration-guide/v0-to-v1.md
Use a flag to prevent repeated initialization in the onBeforeGtmStart callback.
```diff
+let initialized = false
useScriptGoogleTagManager({
onBeforeGtmStart: (gtag) => {
+ if (initialized) return
+ initialized = true
// your init code
}
})
```
--------------------------------
### Binary load gate with useScriptTriggerConsent
Source: https://scripts.nuxt.com/docs/guides/consent.md
Sets up a binary consent trigger shared across a main-thread script and a cookie banner. The trigger is created in utils/cookie.ts, passed to useScript() so the analytics script waits for acceptance, and the banner calls scriptsConsent.accept() to grant consent.
```ts
export const scriptsConsent = useScriptTriggerConsent()
```
```vue
```
```vue