New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
next/script does not trigger onLoad callback when used with beforeInteractive strategy #33191
Comments
This is expected. See #26343 (comment) Also related #28889 |
Sorry if I'm missing something, but this is not about inline scripts at all. It is a third party script just like the example in the docs.
|
The documentation does not use Removing that prop loads it correctly. So just curious if it was a misunderstanding or just a bad reproduction? What is your use case by the way? What kind of third-party script do you try embedding with the |
We are trying to use google optimize script with beforeInteractive strategy so it can apply DOM changes as soon as possible. However, because we are using SSR, this is not enough to prevent flickering, so we tried to use antiflicker script provided by Google but it sets opacity:0 to the whole page and this hurts web vitals (mainly LCP). We are trying to implement the anti-flicking algorithm by ourselves so it can be used per component / section of the page |
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 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 Now, because |
We are having the same issue with Here is the sample code inside With a simple code inside
And
Then we have the output of
|
ability to use a suggested solution is to execute the onLoad only on the client side instead of ignoring it - which happens now. I can confirm the documentation is slightly confusing as it does not clarify you cannot have |
Is it safe to assume that using |
Having the problem, any solutions? I need to load two scripts in order, onLoad not working with beforeInteractive is a dealbreaker, don't want to force a refresh. |
Is any progress on this? I'm having the same issue. onLoad is working only with afterInteractive. |
Yes any update of this issue? |
I'm also facing this issue. My use case is to load the scripts to access the Google APIs ( Additionally, as previous commenters have mentioned, the documentation made me think there was a bug as it mentions running code after a library has loaded and the immediate example underneath it loads an external Stripe script with an In fact, reading over the documentation once again, surely this sentence is incorrect (or at least, incomplete)?
If this doesn't apply for external Scripts, it should probably be included in the documentation. It gets even more confusing now I found that the API documentation for |
#33097 adds a note to the `Script` documentation explaining that `onLoad` cannot be used with the `beforeInteractive` strategy. However, this note was missing in the [Basic Features](https://nextjs.org/docs/basic-features/script) documentation, causing some confusion. This adds the note there too. This will hopefully fix a lot of confusion noted in #33191.
Sorry for all the confusion folks. @davidbarker you are indeed correct that the documentation is wrong. |
Please verify that your issue can be recreated with Why was this issue marked with the
|
This issue has been automatically closed because it wasn't verified against next@canary. If you think it was closed by accident, please leave a comment. If you are running into a similar issue, please open a new issue with a reproduction. Thank you. |
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
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 ofnext/script
component can be used either withbeforeInteractive
andafterInteractive
strategies. But when using withbeforeInteractive
strategy,onLoad
callback never gets called.Expected Behavior
Expect
onLoad
callback to be called when loading script withbeforeInteractive
strategyTo 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
The text was updated successfully, but these errors were encountered: