-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathsw.js
More file actions
120 lines (107 loc) · 3.15 KB
/
Copy pathsw.js
File metadata and controls
120 lines (107 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Service Worker for SpellBloc - Offline Support
const CACHE_NAME = 'spellbloc-v1.0.0';
const urlsToCache = [
'/',
'/index.html',
'/styles.css',
'/game.js',
'/advanced-systems.js',
'/manifest.json'
];
// Install event - cache resources
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME)
.then((cache) => {
return cache.addAll(urlsToCache);
})
);
});
// Fetch event - serve from cache when offline
self.addEventListener('fetch', (event) => {
event.respondWith(
caches.match(event.request)
.then((response) => {
// Return cached version or fetch from network
return response || fetch(event.request);
})
);
});
// Activate event - clean up old caches
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((cacheNames) => {
return Promise.all(
cacheNames.map((cacheName) => {
if (cacheName !== CACHE_NAME) {
return caches.delete(cacheName);
}
})
);
})
);
});
// Background sync for analytics data
self.addEventListener('sync', (event) => {
if (event.tag === 'analytics-sync') {
event.waitUntil(syncAnalyticsData());
}
});
async function syncAnalyticsData() {
try {
// Get pending analytics data from IndexedDB
const data = await getPendingAnalytics();
// Send to server (placeholder)
await fetch('/api/analytics', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(data)
});
// Clear pending data after successful sync
await clearPendingAnalytics();
} catch (error) {
console.log('Analytics sync failed, will retry later');
}
}
// Placeholder functions for IndexedDB operations
async function getPendingAnalytics() {
return JSON.parse(localStorage.getItem('spellbloc_pending_analytics') || '[]');
}
async function clearPendingAnalytics() {
localStorage.removeItem('spellbloc_pending_analytics');
}
// Push notifications for engagement (if user opts in)
self.addEventListener('push', (event) => {
const options = {
body: 'Time for some spelling practice! 📚✨',
icon: '/icon-192x192.png',
badge: '/badge-72x72.png',
tag: 'spelling-reminder',
actions: [
{
action: 'play',
title: 'Start Playing',
icon: '/play-icon.png'
},
{
action: 'dismiss',
title: 'Maybe Later',
icon: '/dismiss-icon.png'
}
]
};
event.waitUntil(
self.registration.showNotification('SpellBloc Reminder', options)
);
});
// Handle notification clicks
self.addEventListener('notificationclick', (event) => {
event.notification.close();
if (event.action === 'play') {
event.waitUntil(
clients.openWindow('/')
);
}
});