Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use updated recursive rm fs method for image-optimizer #34210

Merged
merged 2 commits into from Feb 11, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 22 additions & 9 deletions packages/next/server/image-optimizer.ts
Expand Up @@ -240,14 +240,18 @@ export class ImageOptimizerCache {
Math.max(revalidate, this.nextConfig.images.minimumCacheTTL) * 1000 +
Date.now()

await writeToCacheDir(
join(this.cacheDir, cacheKey),
value.extension,
revalidate,
expireAt,
value.buffer,
value.etag
)
try {
await writeToCacheDir(
join(this.cacheDir, cacheKey),
value.extension,
revalidate,
expireAt,
value.buffer,
value.etag
)
} catch (err) {
console.error(`Failed to write image to cache ${cacheKey}`, err)
}
}
}
export class ImageError extends Error {
Expand Down Expand Up @@ -543,7 +547,16 @@ async function writeToCacheDir(
etag: string
) {
const filename = join(dir, `${maxAge}.${expireAt}.${etag}.${extension}`)
await promises.rmdir(dir, { recursive: true })

// Added in: v14.14.0 https://nodejs.org/api/fs.html#fspromisesrmpath-options
// attempt cleaning up existing stale cache
if ((promises as any).rm) {
await (promises as any)
.rm(dir, { force: true, recursive: true })
.catch(() => {})
} else {
await promises.rmdir(dir, { recursive: true }).catch(() => {})
}
await promises.mkdir(dir, { recursive: true })
await promises.writeFile(filename, buffer)
}
Expand Down