question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

next/script does not trigger onLoad callback when used with beforeInteractive strategy

See original GitHub issue

Run 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 image

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:8
  • Comments:13 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
akselinurmiocommented, Jan 17, 2022

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 own onLoads 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 onLoads for these scripts are just silently ignored, we can’t do initialization on neither of these libraries.

3reactions
hemmachatcommented, Jan 19, 2022

We are having the same issue with beforeInteractive and onLoad as the code inside onLoad had never being executed.

Here is the sample code inside _app.tsx:

Screen Shot 2022-01-19 at 2 57 28 pm

With a simple code inside script1.js to be

console.log('loading script1.js');

And script2.js:

console.log('loading script2.js');

Then we have the output of

loading script1.js
loading script2.js
afterInteractive onLoad()
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found