mirror of
https://github.com/Tony0410/nextstep.git
synced 2026-05-24 21:31:43 +08:00
Fix service worker install failure due to missing icon files
The service worker was failing to install because it tried to cache icon files that don't exist (icon-192.png, icon-512.png). Simplified the service worker to focus only on push notifications: - Removed caching during install (was causing "redundant" state) - Removed fetch handler caching - Removed references to non-existent icon files Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
66
public/sw.js
66
public/sw.js
@@ -1,66 +1,16 @@
|
|||||||
// NextStep Service Worker for Push Notifications
|
// NextStep Service Worker for Push Notifications
|
||||||
|
|
||||||
const CACHE_NAME = 'nextstep-v1'
|
// Install event - activate immediately
|
||||||
|
|
||||||
// Install event - cache critical assets
|
|
||||||
self.addEventListener('install', (event) => {
|
self.addEventListener('install', (event) => {
|
||||||
console.log('Service Worker: Installing...')
|
console.log('Service Worker: Installing...')
|
||||||
event.waitUntil(
|
// Skip waiting to activate immediately
|
||||||
caches.open(CACHE_NAME).then((cache) => {
|
|
||||||
return cache.addAll([
|
|
||||||
'/',
|
|
||||||
'/today',
|
|
||||||
'/meds',
|
|
||||||
'/icon-192.png',
|
|
||||||
'/icon-512.png',
|
|
||||||
])
|
|
||||||
})
|
|
||||||
)
|
|
||||||
self.skipWaiting()
|
self.skipWaiting()
|
||||||
})
|
})
|
||||||
|
|
||||||
// Activate event - clean up old caches
|
// Activate event - claim clients immediately
|
||||||
self.addEventListener('activate', (event) => {
|
self.addEventListener('activate', (event) => {
|
||||||
console.log('Service Worker: Activating...')
|
console.log('Service Worker: Activating...')
|
||||||
event.waitUntil(
|
event.waitUntil(self.clients.claim())
|
||||||
caches.keys().then((cacheNames) => {
|
|
||||||
return Promise.all(
|
|
||||||
cacheNames
|
|
||||||
.filter((name) => name !== CACHE_NAME)
|
|
||||||
.map((name) => caches.delete(name))
|
|
||||||
)
|
|
||||||
})
|
|
||||||
)
|
|
||||||
self.clients.claim()
|
|
||||||
})
|
|
||||||
|
|
||||||
// Fetch event - serve from cache when offline
|
|
||||||
self.addEventListener('fetch', (event) => {
|
|
||||||
// Only cache GET requests
|
|
||||||
if (event.request.method !== 'GET') return
|
|
||||||
|
|
||||||
event.respondWith(
|
|
||||||
caches.match(event.request).then((cached) => {
|
|
||||||
// Return cached version or fetch from network
|
|
||||||
return (
|
|
||||||
cached ||
|
|
||||||
fetch(event.request).then((response) => {
|
|
||||||
// Don't cache API responses
|
|
||||||
if (event.request.url.includes('/api/')) {
|
|
||||||
return response
|
|
||||||
}
|
|
||||||
// Cache successful responses
|
|
||||||
if (response.status === 200) {
|
|
||||||
const clone = response.clone()
|
|
||||||
caches.open(CACHE_NAME).then((cache) => {
|
|
||||||
cache.put(event.request, clone)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
return response
|
|
||||||
})
|
|
||||||
)
|
|
||||||
})
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
// Push event - handle incoming push notifications
|
// Push event - handle incoming push notifications
|
||||||
@@ -70,8 +20,6 @@ self.addEventListener('push', (event) => {
|
|||||||
let data = {
|
let data = {
|
||||||
title: 'Medication Reminder',
|
title: 'Medication Reminder',
|
||||||
body: 'Time to take your medication',
|
body: 'Time to take your medication',
|
||||||
icon: '/icon-192.png',
|
|
||||||
badge: '/badge-72.png',
|
|
||||||
tag: 'medication-reminder',
|
tag: 'medication-reminder',
|
||||||
data: {
|
data: {
|
||||||
url: '/meds',
|
url: '/meds',
|
||||||
@@ -88,15 +36,9 @@ self.addEventListener('push', (event) => {
|
|||||||
|
|
||||||
const options = {
|
const options = {
|
||||||
body: data.body,
|
body: data.body,
|
||||||
icon: data.icon || '/icon-192.png',
|
|
||||||
badge: data.badge || '/badge-72.png',
|
|
||||||
tag: data.tag || 'default',
|
tag: data.tag || 'default',
|
||||||
vibrate: [100, 50, 100],
|
vibrate: [100, 50, 100],
|
||||||
data: data.data || {},
|
data: data.data || {},
|
||||||
actions: data.actions || [
|
|
||||||
{ action: 'take', title: 'Taken' },
|
|
||||||
{ action: 'snooze', title: 'Snooze' },
|
|
||||||
],
|
|
||||||
requireInteraction: true,
|
requireInteraction: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user