refactor: Use stable cache name with incremental asset updates

Instead of invalidating the entire cache on every deployment (via git hash
in cache name), use a stable cache name and leverage Vite's content hashes
in filenames.

Changes:
- Use fixed cache name 'chatwoot-assets-v1' instead of 'chatwoot-{git-hash}'
- Add cleanupStaleAssets() to remove old assets not in current manifest
- Unchanged files (same content hash) stay cached across deployments
- Only new/changed files are fetched
- Remove unused getCacheVersion() and CACHE_VERSION references

This significantly reduces bandwidth on deployments since most assets
don't change between releases.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Pranav
2026-01-10 16:28:42 -08:00
co-authored by Claude Opus 4.5
parent 0d21a711e7
commit 38232a73dc
2 changed files with 38 additions and 47 deletions
+2 -33
View File
@@ -2,29 +2,8 @@
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
const { build } = require('vite');
/**
* Get the cache version for service worker
* Priority: CHATWOOT_VERSION env var > git commit hash > timestamp
*/
function getCacheVersion() {
if (process.env.CHATWOOT_VERSION) {
return process.env.CHATWOOT_VERSION;
}
try {
const gitHash = execSync('git rev-parse --short HEAD', {
encoding: 'utf8',
}).trim();
return `git-${gitHash}`;
} catch (error) {
console.warn('Could not get git hash, using timestamp');
return `v${Date.now()}`;
}
}
/**
* Generate asset manifest from Vite build output
* Returns array of { url, revision } for JS/CSS files
@@ -75,13 +54,11 @@ function generateAssetManifest() {
async function buildServiceWorker() {
console.log('🔨 Building service worker...');
const cacheVersion = getCacheVersion();
const assetOrigin = process.env.ASSET_CDN_HOST || '';
const isProduction = process.env.NODE_ENV === 'production';
// In production assets are in /packs/, in development they're in /vite-dev/
const assetPath = isProduction ? '/packs/' : '/vite-dev/';
console.log(`📦 Cache version: ${cacheVersion}`);
console.log(`🌐 Asset origin: ${assetOrigin || '(local)'}`);
console.log(`📁 Asset path: ${assetPath}`);
console.log(`🏭 Environment: ${isProduction ? 'production' : 'development'}`);
@@ -144,7 +121,6 @@ async function buildServiceWorker() {
},
},
define: {
__CACHE_VERSION__: JSON.stringify(cacheVersion),
__ASSET_ORIGIN__: JSON.stringify(assetOrigin),
__ASSET_PATH__: JSON.stringify(assetPath),
__ASSET_MANIFEST__: JSON.stringify(assetManifest),
@@ -161,7 +137,7 @@ async function buildServiceWorker() {
'utf8'
);
// Read the push handlers template
// Read the push handlers
const pushHandlers = fs.readFileSync(
path.resolve(
__dirname,
@@ -170,21 +146,14 @@ async function buildServiceWorker() {
'utf8'
);
// Replace version placeholder in push handlers
const processedHandlers = pushHandlers.replace(
/__CACHE_VERSION__/g,
cacheVersion
);
// Combine them
const finalServiceWorker = `/* Service Worker for Chatwoot - Generated at build time */
/* Cache version: ${cacheVersion} */
/* Generated: ${new Date().toISOString()} */
/* Precached assets: ${assetManifest.length} */
${runtimeBundle}
${processedHandlers}
${pushHandlers}
`;
// Write to public/sw.js