next/script onLoad run once per cache Key (id, src)
See original GitHub issueWhat version of Next.js are you using?
12.0.2
What version of Node.js are you using?
14.18.1
What browser are you using?
Chrome
What operating system are you using?
macOS
How are you deploying your application?
codesandbox
Describe the Bug
When using more than one Script
with same id
or src
, only first script that complete load trigger the onLoad
prop.
Same problem happens using one Script
but after unmount and remount, for example when navigating throughout client-side router the onLoad
will not be called.
@giorgiabosello
Expected Behavior
Script
component should load once the src script but run onLoad
on component did mount and script is loaded.
To Reproduce
Issue Analytics
- State:
- Created 2 years ago
- Reactions:5
- Comments:16 (2 by maintainers)
Top Results From Across the Web
Basic Features: Handling Scripts - Next.js
Event handlers can be used with the Script component to execute additional code after a certain event occurs: onLoad : Execute code after...
Read more >Optimizing third-party script loading in Next.js
This article discusses the Next.js Script component and demonstrates how it can be used for better sequencing of third-party scripts.
Read more >Best practices to increase the speed for Next.js apps
Next.js has built-in caching so pages load faster. ... By setting the value of the strategy prop in the next/script component, ...
Read more >How to load external scripts dynamically in Angular?
You can use following technique to dynamically load JS scripts and libraries on demand in your Angular project. script.store.ts will contain the path...
Read more >Optimize loading third-parties - Patterns.dev
For example, you may want to execute specific analytics scripts early without missing any early page-load data. <script src="https://example.com/deferthis.js" ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks for brainstorming with me folks, I appreciate it 😃
PR https://github.com/vercel/next.js/pull/38849 should solve for this by introducing
onReady
as a prop. Please feel free to leave any comments directly in the PR if you think anything should be changed, but it’s set up to run afterload
for when the script first loads and then again with every component re-mount.This is indeed an issue, so thanks all for flagging this.
The hook is a workaround for now, but I’ll either approve @bordeo’s PR (#30966) to remove the current caching functionality or investigate a way to improve it so that it doesn’t ignore
onLoad
for repeat scripts.