next/script ka component hota hai third-party JS files ko load karne ke liye (jaise Google Analytics, Ads, etc.) optimized way mein.
import Script from 'next/script';
export default function MyComponent() {
return (
<>
<h1>Google Analytics Example</h1>
{/* ✅ External Script (Lazy Load) */}
<Script
src="https://www.google-analytics.com/analytics.js"
strategy="lazyOnload"
onLoad={() => console.log("✅ Google Analytics script loaded!")}
/>
{/* ✅ Script from Public Folder */}
<Script src="/myscript.js" strategy="afterInteractive" />
{/* ✅ Inline Script */}
<Script
id="custom-inline-script"
strategy="afterInteractive"
dangerouslySetInnerHTML={{
__html: console.log("✅ Inline script executed after hydration!");
}}
/>
</>
);
}beforeInteractive – Render se pehle (rarely use)afterInteractive – Page load ke baad (⚡ recommended)lazyOnload – Sab kuch load hone ke baad