fix(blog): reduce list-to-post navigation lag
This commit is contained in:
@@ -13,12 +13,11 @@ export default function Comments({ lang = 'en', ...props }: CommentsProps) {
|
||||
const containerRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
const walineLang = lang === 'zh' ? 'zh-CN' : 'en';
|
||||
const getTheme = () => (document.documentElement.classList.contains('dark') ? 'html.dark' : false);
|
||||
|
||||
useEffect(() => {
|
||||
if (!containerRef.current) return;
|
||||
|
||||
const getTheme = () => document.documentElement.classList.contains('dark') ? 'html.dark' : false;
|
||||
|
||||
const pathname = window.location.pathname;
|
||||
const postsMatch = pathname.match(/\/posts\/([^/]+)/);
|
||||
const path = postsMatch ? postsMatch[1] : pathname;
|
||||
@@ -34,18 +33,6 @@ export default function Comments({ lang = 'en', ...props }: CommentsProps) {
|
||||
path,
|
||||
});
|
||||
|
||||
return () => walineInstanceRef.current?.destroy();
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
const getTheme = () => document.documentElement.classList.contains('dark') ? 'html.dark' : false;
|
||||
|
||||
walineInstanceRef.current?.update({
|
||||
...props,
|
||||
lang: walineLang,
|
||||
dark: getTheme(),
|
||||
});
|
||||
|
||||
const handleThemeChange = () => {
|
||||
walineInstanceRef.current?.update({
|
||||
...props,
|
||||
@@ -57,8 +44,22 @@ export default function Comments({ lang = 'en', ...props }: CommentsProps) {
|
||||
const observer = new MutationObserver(handleThemeChange);
|
||||
observer.observe(document.documentElement, { attributes: true, attributeFilter: ['class'] });
|
||||
|
||||
return () => observer.disconnect();
|
||||
}, [lang, props, walineLang]);
|
||||
return () => {
|
||||
observer.disconnect();
|
||||
const instance = walineInstanceRef.current;
|
||||
walineInstanceRef.current = null;
|
||||
|
||||
if (instance) {
|
||||
try {
|
||||
instance.destroy();
|
||||
} catch {
|
||||
if (containerRef.current) {
|
||||
containerRef.current.innerHTML = '';
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}, [props, walineLang]);
|
||||
|
||||
return <div ref={containerRef} className="waline-container" />;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user