1- import fs from 'fs' ;
2- import path from 'path' ;
1+ import { createHash } from 'crypto' ;
32
43import { cache } from 'react' ;
54import matter from 'gray-matter' ;
65import { s } from 'hastscript' ;
76import yaml from 'js-yaml' ;
87import { bundleMDX } from 'mdx-bundler' ;
8+ import fs from 'node:fs' ;
9+ import { readFile , writeFile } from 'node:fs/promises' ;
10+ import path from 'node:path' ;
911import rehypeAutolinkHeadings from 'rehype-autolink-headings' ;
1012import rehypePresetMinify from 'rehype-preset-minify' ;
1113import rehypePrismDiff from 'rehype-prism-diff' ;
@@ -33,6 +35,10 @@ import {isNotNil} from './utils';
3335import { isVersioned , VERSION_INDICATOR } from './versioning' ;
3436
3537const root = process . cwd ( ) ;
38+ const CACHE_DIR = path . join ( root , '.next' , 'cache' , 'mdx-bundler' ) ;
39+ fs . mkdirSync ( CACHE_DIR , { recursive : true } ) ;
40+
41+ const md5 = data => createHash ( 'md5' ) . update ( data ) . digest ( 'hex' ) ;
3642
3743function formatSlug ( slug : string ) {
3844 return slug . replace ( / \. ( m d x | m d ) / , '' ) ;
@@ -304,8 +310,10 @@ export async function getFileBySlug(slug: string) {
304310 const configPath = path . join ( root , slug . split ( VERSION_INDICATOR ) [ 0 ] , 'config.yml' ) ;
305311
306312 let configFrontmatter : PlatformConfig | undefined ;
307- if ( fs . existsSync ( configPath ) ) {
308- configFrontmatter = yaml . load ( fs . readFileSync ( configPath , 'utf8' ) ) as PlatformConfig ;
313+ try {
314+ configFrontmatter = yaml . load ( await readFile ( configPath , 'utf8' ) ) as PlatformConfig ;
315+ } catch ( e ) {
316+ // If the config file does not exist, we can ignore it.
309317 }
310318
311319 let mdxPath = path . join ( root , `${ slug } .mdx` ) ;
@@ -352,7 +360,20 @@ export async function getFileBySlug(slug: string) {
352360 [ mdxPath , mdxIndexPath , mdPath , versionedMdxIndexPath ] . find ( fs . existsSync ) ??
353361 mdIndexPath ;
354362
355- const source = fs . readFileSync ( sourcePath , 'utf8' ) ;
363+ const source = await readFile ( sourcePath , 'utf8' ) ;
364+ const cacheKey = md5 ( source ) ;
365+ const cacheFile = path . join ( CACHE_DIR , cacheKey ) ;
366+
367+ try {
368+ const cached = JSON . parse ( await readFile ( cacheFile , 'utf8' ) ) ;
369+ return cached ;
370+ } catch ( e ) {
371+ if ( e . code !== 'ENOENT' ) {
372+ // If cache is corrupted, ignore and proceed
373+ // eslint-disable-next-line no-console
374+ console . warn ( `Failed to read MDX cache: ${ cacheFile } ` , e ) ;
375+ }
376+ }
356377
357378 process . env . ESBUILD_BINARY_PATH = path . join (
358379 root ,
@@ -469,7 +490,7 @@ export async function getFileBySlug(slug: string) {
469490 mergedFrontmatter = { ...frontmatter , ...configFrontmatter } ;
470491 }
471492
472- return {
493+ const resultObj = {
473494 matter : result . matter ,
474495 mdxSource : code ,
475496 toc,
@@ -478,6 +499,13 @@ export async function getFileBySlug(slug: string) {
478499 slug,
479500 } ,
480501 } ;
502+
503+ writeFile ( cacheFile , JSON . stringify ( resultObj ) , 'utf8' ) . catch ( e => {
504+ // eslint-disable-next-line no-console
505+ console . warn ( `Failed to write MDX cache: ${ cacheFile } ` , e ) ;
506+ } ) ;
507+
508+ return resultObj ;
481509}
482510
483511/**
0 commit comments