Skip to content

Commit f090192

Browse files
Copilotna-trium-144
andcommitted
Remove unnecessary fetch from sidebar and clear context on page change
Co-authored-by: na-trium-144 <[email protected]>
1 parent b1af976 commit f090192

File tree

2 files changed

+7
-18
lines changed

2 files changed

+7
-18
lines changed

app/[docs_id]/pageContent.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ export function PageContent(props: PageContentProps) {
4242
}));
4343
setLocalDynamicMdContent(newContent);
4444
setDynamicMdContent(newContent);
45+
46+
// クリーンアップ:コンポーネントがアンマウントされたらcontextをクリア
47+
return () => {
48+
setDynamicMdContent([]);
49+
};
4550
}, [props.splitMdContent, props.docs_id, setDynamicMdContent]);
4651

4752
const sectionRefs = useRef<Array<HTMLDivElement | null>>([]);

app/sidebar.tsx

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,16 @@
11
"use client";
22
import Link from "next/link";
33
import { usePathname } from "next/navigation";
4-
import useSWR, { Fetcher } from "swr";
5-
import { splitMarkdown } from "./[docs_id]/splitMarkdown";
64
import { pagesList } from "./pagesList";
75
import { AccountMenu } from "./accountMenu";
86
import { ThemeToggle } from "./[docs_id]/themeToggle";
97
import { useDynamicMdContext } from "./[docs_id]/dynamicMdContext";
108

11-
const fetcher: Fetcher<string, string> = (url) =>
12-
fetch(url).then((r) => r.text());
13-
149
export function Sidebar() {
1510
const pathname = usePathname();
1611
const docs_id = pathname.replace(/^\//, "");
1712
const { dynamicMdContent } = useDynamicMdContext();
1813

19-
// コンテキストが空の場合(まだページがロードされていない場合)はフェッチする
20-
const { data, error, isLoading } = useSWR(
21-
dynamicMdContent.length === 0 ? `/docs/${docs_id}.md` : null,
22-
fetcher
23-
);
24-
25-
if (error) console.error("Sidebar fetch error:", error);
26-
27-
// コンテキストがある場合はそれを使用、ない場合はフェッチしたデータを使用
28-
const splitmdcontent = dynamicMdContent.length > 0 ? dynamicMdContent : splitMarkdown(data ?? "");
29-
3014
// 現在表示中のセクション(最初にinViewがtrueのもの)を見つける
3115
const currentSectionIndex = dynamicMdContent.findIndex(
3216
(section) => section.inView
@@ -78,9 +62,9 @@ export function Sidebar() {
7862
<span className="mr-0">{page.id}.</span>
7963
{page.title}
8064
</Link>
81-
{`${group.id}-${page.id}` === docs_id && !isLoading && (
65+
{`${group.id}-${page.id}` === docs_id && dynamicMdContent.length > 0 && (
8266
<ul className="ml-4 text-sm">
83-
{splitmdcontent.slice(1).map((section, idx) => {
67+
{dynamicMdContent.slice(1).map((section, idx) => {
8468
// idx + 1 は実際のsectionIndexに対応(slice(1)で最初を除外しているため)
8569
const isCurrentSection = idx + 1 === currentSectionIndex;
8670
return (

0 commit comments

Comments
 (0)