Skip to content

Open source personal finance application

fix: add offline support for navigation and i18n in service worker

Ilya Komichevdc0d480073a5
diff --git a/app/sw.ts b/app/sw.ts
index aeae429e..6a85d9c9 100644
--- a/app/sw.ts
+++ b/app/sw.ts
@@ -1,7 +1,7 @@
import { clientsClaim } from 'workbox-core'
import { cleanupOutdatedCaches, PrecacheController, PrecacheRoute } from 'workbox-precaching'
-import { registerRoute } from 'workbox-routing'
-import { CacheFirst } from 'workbox-strategies'
+import { NavigationRoute, registerRoute, setCatchHandler } from 'workbox-routing'
+import { CacheFirst, NetworkFirst } from 'workbox-strategies'
declare let self: ServiceWorkerGlobalScope
@@ -19,6 +19,17 @@ registerRoute(new PrecacheRoute(pc))
// Cleanup caches from previous SW versions
cleanupOutdatedCaches()
+// Navigation: network-first with offline fallback to cached HTML
+registerRoute(new NavigationRoute(new NetworkFirst({
+ cacheName: 'pages',
+})))
+
+// i18n locale messages: network-first for offline support
+registerRoute(
+ ({ url }) => url.pathname.startsWith('/_i18n/'),
+ new NetworkFirst({ cacheName: 'i18n' }),
+)
+
// Runtime caching for external resources
for (const origin of [
'https://api.iconify.design',
@@ -28,6 +39,9 @@ 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)
+setCatchHandler(async () => Response.error())
+
// Defer precaching until app signals auth is ready
let precached = false
self.addEventListener('message', (event) => {