-
Notifications
You must be signed in to change notification settings - Fork 27.5k
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
Scripts with async in Head are executed twice #9070
Scripts with async in Head are executed twice #9070
Comments
I'm very new to nextjs, but this issue peaked my interest. I've managed to take a quick look at the issue and found that it also happens for script tags with the Testing it out locally, it looks like it happens when the client mounts and the script tag goes from I tracked it down to this line in the next-server/lib/head.tsx file. Removing the Hopefully this little investigation can help track down the true source of the issue. |
On the client, a NextHead update following initial hydration will change the value of the A brute-force fix for this would be to read in the .map((c: React.ReactElement<any>, i: number) => {
const key = c.key || i
const props = { key }
if ('async' in c.props) props.async = c.props.async ? '' : undefined
if ('defer' in c.props) props.defer = c.props.defer ? '' : undefined
return React.cloneElement(c, props)
}) This is not a real solution either, but it might help narrow down the issue. |
A temporary work around is to probably render this instead: <script src="/static/log.js" async="async"></script> |
I gave that a try when looking into the issue and that still results in the script been re-executed (I should have mentioned that before). Again, the script element goes from If you end up going with @developit suggestion, it might be worth taking a look at other boolean attributes for script tags. |
I'm experiencing the same issue on a client's website. They are using Google Hire for job openings so we are loading the following script in
On a client-side render, it loads as expected. On a server-side render, it runs twice resulting in two identical iframes. I tried adjusting Has anyone found a solution that does not involve the temporary workarounds? |
@dsmikeyorke a temporary workaround is to have a global var init at the top of the file...
While not ideal, this is a workaround to stop the file from running twice. |
Maybe putting the scripts into |
Thanks for the feedback! I ended up wrapping the script in the following conditional so it only renders client-side:
|
@dsmikeyorke oh that's a nifty way to handle that. Any side effects to doing this on a ton of script inclusions? |
@jonkwheeler I only have this conditional on one script. I have not encountered any side effects and it cleared up the double load issue! |
@dsmikeyorke I think I'm gonna go with @timneutkens suggestion here - #2177 (comment) Very similar to what you said, and to what he said. He says not to assign it to a variable so it tree shakes away, so I'd love confirmation if this is bad or not. I can't imagine one var declaration harming anything, but wanted to double check.
Edit: Only thing that has worked for me is the following, and creating a var like
or
|
I changed some code and this stopped working. I am now using
|
Just came across this too, PR to hopefully fix ☝️ |
This pull request correctly assigns boolean attributes for `<script />` to match the element as it is created by a server-side render. Prior to this pull request, we'd double-execute `<script>` tags with the `async`, `defer`, or `nomodule` property. --- Fixes vercel#9070
This pull request correctly assigns boolean attributes for `<script />` to match the element as it is created by a server-side render. Prior to this pull request, we'd double-execute `<script>` tags with the `async`, `defer`, or `nomodule` property. --- Fixes #9070
In my case, I needed to specify |
This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Bug report
Describe the bug
Scripts in the Head are run twice if marked
async
.To Reproduce
Steps to reproduce the behavior:
/static/log.js
withconsole.log('hello');
insideasync
attribute)Expected behavior
Scripts should only run once.
Screenshots
System information
The text was updated successfully, but these errors were encountered: