Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

A script you added to a Next.js page never runs

ErrorThe script tag is in the page source but the code never runs

Short answer

Load scripts in Next.js with the next/script component, the documented way. When one still does not run, three documented reasons cover most cases: an inline script has no id, a beforeInteractive script is not in the root layout, or onLoad, onReady or onError sit in a Server Component.

  • Applies to Next.js 15, Next.js 16, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Open the Network and Console tabs. If the file is requested and returns 200, the loading works and the cause is on this page; if it is not requested, the component is not rendered where you think.

    grep -rn "next/script\|<script" app components pages 2>/dev/null
  2. 2Match what you see, then jump to its fix

    1. 1Inline code inside <Script> or dangerouslySetInnerHTML; no id propAn inline script has no idFix
    2. 2strategy="beforeInteractive" placed in a page or nested layoutbeforeInteractive is used outside the root layoutFix
    3. 3Script loads, but the callback never runsonLoad, onReady or onError are used in a Server ComponentFix
    4. 4Script uses strategy worker inside app/strategy="worker" in the App RouterFix

What does the error look like?

  • The script tag is in the page source but the code never runs
  • A callback passed to the Script component never fires

Cause 1 of 4

An inline script has no id

1Check

Next.js needs an id to track and optimize an inline script.

grep -rn -A3 "<Script" app components | grep -c "id="

2Fix

Add a unique id.

import Script from 'next/script'

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

3Verify

Reload; the banner element changes.

npm run dev

Confirmed by nextjs.org

Did this fix it?

Cause 2 of 4

beforeInteractive is used outside the root layout

1Check

Scripts with this strategy must be inside a root layout such as app/layout.tsx.

grep -rn "beforeInteractive" app

2Fix

Move the Script into the root layout, or use the default afterInteractive for page-level scripts.

// app/layout.tsx
<Script src="https://example.com/script.js" strategy="beforeInteractive" />

3Verify

The script is now requested before hydration.

npm run build && npm run start

Confirmed by nextjs.org

Did this fix it?

Cause 3 of 4

onLoad, onReady or onError are used in a Server Component

1Check

These handlers work only in a Client Component, and onLoad and onError cannot be combined with beforeInteractive.

grep -rn "onLoad\|onReady\|onError" app | head

2Fix

Add 'use client' to the file, and use onReady instead of onLoad for beforeInteractive.

'use client'
import Script from 'next/script'

export default function Page() {
  return <Script src="https://example.com/script.js" onLoad={() => console.log('loaded')} />
}

3Verify

The log line appears once the script loads.

npm run dev

Confirmed by nextjs.org

Did this fix it?

Cause 4 of 4

strategy="worker" in the App Router

1Check

The worker strategy is experimental and currently works only in the pages directory.

grep -rn "strategy=\"worker\"" app pages

2Fix

Use the default afterInteractive strategy in the App Router.

<Script src="https://example.com/script.js" strategy="afterInteractive" />

3Verify

The script runs after hydration.

npm run dev

Confirmed by nextjs.org

Did this fix it?

How do I stop it coming back?

  • Use next/script for every third-party and inline script.
  • Put site-wide scripts in the root layout and page-specific ones in the page.
  • Add use client to any file that passes event handlers to Script.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

next/link throws React.Children.only on its children

Error: React.Children.only expected to receive a single React element child.

React.Children.only accepts exactly one React element and throws for anything else, including an array with one item.

3 causes, each with a fix

Web apps

Hydration failed after adding next-themes dark mode

Console reports a server and client mismatch on the html element

next-themes changes the html element and knows the theme only in the browser, so server HTML and client HTML differ.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.