Skip to content

Open source personal finance application

fix: rewrite SW with immediate precache and StaleWhileRevalidate

Ilya Komichev8132ca0b6017
diff --git a/app/layouts/default.vue b/app/layouts/default.vue
index 733e01ad..8390b4b1 100644
--- a/app/layouts/default.vue
+++ b/app/layouts/default.vue
@@ -38,7 +38,6 @@ const { error, status } = useAsyncData(
loadDataFromDB()
}
- triggerServiceWorkerPrecache()
},
{
lazy: true,
@@ -46,22 +45,6 @@ const { error, status } = useAsyncData(
},
)
-function triggerServiceWorkerPrecache() {
- if ('serviceWorker' in navigator) {
- navigator.serviceWorker.ready.then(async (reg) => {
- reg.active?.postMessage({ type: 'PRECACHE_APP' })
-
- // Warm pages cache: ensure current page is available offline
- try {
- const cache = await caches.open('pages')
- if (!await cache.match(location.href))
- await cache.add(location.href)
- }
- catch {}
- })
- }
-}
-
defineShortcuts({
'escape': () => {
if (trnsFormStore.ui.isShow)
diff --git a/app/sw.ts b/app/sw.ts
index 2f4142da..9ee7df44 100644
--- a/app/sw.ts
+++ b/app/sw.ts
@@ -1,22 +1,15 @@
import { clientsClaim } from 'workbox-core'
-import { cleanupOutdatedCaches, PrecacheController, PrecacheRoute } from 'workbox-precaching'
+import { cleanupOutdatedCaches, precacheAndRoute } from 'workbox-precaching'
import { NavigationRoute, registerRoute, setCatchHandler } from 'workbox-routing'
-import { CacheFirst, NetworkFirst } from 'workbox-strategies'
+import { CacheFirst, StaleWhileRevalidate } from 'workbox-strategies'
declare let self: ServiceWorkerGlobalScope
self.skipWaiting()
clientsClaim()
-// Set up precache controller with build manifest (injected by vite-plugin-pwa)
-// Using addToCacheList (NOT precache) — does NOT register install/activate listeners
-const pc = new PrecacheController()
-pc.addToCacheList(self.__WB_MANIFEST)
-
-// Route: serve precached files when available
-registerRoute(new PrecacheRoute(pc))
-
-// Cleanup caches from previous SW versions
+// Precache static assets (JS, CSS, fonts, images) immediately on install
+precacheAndRoute(self.__WB_MANIFEST)
cleanupOutdatedCaches()
// Only cache 200 OK responses (skip redirects, opaque, error responses)
@@ -25,16 +18,16 @@ const cacheOkOnly = {
response.status === 200 ? response : null,
}
-// Navigation: network-first with offline fallback to cached HTML
-registerRoute(new NavigationRoute(new NetworkFirst({
+// Navigation: serve cached HTML instantly, update in background
+registerRoute(new NavigationRoute(new StaleWhileRevalidate({
cacheName: 'pages',
plugins: [cacheOkOnly],
})))
-// i18n locale messages: network-first for offline support
+// i18n locale messages: serve cached, update in background
registerRoute(
({ url }) => url.pathname.startsWith('/_i18n/'),
- new NetworkFirst({ cacheName: 'i18n', plugins: [cacheOkOnly] }),
+ new StaleWhileRevalidate({ cacheName: 'i18n', plugins: [cacheOkOnly] }),
)
// Runtime caching for external resources
@@ -46,16 +39,5 @@ for (const origin of [
registerRoute(({ url }) => url.origin === origin, new CacheFirst())
}
-// Prevent uncaught errors when precache or network fails (e.g. offline before precaching completes)
+// Prevent uncaught errors when network fails offline
setCatchHandler(async () => Response.error())
-
-// Defer precaching until app signals auth is ready
-let precached = false
-self.addEventListener('message', (event) => {
- if (event.data?.type === 'PRECACHE_APP' && !precached) {
- precached = true
- event.waitUntil(
- pc.install(event).then(() => pc.activate(event)),
- )
- }
-})