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>
22 lines
949 B
Ruby
22 lines
949 B
Ruby
# ref: https://github.com/rails/rails/issues/43906#issuecomment-1094380699
|
|
# https://github.com/rails/rails/issues/43906#issuecomment-1099992310
|
|
task before_assets_precompile: :environment do
|
|
# run a command which starts your packaging
|
|
system('pnpm install')
|
|
system('echo "-------------- Bulding SDK for Production --------------"')
|
|
system('pnpm run build:sdk')
|
|
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, then 'after_assets_precompile' at the end
|
|
Rake::Task['assets:precompile'].enhance %w[before_assets_precompile] do
|
|
Rake::Task['after_assets_precompile'].invoke
|
|
end
|