Skip to content

Commit fb4b6d8

Browse files
committed
feat: enable auto-update for service worker
1 parent 65b4dbf commit fb4b6d8

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

dashboard/src/sw-register.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
1-
// Service Worker Registration - Simplified
21
export function registerSW() {
32
if ('serviceWorker' in navigator) {
4-
// Get the base URL from Vite's environment
53
const baseUrl = import.meta.env.BASE_URL || '/'
64

7-
// Register service worker
85
navigator.serviceWorker
96
.register(`${baseUrl}sw.js`)
107
.then(registration => {
11-
// Registration successful
8+
setInterval(() => {
9+
registration.update()
10+
}, 60 * 60 * 1000)
11+
12+
let refreshing = false
13+
navigator.serviceWorker.addEventListener('controllerchange', () => {
14+
if (!refreshing) {
15+
refreshing = true
16+
window.location.reload()
17+
}
18+
})
1219
})
1320
.catch(registrationError => {
14-
// Registration failed
21+
console.error('Service Worker registration failed:', registrationError)
1522
})
1623
}
1724
}

dashboard/vite.config.mts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,13 @@ export default defineConfig({
7474
svgr(),
7575
tsconfigPaths(),
7676
VitePWA({
77-
registerType: 'prompt',
78-
injectRegister: false, // Disable automatic registration
77+
registerType: 'autoUpdate',
78+
injectRegister: false,
7979
workbox: {
8080
navigateFallback: '/index.html',
8181
globPatterns: ['**/*.{js,css,html,ico,png,svg,woff2}'],
82+
skipWaiting: true,
83+
clientsClaim: true,
8284
runtimeCaching: [
8385
{
8486
urlPattern: /^https:\/\/fonts\.googleapis\.com\/.*/i,
@@ -87,13 +89,12 @@ export default defineConfig({
8789
cacheName: 'google-fonts-cache',
8890
expiration: {
8991
maxEntries: 10,
90-
maxAgeSeconds: 60 * 60 * 24 * 365, // 1 year
92+
maxAgeSeconds: 60 * 60 * 24 * 365,
9193
},
9294
},
9395
},
9496
],
9597
},
96-
// Remove the manifest configuration entirely - we'll handle it dynamically
9798
}),
9899
],
99100
})

0 commit comments

Comments
 (0)