Script

There are four different strategies that can be used to load a <Script>:

Press F12 to read the log on the browser console. Notice the file myscript.js is placed in the public folder.

To load a third-party script for all routes in Next.js, import next/script and include the script in the root layout. It will execute only once across the app, even during navigation.

Add scripts only to specific pages or layouts to minimize performance impact.

Any extra DOM attributes like nonce or custom data attributes are automatically forwarded to the final <script> element.

Inline scripts are also supported.
<Script id="show-banner">
  {`document.getElementById('banner').classList.remove('hidden')`}
</Script>

<Script
  id="show-banner"
  dangerouslySetInnerHTML={{
    __html: `document.getElementById('banner').classList.remove('hidden')`,
  }}
/>