11import fs from 'fs' ;
22
3- import { useMemo } from 'react' ;
3+ import { cache , useMemo } from 'react' ;
44import { getMDXComponent } from 'mdx-bundler/client' ;
55
66import { getCurrentGuide , getDocsRootNode , getPlatform } from 'sentry-docs/docTree' ;
7- import { getFileBySlug } from 'sentry-docs/mdx' ;
7+ import { getFileBySlugWithCache } from 'sentry-docs/mdx' ;
88import { mdxComponents } from 'sentry-docs/mdxComponents' ;
99import { serverContext } from 'sentry-docs/serverContext' ;
1010import {
@@ -24,7 +24,7 @@ type Props = {
2424 platform ?: string ;
2525} ;
2626
27- const udpatePathIfVersionedFileDoesNotExist = ( path : string ) : string => {
27+ const updatePathIfVersionedFileDoesNotExist = ( path : string ) : string => {
2828 if ( ! isVersioned ( path ) ) {
2929 return path ;
3030 }
@@ -39,6 +39,21 @@ const udpatePathIfVersionedFileDoesNotExist = (path: string): string => {
3939 return path ;
4040} ;
4141
42+ /**
43+ * Cache the result of updatePathIfVersionedFileDoesNotExist
44+ * to avoid calling it multiple times for the same path.
45+ *
46+ * This is important because we want to skip the `fs.existsSync` call if possible.
47+ */
48+ const updatePathIfVersionedFileDoesNotExistWithCache = cache (
49+ updatePathIfVersionedFileDoesNotExist
50+ ) ;
51+
52+ function MDXLayoutRenderer ( { mdxSource : source , ...rest } ) {
53+ const MDXLayout = useMemo ( ( ) => getMDXComponent ( source ) , [ source ] ) ;
54+ return < MDXLayout components = { mdxComponentsWithWrapper } { ...rest } /> ;
55+ }
56+
4257export async function PlatformContent ( { includePath, platform, noGuides} : Props ) {
4358 const { path} = serverContext ( ) ;
4459
@@ -54,15 +69,15 @@ export async function PlatformContent({includePath, platform, noGuides}: Props)
5469 guide = `${ platform } .${ path [ 3 ] } ` ;
5570 }
5671
57- let doc : Awaited < ReturnType < typeof getFileBySlug > > | null = null ;
72+ let doc : Awaited < ReturnType < typeof getFileBySlugWithCache > > | undefined ;
5873
5974 if ( guide ) {
60- const guidePath = udpatePathIfVersionedFileDoesNotExist (
75+ const guidePath = updatePathIfVersionedFileDoesNotExistWithCache (
6176 `platform-includes/${ includePath } /${ guide } `
6277 ) ;
6378
6479 try {
65- doc = await getFileBySlug ( guidePath ) ;
80+ doc = await getFileBySlugWithCache ( guidePath ) ;
6681 } catch ( e ) {
6782 // It's fine - keep looking.
6883 }
@@ -72,13 +87,13 @@ export async function PlatformContent({includePath, platform, noGuides}: Props)
7287 const rootNode = await getDocsRootNode ( ) ;
7388 const guideObject = getCurrentGuide ( rootNode , path ) ;
7489
75- const fallbackGuidePath = udpatePathIfVersionedFileDoesNotExist (
90+ const fallbackGuidePath = updatePathIfVersionedFileDoesNotExistWithCache (
7691 `platform-includes/${ includePath } /${ guideObject ?. fallbackGuide } ${ VERSION_INDICATOR } ${ getVersion ( guide || '' ) } `
7792 ) ;
7893
7994 if ( guideObject ?. fallbackGuide ) {
8095 try {
81- doc = await getFileBySlug ( fallbackGuidePath ) ;
96+ doc = await getFileBySlugWithCache ( fallbackGuidePath ) ;
8297 } catch ( e ) {
8398 // It's fine - keep looking.
8499 }
@@ -87,11 +102,11 @@ export async function PlatformContent({includePath, platform, noGuides}: Props)
87102
88103 if ( ! doc ) {
89104 try {
90- const platformPath = udpatePathIfVersionedFileDoesNotExist (
105+ const platformPath = updatePathIfVersionedFileDoesNotExistWithCache (
91106 `platform-includes/${ includePath } /${ platform } `
92107 ) ;
93108
94- doc = await getFileBySlug ( platformPath ) ;
109+ doc = await getFileBySlugWithCache ( platformPath ) ;
95110 } catch ( e ) {
96111 // It's fine - keep looking.
97112 }
@@ -101,13 +116,13 @@ export async function PlatformContent({includePath, platform, noGuides}: Props)
101116 const rootNode = await getDocsRootNode ( ) ;
102117 const platformObject = getPlatform ( rootNode , platform ) ;
103118
104- const fallbackPlatformPath = udpatePathIfVersionedFileDoesNotExist (
119+ const fallbackPlatformPath = updatePathIfVersionedFileDoesNotExistWithCache (
105120 `platform-includes/${ includePath } /${ platformObject ?. fallbackPlatform } `
106121 ) ;
107122
108123 if ( platformObject ?. fallbackPlatform ) {
109124 try {
110- doc = await getFileBySlug ( fallbackPlatformPath ) ;
125+ doc = await getFileBySlugWithCache ( fallbackPlatformPath ) ;
111126 } catch ( e ) {
112127 // It's fine - keep looking.
113128 }
@@ -116,18 +131,14 @@ export async function PlatformContent({includePath, platform, noGuides}: Props)
116131
117132 if ( ! doc ) {
118133 try {
119- doc = await getFileBySlug ( `platform-includes/${ includePath } /_default` ) ;
134+ doc = await getFileBySlugWithCache ( `platform-includes/${ includePath } /_default` ) ;
120135 } catch ( e ) {
121136 // Couldn't find anything.
122137 return null ;
123138 }
124139 }
125140
126141 const { mdxSource} = doc ;
127- function MDXLayoutRenderer ( { mdxSource : source , ...rest } ) {
128- const MDXLayout = useMemo ( ( ) => getMDXComponent ( source ) , [ source ] ) ;
129- return < MDXLayout components = { mdxComponentsWithWrapper } { ...rest } /> ;
130- }
131142 return < MDXLayoutRenderer mdxSource = { mdxSource } /> ;
132143}
133144
0 commit comments