<Script> component gives you control over when third-party scripts load, helping you improve performance without manually managing <script> tag placement.
app/dashboard/page.js
Props
The URL of the external script to load. Required unless you are using an inline script.
Controls when the script loads. Four strategies are available:
"beforeInteractive"— loads before any Next.js code and before page hydration"afterInteractive"— (default) loads early, after some hydration has occurred"lazyOnload"— loads during browser idle time, after all other resources"worker"— (experimental) offloads the script to a web worker
Called once after the script finishes loading. Use to run initialization code that depends on the script.Only works with
afterInteractive and lazyOnload strategies. Requires a Client Component.Called after the script loads and on every subsequent component mount (for example, after client-side navigation). Useful for scripts that need to reinitialize on navigation.Requires a Client Component.
Called if the script fails to load. Cannot be used with
beforeInteractive. Requires a Client Component.Loading strategies
beforeInteractive
Injects the script into the initial HTML from the server. The script downloads before any Next.js module but does not block hydration.
Must be placed in the root layout (app/layout.js). Use only for critical scripts needed site-wide, such as bot detectors and cookie consent managers.
app/layout.js
Scripts with
beforeInteractive are always injected into the <head> regardless of their placement in the component tree.afterInteractive
The default strategy. Injects the script client-side after some hydration has occurred. Good for tag managers and analytics.
app/page.js
lazyOnload
Injects the script client-side during browser idle time, after all other resources have loaded. Use for low-priority scripts like chat widgets and social media embeds.
app/page.js
worker (experimental)
Offloads the script to a web worker to keep the main thread free for first-party code. Requires enabling nextScriptWorkers in next.config.js:
next.config.js
pages/ directory:
pages/home.js
Inline scripts
For inline scripts, use theid prop so Next.js can track and optimize the script:
dangerouslySetInnerHTML:
Version history
| Version | Changes |
|---|---|
v13.0.0 | beforeInteractive and afterInteractive updated to support App Router |
v12.2.4 | onReady prop added |
v12.2.2 | beforeInteractive supported in _document |
v11.0.0 | next/script introduced |
