|
| 1 | +import fs from 'node:fs/promises' |
| 2 | +import path from 'node:path' |
| 3 | + |
| 4 | +const INVALIDATION_MARKER = '__nextjs_invalidated_cache' |
| 5 | + |
| 6 | +/** |
| 7 | + * Atomically write an invalidation marker. |
| 8 | + * |
| 9 | + * Because attempting to delete currently open cache files could cause issues, |
| 10 | + * actual deletion of files is deferred until the next start-up (in |
| 11 | + * `checkPersistentCacheInvalidationAndCleanup`). |
| 12 | + * |
| 13 | + * In the case that no database is currently open (e.g. via a separate CLI |
| 14 | + * subcommand), you should call `cleanupPersistentCache` *after* this to eagerly |
| 15 | + * remove the cache files. |
| 16 | + */ |
| 17 | +export async function invalidatePersistentCache(cacheDirectory: string) { |
| 18 | + let file |
| 19 | + try { |
| 20 | + // We're just opening it so that `open()` creates the file. |
| 21 | + file = await fs.open(path.join(cacheDirectory, INVALIDATION_MARKER), 'w') |
| 22 | + // We don't currently write anything to the file, but we could choose to |
| 23 | + // later, e.g. a reason for the invalidation. |
| 24 | + } finally { |
| 25 | + file?.close() |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Called during startup. See if the cache is in a partially-completed |
| 31 | + * invalidation state. Finds and delete any invalidated cache files. |
| 32 | + */ |
| 33 | +export async function checkPersistentCacheInvalidationAndCleanup( |
| 34 | + cacheDirectory: string |
| 35 | +) { |
| 36 | + const invalidated = await fs |
| 37 | + .access(path.join(cacheDirectory, INVALIDATION_MARKER)) |
| 38 | + .then( |
| 39 | + () => true, |
| 40 | + () => false |
| 41 | + ) |
| 42 | + if (invalidated) { |
| 43 | + await cleanupPersistentCache(cacheDirectory) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * Helper for `checkPersistentCacheInvalidationAndCleanup`. You can call this to |
| 49 | + * explicitly clean up a database after running `invalidatePersistentCache` when |
| 50 | + * webpack is not running. |
| 51 | + * |
| 52 | + * You should not run this if the cache has not yet been invalidated, as this |
| 53 | + * operation is not atomic and could result in a partially-deleted and corrupted |
| 54 | + * database. |
| 55 | + */ |
| 56 | +async function cleanupPersistentCache(cacheDirectory: string) { |
| 57 | + try { |
| 58 | + await cleanupPersistentCacheInner(cacheDirectory) |
| 59 | + } catch (e) { |
| 60 | + // generate a user-friendly error message |
| 61 | + throw new Error( |
| 62 | + `Unable to remove an invalidated webpack cache. If this issue persists ` + |
| 63 | + `you can work around it by deleting ${cacheDirectory}`, |
| 64 | + { cause: e } |
| 65 | + ) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +async function cleanupPersistentCacheInner(cacheDirectory: string) { |
| 70 | + const files = await fs.readdir(cacheDirectory) |
| 71 | + |
| 72 | + // delete everything except the invalidation marker |
| 73 | + await Promise.all( |
| 74 | + files.map((name) => |
| 75 | + name !== INVALIDATION_MARKER |
| 76 | + ? fs.rm(path.join(cacheDirectory, name), { |
| 77 | + force: true, // ignore errors if path does not exist |
| 78 | + recursive: true, |
| 79 | + maxRetries: 2, // windows prevents deletion of open files |
| 80 | + }) |
| 81 | + : null |
| 82 | + ) |
| 83 | + ) |
| 84 | + |
| 85 | + // delete the invalidation marker last, once we're sure everything is cleaned |
| 86 | + // up |
| 87 | + await fs.rm(path.join(cacheDirectory, INVALIDATION_MARKER), { |
| 88 | + force: true, |
| 89 | + maxRetries: 2, |
| 90 | + }) |
| 91 | +} |
0 commit comments