@@ -5,7 +5,7 @@ import matter from "gray-matter";
5
5
import type { Metadata } from "next" ;
6
6
import { notFound } from "next/navigation" ;
7
7
import { MDXRemote } from "next-mdx-remote/rsc" ;
8
- import { Suspense , cache } from "react" ;
8
+ import { Suspense } from "react" ;
9
9
10
10
import { DocsHeader } from "@/components/docs/content/DocsHeader" ;
11
11
import { DocsNavigation } from "@/components/docs/content/DocsNavigation" ;
@@ -34,27 +34,39 @@ interface DocNavigation {
34
34
next : { title : string ; path : string } | null ;
35
35
}
36
36
37
- const getFlatDocs = cache ( ( ) => {
37
+ function getFlatDocs ( ) : { title : string ; path : string } [ ] {
38
38
function flattenDocs (
39
39
structure : {
40
40
title : string ;
41
41
path : string ;
42
- children ?: { title : string ; path : string } [ ] ;
42
+ children ?: {
43
+ title : string ;
44
+ path : string ;
45
+ children ?: any [ ] ;
46
+ } [ ] ;
43
47
} [ ]
44
48
) : { 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 ! ) ) ;
50
56
}
51
- return acc ;
52
- } , [ ] ) ;
57
+
58
+ if ( ! hasChildren ) {
59
+ result . push ( { title : item . title , path : item . path } ) ;
60
+ }
61
+ }
62
+
63
+ return result ;
53
64
}
65
+
54
66
return flattenDocs ( docsStructure ) ;
55
- } ) ;
67
+ }
56
68
57
- const getDocBySlug = cache ( async ( slug : string [ ] ) : Promise < Doc | null > = > {
69
+ async function getDocBySlug ( slug : string [ ] ) : Promise < Doc | null > {
58
70
const docsDirectory = path . join ( process . cwd ( ) , "content/docs" ) ;
59
71
const fullPath = path . join ( docsDirectory , slug . join ( "/" ) + ".md" ) ;
60
72
@@ -74,16 +86,16 @@ const getDocBySlug = cache(async (slug: string[]): Promise<Doc | null> => {
74
86
console . error ( `Error reading doc file ${ fullPath } :` , error ) ;
75
87
return null ;
76
88
}
77
- } ) ;
89
+ }
78
90
79
- const getDocNavigation = cache ( ( currentPath : string ) : DocNavigation => {
91
+ function getDocNavigation ( currentPath : string ) : DocNavigation {
80
92
const flatDocs = getFlatDocs ( ) ;
81
93
const currentIndex = flatDocs . findIndex ( ( doc ) => doc . path === currentPath ) ;
82
94
return {
83
95
prev : currentIndex > 0 ? flatDocs [ currentIndex - 1 ] : null ,
84
96
next : currentIndex < flatDocs . length - 1 ? flatDocs [ currentIndex + 1 ] : null ,
85
97
} ;
86
- } ) ;
98
+ }
87
99
88
100
interface Props {
89
101
params : Promise < {
0 commit comments