Skip to content

Commit 1675fc9

Browse files
committed
feat(twikoo): lazy-load comments after route change
1 parent 2ad1f93 commit 1675fc9

File tree

2 files changed

+86
-39
lines changed

2 files changed

+86
-39
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- 站点 Origin 建议:优先保留 `https://www.sicnuwiki.com`(可按需追加 `https://sicnuwiki.com`
77
- twikoo:修正 Docsify 页面 path 计算,确保不同页面评论区独立
88
- twikoo:统一 README 相关路径(如 `/README``/./README`)以共享评论区
9+
- twikoo:切换页面时懒加载评论区,减少移动端卡顿并自动刷新评论
910

1011
### 腾讯云 COS CORS 设置要点
1112
- 路径:对象存储 COS → 存储桶 → 安全管理 → 跨域访问 CORS

docs/index.html

Lines changed: 85 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -221,31 +221,100 @@
221221
hook.afterEach(function(html) {
222222
window.ViewImage && ViewImage.init('.ebook img,a.zoom');
223223
});
224-
// 动态插入 div#tcomment 的 dom
225-
hook.mounted(function () {
224+
function ensureTcomment() {
225+
var tcomment = document.getElementById('tcomment');
226+
if (tcomment) return tcomment;
226227
var a = Docsify.dom;
227228
var n = a.create("div");
228229
n.id = "tcomment";
229-
a.appendTo(a.find(".content"), n);
230+
a.appendTo(a.find(".content"), n);
231+
return n;
232+
}
233+
234+
function normalizeCommentPath(path) {
235+
var p = decodeURIComponent(path || '/').trim();
236+
if (!p || p === '#') return '/';
237+
if (p.charAt(0) !== '/') p = '/' + p;
238+
p = p.replace(/\/\.\//g, '/');
239+
p = p.replace(/\/{2,}/g, '/');
240+
// 统一 README 首页写法
241+
if (p === '/README' || p === '/README.md' || p === '/./README') return '/README';
242+
return p;
243+
}
244+
245+
// 动态插入 div#tcomment 的 dom
246+
hook.mounted(function () {
247+
ensureTcomment();
230248
})
231249

232250
// twikoo 初始化控制变量
233251
var twikooTimer = null;
252+
var twikooObserver = null;
253+
var twikooLoading = false;
234254
var lastPath = null;
255+
256+
function loadTwikoo(path) {
257+
if (twikooLoading) return;
258+
twikooLoading = true;
259+
twikoo.init({
260+
envId: 'https://twikooeo.sicnuwiki.com',
261+
el: '#tcomment',
262+
lang: 'zh-CN',
263+
path: path,
264+
onCommentLoaded: function () {
265+
window.ViewImage && ViewImage.init('.tk-content img:not(.avatar,.tk-avatar-img,.tk-owo-emotion),.ebook img,a.zoom');
266+
}
267+
}).then(function () {
268+
twikooLoading = false;
269+
// 可选:强制刷新一次,确保切换页面后评论更新
270+
setTimeout(function () {
271+
var tkIcon = document.getElementsByClassName("tk-icon")[0];
272+
if (tkIcon) tkIcon.click();
273+
}, 200);
274+
}).catch(function () {
275+
twikooLoading = false;
276+
});
277+
}
278+
279+
function observeAndLoad(path) {
280+
if (twikooObserver) {
281+
twikooObserver.disconnect();
282+
twikooObserver = null;
283+
}
284+
if (twikooTimer) {
285+
clearTimeout(twikooTimer);
286+
twikooTimer = null;
287+
}
288+
289+
var tcomment = ensureTcomment();
290+
if (!tcomment) return;
291+
292+
if (!('IntersectionObserver' in window)) {
293+
twikooTimer = setTimeout(function () {
294+
loadTwikoo(path);
295+
}, 0);
296+
return;
297+
}
298+
299+
twikooObserver = new IntersectionObserver(function (entries) {
300+
if (!entries || !entries.length) return;
301+
if (entries[0].isIntersecting) {
302+
if (twikooObserver) {
303+
twikooObserver.disconnect();
304+
twikooObserver = null;
305+
}
306+
twikooTimer = setTimeout(function () {
307+
loadTwikoo(path);
308+
}, 0);
309+
}
310+
}, { rootMargin: '200px 0px' });
311+
312+
twikooObserver.observe(tcomment);
313+
}
235314

236315
// 调用 twikoo 评论,正则 path,刷新当前评论
237316
hook.doneEach(function () {
238317
// 获取当前 docsify 页面路径作为评论区分标识
239-
function normalizeCommentPath(path) {
240-
var p = decodeURIComponent(path || '/').trim();
241-
if (!p || p === '#') return '/';
242-
if (p.charAt(0) !== '/') p = '/' + p;
243-
p = p.replace(/\/\.\//g, '/');
244-
p = p.replace(/\/{2,}/g, '/');
245-
// 统一 README 首页写法
246-
if (p === '/README' || p === '/README.md' || p === '/./README') return '/README';
247-
return p;
248-
}
249318
var currentPath = normalizeCommentPath(vm.route.path || location.hash.replace(/^#/, '').replace(/\?.*$/, '') || '/');
250319

251320
// 如果路径相同,不重复初始化
@@ -254,36 +323,13 @@
254323
}
255324
lastPath = currentPath;
256325

257-
// 取消之前的延迟初始化(防止快速切换页面导致叠加)
258-
if (twikooTimer) {
259-
clearTimeout(twikooTimer);
260-
twikooTimer = null;
261-
}
262-
263326
// 先清空评论容器,避免叠加
264-
var tcomment = document.getElementById('tcomment');
327+
var tcomment = ensureTcomment();
265328
if (tcomment) {
266329
tcomment.innerHTML = '';
267330
}
268-
269-
// 延迟加载 twikoo,避免影响页面滑动性能
270-
twikooTimer = setTimeout(function() {
271-
// 再次检查路径是否仍然匹配(用户可能已切换)
272-
var nowPath = normalizeCommentPath(vm.route.path || location.hash.replace(/^#/, '').replace(/\?.*$/, '') || '/');
273-
if (nowPath !== currentPath) {
274-
return;
275-
}
276-
277-
twikoo.init({
278-
envId: 'https://twikooeo.sicnuwiki.com',
279-
el: '#tcomment',
280-
lang: 'zh-CN',
281-
path: currentPath,
282-
onCommentLoaded: function () {
283-
window.ViewImage && ViewImage.init('.tk-content img:not(.avatar,.tk-avatar-img,.tk-owo-emotion),.ebook img,a.zoom');
284-
}
285-
});
286-
}, 300);
331+
332+
observeAndLoad(currentPath);
287333
})
288334
}
289335
]

0 commit comments

Comments
 (0)