Skip to content

Commit a59d060

Browse files
committed
fix: improve manifest reading and caching logic; invalidate cache on manifest changes
1 parent 4ecd231 commit a59d060

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

lib/vite-helpers.js

+20-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const fs = require('fs')
22
const path = require('path')
33

44
let manifest = null
5+
const assetsCache = {}
56

67
// Configuration for manual CSS ordering
78
const CSS_ORDER_CONFIG = {
@@ -26,18 +27,22 @@ function readManifest () {
2627
if (process.env.NODE_ENV !== 'production') {
2728
return {} // No manifest needed in dev
2829
}
29-
if (manifest) {
30-
return manifest
31-
}
30+
3231
try {
3332
const manifestPath = path.resolve(__dirname, '../public/build/.vite/manifest.json')
34-
if (fs.existsSync(manifestPath)) {
35-
manifest = JSON.parse(fs.readFileSync(manifestPath, 'utf-8'))
36-
return manifest
37-
} else {
33+
if (!fs.existsSync(manifestPath)) {
3834
console.error('Vite manifest.json not found! Run `npm run build`.')
3935
return {}
4036
}
37+
38+
// Check if manifest has changed
39+
const currentManifest = JSON.parse(fs.readFileSync(manifestPath, 'utf-8'))
40+
if (!manifest || JSON.stringify(manifest) !== JSON.stringify(currentManifest)) {
41+
manifest = currentManifest
42+
// Invalidate cache when manifest changes
43+
Object.keys(assetsCache).forEach(key => delete assetsCache[key])
44+
}
45+
return manifest
4146
} catch (err) {
4247
console.error('Error reading Vite manifest.json:', err)
4348
return {}
@@ -46,6 +51,11 @@ function readManifest () {
4651

4752
// Helper function to get asset paths for an entry point
4853
function getViteAssets(entryName) {
54+
// Check cache first
55+
if (assetsCache[entryName]) {
56+
return assetsCache[entryName];
57+
}
58+
4959
const manifestData = readManifest();
5060
const assets = { js: [], css: [] };
5161
const isProduction = process.env.NODE_ENV === 'production';
@@ -84,6 +94,9 @@ function getViteAssets(entryName) {
8494
assets.css = orderedCss;
8595
}
8696

97+
// Cache the result
98+
assetsCache[entryName] = assets;
99+
87100
return assets;
88101
}
89102

0 commit comments

Comments
 (0)