From 45cfc6acc4c376f7995e5540be99cf116bc67e64 Mon Sep 17 00:00:00 2001 From: gbanyan Date: Thu, 20 Nov 2025 20:29:30 +0800 Subject: [PATCH] Fix TOC showing wrong headings across navigation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The TOC component was only extracting headings once at mount, causing it to show stale headings when navigating between posts via client-side routing. Now it re-extracts headings whenever the pathname changes. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- components/post-toc.tsx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/components/post-toc.tsx b/components/post-toc.tsx index 2294f61..6a6908b 100644 --- a/components/post-toc.tsx +++ b/components/post-toc.tsx @@ -1,6 +1,7 @@ 'use client'; import { useEffect, useRef, useState } from 'react'; +import { usePathname } from 'next/navigation'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { faListUl } from '@fortawesome/free-solid-svg-icons'; @@ -16,6 +17,7 @@ export function PostToc({ onLinkClick }: { onLinkClick?: () => void }) { const listRef = useRef(null); const itemRefs = useRef>({}); const [indicator, setIndicator] = useState({ top: 0, opacity: 0 }); + const pathname = usePathname(); useEffect(() => { const headings = Array.from( @@ -51,7 +53,7 @@ export function PostToc({ onLinkClick }: { onLinkClick?: () => void }) { headings.forEach((el) => observer.observe(el)); return () => observer.disconnect(); - }, []); + }, [pathname]); useEffect(() => { if (!activeId || !listRef.current) {