Skip to content

Commit f6ae6ff

Browse files
committed
Fix navigation with children's.
1 parent 62b7027 commit f6ae6ff

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

app/docs/[...slug]/page.tsx

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import matter from "gray-matter";
55
import type { Metadata } from "next";
66
import { notFound } from "next/navigation";
77
import { MDXRemote } from "next-mdx-remote/rsc";
8-
import { Suspense, cache } from "react";
8+
import { Suspense } from "react";
99

1010
import { DocsHeader } from "@/components/docs/content/DocsHeader";
1111
import { DocsNavigation } from "@/components/docs/content/DocsNavigation";
@@ -34,27 +34,39 @@ interface DocNavigation {
3434
next: { title: string; path: string } | null;
3535
}
3636

37-
const getFlatDocs = cache(() => {
37+
function getFlatDocs(): { title: string; path: string }[] {
3838
function flattenDocs(
3939
structure: {
4040
title: string;
4141
path: string;
42-
children?: { title: string; path: string }[];
42+
children?: {
43+
title: string;
44+
path: string;
45+
children?: any[];
46+
}[];
4347
}[]
4448
): { title: string; path: string }[] {
45-
return structure.reduce<{ title: string; path: string }[]>((acc, item) => {
46-
if (item.children?.length) {
47-
acc.push(...item.children);
48-
} else {
49-
acc.push({ title: item.title, path: item.path });
49+
const result: { title: string; path: string }[] = [];
50+
51+
for (const item of structure) {
52+
const hasChildren = item.children && item.children.length > 0;
53+
54+
if (hasChildren) {
55+
result.push(...flattenDocs(item.children!));
5056
}
51-
return acc;
52-
}, []);
57+
58+
if (!hasChildren) {
59+
result.push({ title: item.title, path: item.path });
60+
}
61+
}
62+
63+
return result;
5364
}
65+
5466
return flattenDocs(docsStructure);
55-
});
67+
}
5668

57-
const getDocBySlug = cache(async (slug: string[]): Promise<Doc | null> => {
69+
async function getDocBySlug(slug: string[]): Promise<Doc | null> {
5870
const docsDirectory = path.join(process.cwd(), "content/docs");
5971
const fullPath = path.join(docsDirectory, slug.join("/") + ".md");
6072

@@ -74,16 +86,16 @@ const getDocBySlug = cache(async (slug: string[]): Promise<Doc | null> => {
7486
console.error(`Error reading doc file ${fullPath}:`, error);
7587
return null;
7688
}
77-
});
89+
}
7890

79-
const getDocNavigation = cache((currentPath: string): DocNavigation => {
91+
function getDocNavigation(currentPath: string): DocNavigation {
8092
const flatDocs = getFlatDocs();
8193
const currentIndex = flatDocs.findIndex((doc) => doc.path === currentPath);
8294
return {
8395
prev: currentIndex > 0 ? flatDocs[currentIndex - 1] : null,
8496
next: currentIndex < flatDocs.length - 1 ? flatDocs[currentIndex + 1] : null,
8597
};
86-
});
98+
}
8799

88100
interface Props {
89101
params: Promise<{

0 commit comments

Comments
 (0)