Commit Graph
13 Commits
Author SHA1 Message Date
Pranav 9d32e27c98 Add a check with http 2026-01-11 11:31:32 -08:00
Pranav 3527c07e94 Cleanup the code to make more readable 2026-01-11 11:18:17 -08:00
PranavandClaude Opus 4.5 b345826566 refactor: Simplify prefetch by moving to activate event
Remove message-based prefetch in favor of prefetching on activate:
- Remove PREFETCH_ASSETS message listener from service worker
- Remove prefetchAssets() call and idle callback from pushHelper.js
- Call prefetchAssets() directly in activate event

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-10 21:14:35 -08:00
PranavandClaude Opus 4.5 6d3d76982a refactor: Remove HTML shell caching for navigation
Remove networkFirstWithShellCache - offline shell caching has limited
value for a real-time chat app that requires network connectivity.

The service worker now only caches static assets (JS, CSS, fonts, images).

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-10 20:58:13 -08:00
PranavandClaude Opus 4.5 59427c4de3 fix: Include assets (fonts, images) from manifest
Add entry.assets array to precache list for fonts and images.
Increases precached assets from 69 to 86.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-10 20:54:05 -08:00
PranavandClaude Opus 4.5 7787ec1265 fix: Include CSS files from manifest css arrays
The Vite manifest has CSS files in two places:
- entry.file (main CSS files)
- entry.css (CSS imported by JS modules)

The previous code only read entry.file, missing CSS from entry.css.
This increases precached assets from 57 to 69.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-10 20:51:27 -08:00
PranavandClaude Opus 4.5 8daac4cdd1 fix: Construct CDN asset URLs correctly with protocol and path
- Add https:// protocol to ASSET_CDN_HOST if missing
- Include asset path (/vite/) when constructing CDN URLs
- Fix URL: https://cdn.example.com/vite/assets/file.css

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-10 20:13:39 -08:00
PranavandClaude Opus 4.5 adbee23e12 refactor: Colocate service worker build script with source files
Move build script from scripts/build-service-worker.js to
app/javascript/service-worker/build.js to keep all service worker
related code in one place.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-10 19:30:15 -08:00
PranavandClaude Opus 4.5 38232a73dc 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>
2026-01-10 16:28:42 -08:00
PranavandClaude Opus 4.5 0d21a711e7 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>
2026-01-10 16:18:00 -08:00
PranavandClaude Opus 4.5 f5d538ba48 fix: Use bare identifiers for Vite define replacement in service worker
Vite's define option replaces identifiers, not string literals.
Changed from '__CACHE_VERSION__' (string) to __CACHE_VERSION__ (identifier)
so Vite can properly inject the values at build time.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-10 15:38:12 -08:00
PranavandClaude Opus 4.5 f713678328 refactor: Simplify service worker implementation
Remove over-engineering from asset caching service worker:
- Remove retry logic, stats tracking, unused message handlers
- Inline helper functions used only once
- Reduce sw-runtime.js from 314 to 170 lines (46% reduction)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-10 15:02:34 -08:00
PranavandClaude Opus 4.5 d34df7c314 feat: Add service worker for asset caching
- Add Workbox-based service worker to cache JS, CSS, and fonts
- JS/CSS cached for 30 days, fonts for 1 year
- Support for CDN assets via ASSET_CDN_HOST
- Version-based cache invalidation on deployments
- Automatic cache cleanup for old versions
- Build service worker during asset precompilation

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-10 14:48:23 -08:00