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) {