fix: Build service worker after assets to include manifest

Two issues were causing the service worker to not cache assets in production:

1. The prefetch baseUrl was hardcoded to /vite-dev/ which only exists in
   development. In production, assets are served from /packs/. Added
   ASSET_PATH build-time constant to use the correct path per environment.

2. The service worker was built before assets:precompile, so the Vite
   manifest didn't exist yet, resulting in 0 precached assets. Moved
   the SW build to after_assets_precompile so it can read the manifest.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Pranav
2026-01-10 16:18:00 -08:00
co-authored by Claude Opus 4.5
parent f5d538ba48
commit 0d21a711e7
3 changed files with 16 additions and 5 deletions
+2 -1
View File
@@ -3,6 +3,7 @@
// Build-time injected values (replaced by Vite define at build time)
const CACHE_VERSION = __CACHE_VERSION__;
const ASSET_ORIGIN = __ASSET_ORIGIN__;
const ASSET_PATH = __ASSET_PATH__;
const ASSET_MANIFEST = __ASSET_MANIFEST__;
const CACHE_NAME = `chatwoot-${CACHE_VERSION}`;
@@ -121,7 +122,7 @@ async function prefetchAssets() {
if (!ASSET_MANIFEST?.length) return;
const cache = await caches.open(CACHE_NAME);
const baseUrl = ASSET_ORIGIN || `${self.location.origin}/vite-dev/`;
const baseUrl = ASSET_ORIGIN || `${self.location.origin}${ASSET_PATH}`;
// Prefetch in batches of 5 (await in loop is intentional for throttling)
for (let i = 0; i < ASSET_MANIFEST.length; i += 5) {
+10 -4
View File
@@ -5,11 +5,17 @@ task before_assets_precompile: :environment do
system('pnpm install')
system('echo "-------------- Bulding SDK for Production --------------"')
system('pnpm run build:sdk')
system('echo "-------------- Building Service Worker --------------"')
system('NODE_ENV=production pnpm run build:sw')
system('echo "-------------- Bulding App for Production --------------"')
end
task after_assets_precompile: :environment do
# Build service worker after Vite has generated the asset manifest
system('echo "-------------- Building Service Worker --------------"')
system('NODE_ENV=production pnpm run build:sw')
end
# every time you execute 'rake assets:precompile'
# run 'before_assets_precompile' first
Rake::Task['assets:precompile'].enhance %w[before_assets_precompile]
# run 'before_assets_precompile' first, then 'after_assets_precompile' at the end
Rake::Task['assets:precompile'].enhance %w[before_assets_precompile] do
Rake::Task['after_assets_precompile'].invoke
end
+4
View File
@@ -78,9 +78,12 @@ async function buildServiceWorker() {
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'}`);
// In development mode, just copy the push-handlers-only version
@@ -143,6 +146,7 @@ async function buildServiceWorker() {
define: {
__CACHE_VERSION__: JSON.stringify(cacheVersion),
__ASSET_ORIGIN__: JSON.stringify(assetOrigin),
__ASSET_PATH__: JSON.stringify(assetPath),
__ASSET_MANIFEST__: JSON.stringify(assetManifest),
'process.env.NODE_ENV': JSON.stringify('production'),
},