Skip to main content
GoodFirstPicks
DashboardIssuesReposLeaderboard

GoodFirstPicks by Leaveitblank © 2026

CreatorRequest a RepoPrivacy PolicyTerms of Service
Inline Next/Script only runs on first page load | GoodFirstPicks

Inline Next/Script only runs on first page load

vercel/next.js 13 comments 1mo ago
View on GitHub
mediumopenScope: somewhat clearSkill match: maybeNext.jsTypeScriptReact

Why this is a good first issue

The issue involves Next.js Script behavior and requires understanding of internal mechanisms.

AI Summary

The issue describes a bug where inline scripts using Next/Script only run on the first page load and not on subsequent navigations. The problem is reproducible and involves understanding how Next.js handles script execution. While the issue is clear, fixing it requires knowledge of Next.js internals and may impact third-party integrations.

Issue Description

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
  Platform: darwin
  Arch: x64
  Version: Darwin Kernel Version 21.2.0: Sun Nov 28 20:28:41 PST 2021; root:xnu-8019.61.5~1/RELEASE_ARM64_T6000
Binaries:
  Node: 16.13.0
  npm: 7.15.1
  Yarn: 1.22.18
  pnpm: N/A
Relevant packages:
  next: 12.2.3
  eslint-config-next: N/A
  react: 17.0.2
  react-dom: 17.0.2

What browser are you using? (if relevant)

Chrome 103.0.5060.134 (Official Build) (arm64)

How are you deploying your application? (if relevant)

No response

Describe the Bug

An inline script (using Next Script) will only run the first time the page is visited. If the user navigates internally to other pages and then comes back to the original page (again, via internal navigation), the script will not run a second time.

<Script
  strategy="lazyOnload"
  dangerouslySetInnerHTML={{ __html: `console.log('Log dangerously')` }}
  id="unique-1"
/>

<Script strategy="lazyOnload" id="unique-2">
  console.log(`Log`)
</Script>

Tested using the lazyOnload, beforeInteractive and afterInteractive strategies.

Removing the ID attribute from Script results in the script being run on subsequent page loads.

Expected Behavior

Whenever a page is visited, inline scripts will be run, regardless of whether the page has already been visited in that session.

Link to reproduction

https://stackblitz.com/edit/nextjs-vyvasp?file=pages/index.js

To Reproduce

  • Open link https://stackblitz.com/edit/nextjs-vyvasp?file=pages/index.js
  • Open Dev Tools console.
  • Refresh the page (notice console.logs)
  • Navigate to Subpage one (notice console.logs)
  • Navigate to Subpage two (notice console.logs)
  • Navigate to any other page (notice only logs for Scripts with no ID)

(Scr

GitHub Labels

bug

Want to work on this?

Claim this issue to let others know you're working on it. You'll earn 10 points when you complete it!

Risk Flags

  • requires understanding of Next.js Script behavior
  • potential impact on third-party integrations
Loading labels...

Details

Points10 pts
Difficultymedium
Scopesomewhat clear
Skill Matchmaybe
Test Focusedno