Skip to content

Commit 5e97f5a

Browse files
committed
cleanup old cache files
1 parent 4f15275 commit 5e97f5a

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

scripts/generate-md-exports.mjs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,38 @@ async function createWork() {
9393
if (noCache) {
9494
console.log(`ℹ️ No cache directory found, this will take a while...`);
9595
await mkdir(CACHE_DIR, {recursive: true});
96+
} else {
97+
// Clean up old cache files to prevent unbounded growth
98+
// Keep files accessed within last 7 days only
99+
const MAX_CACHE_AGE_MS = 7 * 24 * 60 * 60 * 1000; // 7 days
100+
const now = Date.now();
101+
let cleanedCount = 0;
102+
103+
try {
104+
const {readdir, stat, rm} = await import('node:fs/promises');
105+
const files = await readdir(CACHE_DIR);
106+
107+
for (const file of files) {
108+
const filePath = path.join(CACHE_DIR, file);
109+
try {
110+
const stats = await stat(filePath);
111+
const age = now - stats.atimeMs; // Time since last access
112+
113+
if (age > MAX_CACHE_AGE_MS) {
114+
await rm(filePath, {force: true});
115+
cleanedCount++;
116+
}
117+
} catch (err) {
118+
// Skip files we can't stat/delete
119+
}
120+
}
121+
122+
if (cleanedCount > 0) {
123+
console.log(`🧹 Cleaned up ${cleanedCount} old cache files (>7 days)`);
124+
}
125+
} catch (err) {
126+
console.warn('Failed to clean cache:', err);
127+
}
96128
}
97129

98130
// On a 16-core machine, 8 workers were optimal (and slightly faster than 16)

src/mdx.ts

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

69106
const md5 = (data: BinaryLike) => createHash('md5').update(data).digest('hex');

0 commit comments

Comments
 (0)