From f621398f3ae562adda5b707c9ea52f3b8b3d1de5 Mon Sep 17 00:00:00 2001 From: Arthur Cruz Date: Sat, 3 Aug 2024 01:56:31 -0300 Subject: [PATCH] fixing disqus comments thread not loading --- .../partials/comments/provider/disqus.html | 77 ++++++++++++++----- 1 file changed, 58 insertions(+), 19 deletions(-) diff --git a/layouts/partials/comments/provider/disqus.html b/layouts/partials/comments/provider/disqus.html index fb17b77..8dbcc13 100644 --- a/layouts/partials/comments/provider/disqus.html +++ b/layouts/partials/comments/provider/disqus.html @@ -1,22 +1,61 @@ +{{- $pc := .Site.Config.Privacy.Disqus -}} +{{- $disqusjs := .Site.Params.Comments.disqusjs -}} +{{- if and (not $pc.Disable) (and $disqusjs.Shortname $disqusjs.ApiKey) -}} + +{{- $style := resources.Get "scss/partials/comments/disqusjs.scss" | resources.ToCSS | minify -}} + +
- {{ template "_internal/disqus.html" . }} -
- - - - \ No newline at end of file + + function lazyLoadDisqusJS() { + if (["localhost", "127.0.0.1"].indexOf(window.location.hostname) != -1) { + document.getElementById('disqus_thread').innerHTML = 'Disqus comments not available by default when the website is previewed locally.'; + return; + } + + let d = document.createElement('script'); + d.src = 'https://cdn.jsdelivr.net/npm/disqusjs@1.3/dist/disqus.js'; + d.async = false; + document.body.appendChild(d); + d.onload = () => { + loadDisqusJS(); + window.addEventListener('onColorSchemeChange', (e) => { + if (disqusjs) { + loadDisqusJS(); + } + }) + } + } + + let runningOnBrowser = typeof window !== "undefined"; + let isBot = runningOnBrowser && !("onscroll" in window) || typeof navigator !== "undefined" && /(gle|ing|ro|msn)bot|crawl|spider|yand|duckgo/i.test(navigator.userAgent); + let supportsIntersectionObserver = runningOnBrowser && "IntersectionObserver" in window; + + if (!isBot && supportsIntersectionObserver) { + let disqus_observer = new IntersectionObserver(function(entries) { + if (entries[0].isIntersecting) { + lazyLoadDisqusJS(); + disqus_observer.disconnect(); + } + }); + disqus_observer.observe(document.getElementById('disqus_thread')); + } else { + lazyLoadDisqusJS(); + } + + + +{{- end -}} \ No newline at end of file