Comment functionality allow your users to share their thoughts on your pages, products or topics and can be the beautiful beginning of an engaged community surrounding your website.
We create a Astro component called disqus.astro
with the following content:
<div id="disqus_thread"></div>
<script is:inline>
if (!window.DisqusWidget) {
window.DisqusWidget = {
overwriteGlobalSelectors: function () {
window.$disqus = document.querySelector('#disqus_thread')
},
init: function () {
this.overwriteGlobalSelectors()
this.addListeners()
this.initDisqus()
},
addListeners: function () {
// After language switched
document.addEventListener('astro:after-swap', () => {
this.overwriteGlobalSelectors()
this.initDisqus()
})
},
initDisqus: () => {
// early escape if $disqus not exists
if (window.$disqus === null) {
return
}
// Reset DISQUS, Rather than loading new embed.js
if (window.DISQUS) {
window.DISQUS.reset({
reload: true
})
return
}
(function () { // DON'T EDIT BELOW THIS LINE
const d = document, s = d.createElement('script');
s.src = '[PATH_TO_EMBED_JS]';
s.setAttribute('data-timestamp', String(+new Date()));
(d.head || d.body).appendChild(s);
})();
}
}
window.DisqusWidget.init()
}
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by
Disqus.</a></noscript>
Please replace PATH_TO_EMBED_JS
with your path to embed.js and if view transitions are not used in your project, please remove the listener astro:after-swap
above and use the code directly.
addListeners: function () {
// After language switched
this.overwriteGlobalSelectors()
this.initDisqus()
},
Have fun with it! And feel free to leave a comment.