diff --git a/app/javascript/service-worker/sw-runtime.js b/app/javascript/service-worker/sw-runtime.js index 2e6270814..b0648377f 100644 --- a/app/javascript/service-worker/sw-runtime.js +++ b/app/javascript/service-worker/sw-runtime.js @@ -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) { diff --git a/lib/tasks/build.rake b/lib/tasks/build.rake index 1d4dacce5..6234873b5 100644 --- a/lib/tasks/build.rake +++ b/lib/tasks/build.rake @@ -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 diff --git a/scripts/build-service-worker.js b/scripts/build-service-worker.js index 7140fdafc..6f91412a7 100644 --- a/scripts/build-service-worker.js +++ b/scripts/build-service-worker.js @@ -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'), },