--- title: useScript() description: Load third-party scripts with useScript(). Smart defaults for performance, lazy loading triggers, and API proxying for analytics and widgets. --- The `useScript` composable provides an enhanced developer experience for loading third-party scripts with intelligent defaults for performance, security, and lifecycle management. **Quick Start:** ```ts const { proxy, onLoaded } = useScript('https://example.com/analytics.js') // Call functions immediately (queued until loaded) proxy.track('pageview') // Or wait for script to load onLoaded(() => { console.log('Script ready!') }) ``` ## Basic Usage ```ts import { useScript } from '@unhead/dynamic-import' const { onLoaded } = useScript('https://example.com/script.js') onLoaded(() => { // Script loaded successfully console.log('Script is ready to use') }) ``` ## Smart Defaults A singleton pattern is implemented so scripts with the same `src` or `key` are only loaded once globally. This helps prevent duplicate script loading and ensures consistent initialization. The following defaults are applied for optimal performance and security: - Scripts load after hydration by default - `defer`{lang="html"} enabled for proper execution order - `fetchpriority="low"`{lang="html"} to prioritize critical resources - `crossorigin="anonymous"`{lang="html"} prevents cookie access - `referrerpolicy="no-referrer"`{lang="html"} blocks referrer headers ## Input Options ### Simple URL Pass a URL string for the quickest implementation: ```ts import { useScript } from '@unhead/dynamic-import' useScript('https://example.com/script.js') ``` ### Full Configuration Pass an options object to customize any `