A script you added to a Next.js page never runs
The script tag is in the page source but the code never runsShort 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.
Fix it fast
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/null2Match what you see, then jump to its fix
- 1
Inline code inside <Script> or dangerouslySetInnerHTML; no id propAn inline script has no idFix - 2
strategy="beforeInteractive" placed in a page or nested layoutbeforeInteractive is used outside the root layoutFix - 3
Script loads, but the callback never runsonLoad, onReady or onError are used in a Server ComponentFix - 4
Script uses strategy worker inside app/strategy="worker" in the App RouterFix
- 1
What does the error look like?
The script tag is in the page source but the code never runsA 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 devConfirmed by nextjs.org
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" app2Fix
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 startConfirmed by nextjs.org
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 | head2Fix
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 devConfirmed by nextjs.org
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 pages2Fix
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 devConfirmed by nextjs.org
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
ESLint says an element in a list is missing its key prop
react/jsx-key
react/jsx-key fires when an element returned from map, Array.from or an array literal has no key.
3 causes, each with a fixWeb apps
There is no tailwind.config.js after installing Tailwind 4 and HeroUI in Next.js
HeroUI components render unstyled
Nothing is broken: Tailwind CSS 4 is configured in CSS and no longer creates tailwind.config.js.
3 causes, each with a fixWeb apps
react-i18next shows the wrong plural form for a count, or prints the key instead of the text
t('item') prints the key item instead of a translated sentence
i18next picks a plural from the count option and looks for keys ending in _one, _other and so on.
4 causes, each with a fixWeb apps
useSession throws because there is no SessionProvider above it
[next-auth]: `useSession` must be wrapped in a <SessionProvider />
useSession reads the session from React Context, and SessionProvider is what creates that context.
3 causes, each with a fixWeb 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 fixWeb 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