<div id="disqus_thread">
    {% include "templates/components/single/static_comments.twig" %}
</div>

{% if not user_agent.is_pagespeed %}
<script>
    window.addEventListener('load', function () {
        window.setTimeout(function () {
            var countScript = document.createElement('script');
            countScript.id = 'dsq-count-scr';
            countScript.src = 'https://{{disqus.shortname}}.disqus.com/count.js';
            countScript.async = true;
            document.body.appendChild(countScript);
        }, 3000);
    }, { once: true });
</script>
{% endif %}

<script>
    var disqus_config = function () {
        this.page.url        = '{{disqus.url}}';
        this.page.identifier = '{{disqus.identifier}}';
        this.page.title      = '{{disqus.title}}';
    };

    var disqusLoaded = false;
    var disqusTarget = document.getElementById('disqus_thread');

    function loadDisqus() {
        if (disqusLoaded) return;
        var d = document, s = d.createElement('script');
        s.src = 'https://{{disqus.shortname}}.disqus.com/embed.js';
        s.setAttribute('data-timestamp', +new Date());
        (d.head || d.body).appendChild(s);
        disqusLoaded = true;
    }

    if ('IntersectionObserver' in window && disqusTarget) {
        var disqusObserver = new IntersectionObserver(function (entries) {
            if (entries[0].isIntersecting) {
                loadDisqus();
                disqusObserver.disconnect();
            }
        }, { rootMargin: '600px 0px' });
        disqusObserver.observe(disqusTarget);
    } else {
        window.addEventListener('scroll', function () {
            if ((window.innerHeight + window.scrollY) > (document.body.offsetHeight - 1200)) {
                loadDisqus();
            }
        }, { passive: true });
    }
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
