Closed ardalis closed 3 years ago
@ardalis Thanks for the report! I'll investigate potential causes this evening and see if we can get this resolved asap.
@ardalis I tried to do some troubleshooting and it appears that the issue is occurring in the embed.js
script, which is failing to find the #disqus_thread
container on the page.
I can try to help you determine why this is happening if you can provide an example repo that can be used to reproduce the issue locally.
Thanks, it's here:
@ardalis When I spin up the site locally I'm seeing the Disqus thread load correctly, which makes me believe that the issue might be related to the isCmsPreview
ternary that the DiscussionEmbed
is being loaded within. Though, I admittedly don't know much about the CMS setup you're using.
Have you tried pulling the DiscussionEmbed
outside of the ternary and seeing if the thread is loaded in production?
This issue has gone quiet, so I'm gonna close it out for now.
I have reproduced the same issue with latest version, 1.0.11
I'll investigate a bit further to see if I can reproduce the specific circumstances in which this happens, because it only happens with a certain combination of actions and not always.
This is the project in which I use this: https://github.com/acelaya/alejandrocelaya.blog
Hey @tterb.
So, I have investigated a bit and the problem seems to be related with the fact that the insertScript
function is being called by the DiscussionEmbed
component (and probably CommentCount
too), but providing the window.document.body
object as the parent of the script.
The problem is that, if the bundled script is being loaded in the <head />
tag (which seems to be the default behavior in gatsby, which is being used by @ardalis, and next.js, which is used by me), then the body is null
, causing this. This issue is probably the same https://stackoverflow.com/questions/9916747/why-is-document-body-null-in-my-javascript/9916754
In order to fix this I would suggest using a react reference to the element itself as the parent, instead of using the body. I'm using that approach here https://github.com/acelaya/alejandrocelaya.blog/blob/main/src/components/CarbonAds.tsx
I can provide a PR with the fix if needed.
@acelaya Thanks for tracking this down! I think your suggested solution sounds reasonable and PR's are always appreciated and can speed up the process.
I'll reopen this issue now so we can make sure it gets resolved.
So, I have started implementing the fix, and I have found out a two things.
The first one is that the error is not actually thrown by this library. The appendChild
executed in the insertScript
function actually works, and the embed.js
file gets properly loaded, which means the document.body
object is not null
as I suspected.
The error seems to be somewhere in the code loaded there, inside some recommendations.js
file. See the stack trace:
Is this file also part of an open source project? I could try to continue digging there if so.
The second thing I have found is that using references as I suggested does fix the problem, but inderectly as it's clearly not addressing the root cause, it just happens to solve it.
Because of that, I don't think is the right way to go. Also, the tests break, and I need to investigate why.
So, @tterb what's your recommendation here. Should the issue be addressed in this recommendations.js
file, or should I try to fix the tests and provide the change based on the use of react references?
@acelaya The recommendations.js
is part of a new Disqus feature that allows publishers to load Recommendations as a standalone app (There is already #94 to add this functionality to this package). Though in an attempt to maintain the previous experience of loading Recommendations alongside the comment thread for publishers, the app can also be loaded adjacent to the comment thread by the embed.js
script based on the publisher's settings. This is what appears to be causing the issue you are seeing.
I'm going to take a look at this issue today and see if I can track down the underlying cause in the Disqus application.
@acelaya A potential fix was deployed earlier today that should resolve the issue you were experiencing. I'm no longer seeing the error on the site you linked, but I'll wait on your confirmation to close this issue out.
Yep, I can't reproduce it anymore and the comments load properly every time.
Thanks!
@acelaya Great! Thanks for your help reporting and diagnosing this issue!
Describe the bug
Disqus discussion not showing up on page; getting this error in console:
To Reproduce
Steps to reproduce the behavior:
Expected behavior
Disqus on the page working.
Screenshots
See above.