pi0 / nuxt-build-cache

▣ Speed up your Nuxt builds up to 2x [experimental]
147 stars 1 forks source link

globby results are returned in arbitrary order causing random cache miss #7

Open simkuns opened 3 months ago

simkuns commented 3 months ago

In my project I have noticed that most of the time there is cache miss (No build cache found).

Upon further investigation I noticed that contents of the hashes.json for all builds is the same except file order changes.


I checked the implementation of readFilesRecursive and it uses globby which is based on fast-glob. https://github.com/sindresorhus/globby?tab=readme-ov-file#globby https://github.com/mrmlnc/fast-glob?tab=readme-ov-file#fast-glob

Screenshot 2024-06-04 at 20 09 18

Adding .sort((a, b) => a.name.localeCompare(b.name)) to https://github.com/pi0/nuxt-build-cache/blob/d77afa9f43c5366c074e02be69360d5bbc5c8b3b/src/cache.ts#L39 fixes the issue.

simkuns commented 3 months ago

nuxt-build-cache+0.1.1.patch

diff --git a/node_modules/nuxt-build-cache/dist/module.mjs b/node_modules/nuxt-build-cache/dist/module.mjs
index 189fcc1..2b64ee3 100644
--- a/node_modules/nuxt-build-cache/dist/module.mjs
+++ b/node_modules/nuxt-build-cache/dist/module.mjs
@@ -79,7 +79,7 @@ async function getHashes(nuxt) {
         f.data
         /* ArrayBuffer */
       )
-    }));
+    })).sort((a, b) => a.name.localeCompare(b.name));
     const sourceFiles = await readFilesRecursive(layer.config?.srcDir, {
       shouldIgnore: isIgnored,
       // TODO: Validate if works with absolute paths