summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEltrac <hi@guhub.cn>2025-08-29 14:53:15 +0800
committerEltrac <hi@guhub.cn>2025-08-29 14:53:15 +0800
commitf839c9ce6277a5f0bc19e2a1e23a2c225715b1a6 (patch)
tree8011e4683988c7b91c95780081cc009b93569a0a
parent3c577e8ca28662e44297d0735b9b21b121a569b1 (diff)
perf: load giscus and comment count lazily
-rw-r--r--layouts/_partials/posts/action-menu.html2
-rw-r--r--layouts/_partials/posts/comment.html44
-rw-r--r--layouts/_partials/scripts/action-menu-js.html38
3 files changed, 53 insertions, 31 deletions
diff --git a/layouts/_partials/posts/action-menu.html b/layouts/_partials/posts/action-menu.html
index b5c75279..5f79ad98 100644
--- a/layouts/_partials/posts/action-menu.html
+++ b/layouts/_partials/posts/action-menu.html
@@ -1,4 +1,4 @@
-<div class="my-8 mb-4">
+<div id="action-menu" class="my-8 mb-4">
<!-- 操作按钮组 -->
<div class="flex justify-between items-center mb-6">
<div class="flex gap-2">
diff --git a/layouts/_partials/posts/comment.html b/layouts/_partials/posts/comment.html
index 01e04cb4..af0def7e 100644
--- a/layouts/_partials/posts/comment.html
+++ b/layouts/_partials/posts/comment.html
@@ -32,19 +32,35 @@
</div>
<!-- giscus script -->
- <script id="giscus-script"
- src="https://giscus.app/client.js"
- data-repo="{{ .Site.Params.giscus.repo }}"
- data-repo-id="{{ .Site.Params.giscus.repoId }}"
- data-category="{{ .Site.Params.giscus.category }}"
- data-category-id="{{ .Site.Params.giscus.categoryId }}"
- data-mapping="{{ .Site.Params.giscus.mapping }}"
- data-strict="{{ .Site.Params.giscus.strict }}"
- data-reactions-enabled="{{ .Site.Params.giscus.reactionsEnabled }}"
- data-emit-metadata="1"
- data-input-position="{{ .Site.Params.giscus.inputPosition }}"
- data-theme="{{ .Site.Params.giscus.theme }}"
- data-lang="{{ T "lang" }}"
- crossorigin="anonymous">
+ <script>
+ function loadGiscus() {
+ if (document.querySelector('iframe.giscus-frame')) return; // 防止重复加载
+
+ const script = document.createElement("script");
+ script.src = "https://giscus.app/client.js";
+ script.setAttribute("data-repo", "{{ .Site.Params.giscus.repo }}");
+ script.setAttribute("data-repo-id", "{{ .Site.Params.giscus.repoId }}");
+ script.setAttribute("data-category", "{{ .Site.Params.giscus.category }}");
+ script.setAttribute("data-category-id", "{{ .Site.Params.giscus.categoryId }}");
+ script.setAttribute("data-mapping", "{{ .Site.Params.giscus.mapping }}");
+ script.setAttribute("data-strict", "{{ .Site.Params.giscus.strict }}");
+ script.setAttribute("data-reactions-enabled", "{{ .Site.Params.giscus.reactionsEnabled }}");
+ script.setAttribute("data-emit-metadata", "1");
+ script.setAttribute("data-input-position", "{{ .Site.Params.giscus.inputPosition }}");
+ script.setAttribute("data-theme", "{{ .Site.Params.giscus.theme }}");
+ script.setAttribute("data-lang", "{{ T "lang" }}");
+ script.setAttribute("crossorigin", "anonymous");
+ script.async = true;
+ document.body.appendChild(script);
+ }
+
+ const observer = new IntersectionObserver((entries, obs) => {
+ if (entries[0].isIntersecting) {
+ loadGiscus();
+ obs.disconnect();
+ }
+ });
+
+ observer.observe(document.getElementById("action-menu"));
</script>
</div>
diff --git a/layouts/_partials/scripts/action-menu-js.html b/layouts/_partials/scripts/action-menu-js.html
index 66673b71..69f5189c 100644
--- a/layouts/_partials/scripts/action-menu-js.html
+++ b/layouts/_partials/scripts/action-menu-js.html
@@ -31,25 +31,31 @@ function initializeComments() {
// 评论数量功能初始化
function initializeCommentCount() {
const commentCountEl = document.getElementById('comment-count');
- const giscusIframe = document.querySelector('iframe.giscus-frame');
- if (!commentCountEl || !giscusIframe) return;
-
- commentCountEl.textContent = '0';
-
- giscusIframe.addEventListener('load', () => {
- window.addEventListener('message', function(event) {
- if (event.origin !== 'https://giscus.app') return;
-
- const giscusData = event.data;
- if (giscusData.giscus?.discussion) {
- const discussion = giscusData.giscus.discussion;
- const totalComments = (discussion.totalCommentCount || 0) + (discussion.totalReplyCount || 0);
- commentCountEl.textContent = totalComments.toString();
- }
- });
+ if (!commentCountEl) return;
+
+ const observer = new MutationObserver(() => {
+ const giscusIframe = document.querySelector('iframe.giscus-frame');
+ if (giscusIframe) {
+ giscusIframe.addEventListener('load', () => {
+ window.addEventListener('message', function(event) {
+ if (event.origin !== 'https://giscus.app') return;
+ const giscusData = event.data;
+ if (giscusData.giscus?.discussion) {
+ const discussion = giscusData.giscus.discussion;
+ const totalComments = (discussion.totalCommentCount || 0) + (discussion.totalReplyCount || 0);
+ commentCountEl.textContent = totalComments.toString();
+ }
+ });
+ });
+ observer.disconnect();
+ }
});
+
+ observer.observe(document.body, { childList: true, subtree: true });
}
+document.addEventListener('DOMContentLoaded', initializeCommentCount);
+
// 点赞功能初始化
function initializeLikes(pageId) {
const elements = {