The issue involves Next.js Script behavior and requires understanding of internal mechanisms.
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.
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
Chrome 103.0.5060.134 (Official Build) (arm64)
No response
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.
Whenever a page is visited, inline scripts will be run, regardless of whether the page has already been visited in that session.
https://stackblitz.com/edit/nextjs-vyvasp?file=pages/index.js
(Scr
Claim this issue to let others know you're working on it. You'll earn 10 points when you complete it!