From 7685c79705587b1b5a649916d9d49c065231cc16 Mon Sep 17 00:00:00 2001 From: gbanyan Date: Fri, 21 Nov 2025 00:39:56 +0800 Subject: [PATCH] Fix TOC duplication when navigating --- app/blog/[slug]/page.tsx | 5 ++++- app/pages/[slug]/page.tsx | 5 ++++- components/post-toc.tsx | 12 +++++++++++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/app/blog/[slug]/page.tsx b/app/blog/[slug]/page.tsx index de8d5ac..014277e 100644 --- a/app/blog/[slug]/page.tsx +++ b/app/blog/[slug]/page.tsx @@ -198,7 +198,10 @@ export default async function BlogPostPage({ params }: Props) { -
+
{post.feature_image && (
-
+
{page.feature_image && (
void; // Clear items immediately when content changes setItems([]); setActiveId(null); + itemRefs.current = {}; + + const containerSelector = contentKey + ? `[data-toc-content="${contentKey}"]` + : '[data-toc-content]'; + const container = document.querySelector(containerSelector); + + if (!container) { + return undefined; + } let observer: IntersectionObserver | null = null; let rafId1: number; @@ -30,7 +40,7 @@ export function PostToc({ onLinkClick, contentKey }: { onLinkClick?: () => void; rafId1 = requestAnimationFrame(() => { rafId2 = requestAnimationFrame(() => { const headings = Array.from( - document.querySelectorAll('article h2, article h3') + container.querySelectorAll('h2, h3') ); const mapped = headings .filter((el) => el.id)