Skip to content
This repository was archived by the owner on Jan 19, 2025. It is now read-only.

Commit d3dfcbe

Browse files
committed
Fixed disqus async load with intersection observer
1 parent 03e71b4 commit d3dfcbe

File tree

5 files changed

+27
-11
lines changed

5 files changed

+27
-11
lines changed

_includes/dependencies-js-blog.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS_SVG"></script>
1919
{% endif %}
2020
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/cookieconsent@3/build/cookieconsent.min.js" data-cfasync="false"></script>
21-
<script type="text/javascript" src="/assets/js/index.blog.min.js?rev=7878cb2532f75ed3af6934acec4c2205"></script>
21+
<script type="text/javascript" src="/assets/js/index.blog.min.js?rev=0c9e9c3055977cc5c44727b808ce2c51"></script>
2222
<script type="text/javascript">
2323
ChicioCodingBlog.init('{{ include.track-category }}', {{ include.shouldActivatePullToRefresh }})
2424
</script>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
"/assets/js/index.blog.min.js?rev=7878cb2532f75ed3af6934acec4c2205",
1+
"/assets/js/index.blog.min.js?rev=0c9e9c3055977cc5c44727b808ce2c51",

_js/blog/disqus.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,25 @@ const disqus = () => {
55
this.page.identifier = window.location.href
66
}
77
const intersectionObserver: IntersectionObserver = new IntersectionObserver(
8-
loadDisqus,
9-
{ rootMargin: '250px 0px', threshold: 0.01 }
8+
onIntersection,
9+
{ rootMargin: '100px 0px', threshold: 0.01 }
1010
)
1111
const disquisThread = document.getElementById('disqus_thread')
1212
if (disquisThread) {
1313
intersectionObserver.observe(disquisThread)
1414
}
1515
}
1616

17-
const loadDisqus = (entries: IntersectionObserverEntry[], observer: IntersectionObserver) => {
18-
observer.unobserve(entries[0].target)
17+
const onIntersection = (entries: IntersectionObserverEntry[], observer: IntersectionObserver): void => {
18+
for (let i: number = 0; i < entries.length; i++) {
19+
if (entries[i].intersectionRatio > 0) {
20+
loadDisqus(entries[i].target, observer)
21+
}
22+
}
23+
}
24+
25+
const loadDisqus = (element: HTMLElement, observer: IntersectionObserver) => {
26+
observer.unobserve(element)
1927
const s = document.createElement('script')
2028
s.src = 'https://fabrizio-duroni.disqus.com/embed.js'
2129
s.setAttribute('data-timestamp', `${+new Date()}`)

_jsbuild/blog/disqus.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,25 @@ const disqus = () => {
55
this.page.identifier = window.location.href
66
}
77
const intersectionObserver = new IntersectionObserver(
8-
loadDisqus,
9-
{ rootMargin: '250px 0px', threshold: 0.01 }
8+
onIntersection,
9+
{ rootMargin: '100px 0px', threshold: 0.01 }
1010
)
1111
const disquisThread = document.getElementById('disqus_thread')
1212
if (disquisThread) {
1313
intersectionObserver.observe(disquisThread)
1414
}
1515
}
1616

17-
const loadDisqus = (entries, observer) => {
18-
observer.unobserve(entries[0].target)
17+
const onIntersection = (entries, observer) => {
18+
for (let i = 0; i < entries.length; i++) {
19+
if (entries[i].intersectionRatio > 0) {
20+
loadDisqus(entries[i].target, observer)
21+
}
22+
}
23+
}
24+
25+
const loadDisqus = (element, observer) => {
26+
observer.unobserve(element)
1927
const s = document.createElement('script')
2028
s.src = 'https://fabrizio-duroni.disqus.com/embed.js'
2129
s.setAttribute('data-timestamp', `${+new Date()}`)

assets/js/index.blog.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)