Early initialization
A library required before application hydration, such as a script that must establish an early browser capability.
Next.js 16+ · Module
next/script is Next.js’s component for optimizing third-party and inline scripts. Configure its loading timing with strategy and respond to lifecycle events with onLoad, onReady, or onError.
Import Script from next/script and place it in a page or layout. Next.js manages when the script is fetched and executed through strategy; use event handlers for load or failure callbacks.
<Script src="https://example.com/script.js" strategy="afterInteractive" />| Name | Type | Description |
|---|---|---|
src | string | URL or internal path of the external script; required unless the script is inline. |
strategy | string | Controls when the script loads: beforeInteractive, afterInteractive, lazyOnload, or experimental worker. |
onLoad | function | Callback invoked after the script finishes loading. |
onReady | function | Callback invoked after loading and each time the component mounts. |
onError | function | Callback invoked if the script fails to load. |
A React component that renders and manages an optimized external or inline script.
src, which accepts an absolute URL or an internal path.beforeInteractive when the script must run before Next.js code and hydration.lazyOnload for work that can wait until the browser is idle.worker strategy offloads script execution to a web worker using Partytown.onLoad to run code once an external script has finished loading.src; omit it only when providing inline JavaScript.afterInteractive.import Script from 'next/script'
export default function Layout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>
{children}
<Script
src="https://example.com/analytics.js"
strategy="afterInteractive"
onLoad={() => console.log('analytics loaded')}
/>
</body>
</html>
)
}Loads an external script after initial hydration and logs when it finishes loading.
A library required before application hydration, such as a script that must establish an early browser capability.
An analytics or advertising integration that should load soon after the page becomes interactive.
A nonessential widget or integration that should avoid competing with initial page work.
A small configuration or bootstrap snippet that does not need a separate JavaScript file.
next/script solve?next/script is Next.js’s optimized wrapper for third-party scripts. It lets the application control when scripts are fetched and executed without manually managing ordinary script tags.
Follow-up: How would you choose a strategy for a nonessential analytics script? Use lazyOnload so it waits for browser idle time rather than competing with initial page work.
beforeInteractive loads from the initial server HTML before Next.js code and hydration. afterInteractive loads after some hydration and is the default for scripts that should load early but not before first-party code.
Follow-up: When is beforeInteractive appropriate? Use it only for scripts that must be available before hydration, rather than for ordinary third-party integrations.
onLoad runs after a script finishes loading, onReady runs after loading and whenever the component mounts, and onError runs when loading fails.
Follow-up: Which callback suits setup that must run after remounts? Use onReady, because it runs after loading and on every component mount.
Script ComponentstrategyonLoadonReadyonError