From 6fcf10c1af627e48a564454baf2202fe87331b1b Mon Sep 17 00:00:00 2001 From: Reggi Date: Wed, 18 Dec 2024 12:01:43 -0500 Subject: [PATCH] fix: drop cache for new majors (#1419) currently when new majors are introduced we need to shift around branches / older versions need to be rebuild, so we invalidate the cache here when a new major is introduced --- cli/lib/build.js | 6 ++++++ cli/lib/cache.js | 13 +++++++++++++ 2 files changed, 19 insertions(+) diff --git a/cli/lib/build.js b/cli/lib/build.js index 4f7b6be5298..ae3fae9b77f 100644 --- a/cli/lib/build.js +++ b/cli/lib/build.js @@ -113,6 +113,12 @@ const main = async ({loglevel, releases: rawReleases, useCurrent, navPath, conte } }) + /** + * this voids the cache when a new version is added to release.json / not in the cli-cache.json + * this is done so that the previous major versions nav can be reset to legacy and pages can be droped from its variant + */ + cache?.voidOnNewKey(releases.map(v => v.id)) + const updates = await Promise.all( releases.map(r => extractRelease(r, {cache, contentPath, baseNav: navData, prerelease})), ).then(r => r.filter(Boolean)) diff --git a/cli/lib/cache.js b/cli/lib/cache.js index f670b63100d..ead0f201c4c 100644 --- a/cli/lib/cache.js +++ b/cli/lib/cache.js @@ -2,6 +2,8 @@ const fs = require('fs/promises') /** cache npm cli version shas to NOT pull down changes we already have */ class CacheVersionSha { + shouldVoid = false + constructor(cache, path) { this.cache = cache this.path = path @@ -11,6 +13,16 @@ class CacheVersionSha { return new CacheVersionSha(JSON.parse(await fs.readFile(path, 'utf-8')), path) } + get keys() { + return Object.keys(this.cache) + } + + voidOnNewKey(keys) { + if (keys.length !== this.keys.length || !keys.every(key => this.keys.includes(key))) { + this.shouldVoid = true + } + } + async save() { const sortedCache = {} Object.keys(this.cache) @@ -33,6 +45,7 @@ class CacheVersionSha { } same(id, value) { + if (this.shouldVoid) return false return this.cache[id] === value } }