@@ -63,6 +63,43 @@ const CACHE_COMPRESS_LEVEL = 4;
6363const CACHE_DIR = path . join ( root , '.next' , 'cache' , 'mdx-bundler' ) ;
6464if ( process . env . CI ) {
6565 mkdirSync ( CACHE_DIR , { recursive : true } ) ;
66+
67+ // Clean up old cache files in background to prevent unbounded growth
68+ // This runs once per worker process and doesn't block the build
69+ ( async ( ) => {
70+ try {
71+ const MAX_CACHE_AGE_MS = 7 * 24 * 60 * 60 * 1000 ; // 7 days
72+ const now = Date . now ( ) ;
73+ let cleanedCount = 0 ;
74+
75+ const { opendir, rm, stat} = await import ( 'node:fs/promises' ) ;
76+ const dir = await opendir ( CACHE_DIR ) ;
77+
78+ for await ( const dirent of dir ) {
79+ if ( ! dirent . isFile ( ) && ! dirent . isDirectory ( ) ) continue ;
80+
81+ const itemPath = path . join ( CACHE_DIR , dirent . name ) ;
82+ try {
83+ const stats = await stat ( itemPath ) ;
84+ const age = now - stats . atimeMs ; // Time since last access
85+
86+ if ( age > MAX_CACHE_AGE_MS ) {
87+ await rm ( itemPath , { recursive : true , force : true } ) ;
88+ cleanedCount ++ ;
89+ }
90+ } catch ( err ) {
91+ // Skip items we can't stat/delete
92+ }
93+ }
94+
95+ if ( cleanedCount > 0 ) {
96+ // eslint-disable-next-line no-console
97+ console . log ( `🧹 MDX cache: Cleaned up ${ cleanedCount } old items (>7 days)` ) ;
98+ }
99+ } catch ( err ) {
100+ // Silently fail - cache cleanup is not critical
101+ }
102+ } ) ( ) ;
66103}
67104
68105const md5 = ( data : BinaryLike ) => createHash ( 'md5' ) . update ( data ) . digest ( 'hex' ) ;
0 commit comments