next/script does not trigger onLoad callback when used with beforeInteractive strategy
See original GitHub issueRun next info
(available from version 12.0.8 and up)
No response
What version of Next.js are you using?
11.1.2
What version of Node.js are you using?
14.15
What browser are you using?
Chrome
What operating system are you using?
Linux
How are you deploying your application?
next dev
Describe the Bug
According to the docs, onLoad
callback of next/script
component can be used either with beforeInteractive
and afterInteractive
strategies. But when using with beforeInteractive
strategy, onLoad
callback never gets called.
Expected Behavior
Expect onLoad
callback to be called when loading script with beforeInteractive
strategy
To Reproduce
Using the same example of the docs, with stripe script: hasLoaded is never set to true and stripe has loaded never gets printed. I get the same behavior in the latest version of next.
Here is a minimal reproducible repo: https://github.com/loft-br/before-interactive-on-load-bug
Issue Analytics
- State:
- Created 2 years ago
- Reactions:8
- Comments:13 (3 by maintainers)
Top Results From Across the Web
Basic Features: Handling Scripts - Next.js
Optimize your third-party scripts with the built-in `next/script` component. ... Note: The worker strategy is not yet stable and does not yet work...
Read more >Why does the next.js Script tag with the beforeInteractive ...
js Script tag with the strategy beforeInteractive works. For testing i just used lodash. But i keep getting a ReferenceError: _ is not...
Read more >Script Loading, performance and Next Script
There are four strategies in using Next Script. Three really, beforeInteractive , afterInteractive , lazyOnLoad . There is an experimental ...
Read more >Gatsby Script API
Note - Inline scripts and scripts using the off-main-thread strategy do not support the onLoad and onError callbacks. Here is an example using...
Read more >Optimizing third-party script loading in Next.js
Since the default strategy used is afterInteractive , developers must remember to set the strategy to beforeInteractive for scripts necessary ...
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
Me and my team are struggling with this exact issue right now. I created the issue #33402 on documentation inconsistencies, but would also like to see some sort of a resolve to this.
Our use case is following: we use two
next/script
elements to load two external scripts: the first one loads a cookie consent SDK and the second loads our analytics library. Both of the script elements have their ownonLoad
s for initialization.The problem is that the analytics library depends on the cookie consent SDK, so we need to load and initialize that one first. Also, both of these scripts need to be initialized before any React
useEffect
hooks are run, because that’s where the tracking functions of the analytics library are to be used.Now, because
onLoad
s for these scripts are just silently ignored, we can’t do initialization on neither of these libraries.We are having the same issue with
beforeInteractive
andonLoad
as the code insideonLoad
had never being executed.Here is the sample code inside
_app.tsx
:With a simple code inside
script1.js
to beAnd
script2.js
:Then we have the output of