Script component provides optimized loading strategies for third-party scripts (analytics, ads, widgets) with control over when and how they execute.
Import
Basic Usage
API Reference
Props
string
Script source URL.
'beforeInteractive' | 'afterInteractive' | 'lazyOnload' | 'worker'
default:"'afterInteractive'"
Loading strategy that controls when the script executes.
beforeInteractive— Load before page becomes interactive (SSR output)afterInteractive— Load after page becomes interactive (default)lazyOnload— Load during idle time (afterwindow.load)worker— Load in a web worker (requires Partytown)
(e: Event) => void
Callback invoked when the script has loaded.
() => void
Callback invoked when the script is ready.Called after
onLoad, and also on every re-render if the script is already loaded.(e: Event) => void
Callback invoked when the script fails to load.
string
Unique identifier for the script (prevents duplicate loading).
string
Inline script content.
{ __html: string }
Alternative to
children for inline scripts.Standard Script Attributes
All standard<script> attributes are supported:
string
Script MIME type (e.g.,
"module", "text/partytown").boolean
Load script asynchronously.
boolean
Defer script execution.
string
CORS setting (
"anonymous" or "use-credentials").string
Nonce for Content Security Policy.
string
Subresource Integrity hash.
Loading Strategies
beforeInteractive
Load the script before the page becomes interactive. Rendered in SSR output. Use for:- Critical polyfills
- Feature detection scripts
- Scripts that must run before React hydration
afterInteractive (Default)
Load the script after the page becomes interactive. Use for:- Analytics scripts
- Tag managers
- Chat widgets
lazyOnload
Load the script during idle time (afterwindow.load + requestIdleCallback).
Use for:
- Non-critical scripts
- Ads
- Social media widgets
- Anything that can wait
worker (Partytown)
Load the script in a web worker using Partytown. Use for:- Heavy analytics scripts
- Anything that blocks the main thread
Requires Partytown setup. Sets
type="text/partytown" on the script tag.Examples
Google Analytics
Facebook Pixel
External SDK
Inline Configuration
Deduplication
Scripts with the sameid or src are only loaded once:
SSR Behavior
beforeInteractive: Rendered in SSR HTML outputafterInteractive: Not rendered (injected client-side)lazyOnload: Not rendered (injected client-side)worker: Not rendered (injected client-side)
Performance
Impact on Core Web Vitals
Best Practices
- Use
lazyOnloadfor non-critical scripts - Minimize
beforeInteractiveusage (blocks hydration) - Use
workerfor heavy analytics (requires Partytown) - Always provide
idfor inline scripts (enables deduplication) - Avoid blocking the main thread
Limitations
Migration from HTML script tag
Source
View source code → Implementation:/home/daytona/workspace/source/packages/vinext/src/shims/script.tsx