-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
150 lines (135 loc) · 4.35 KB
/
Copy pathsw.js
File metadata and controls
150 lines (135 loc) · 4.35 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
* ReadIn52 Service Worker
* Provides offline support and caching
*/
const CACHE_NAME = 'readin52-v6';
const STATIC_CACHE = 'readin52-static-v6';
const API_CACHE = 'readin52-api-v6';
// Static assets to cache
const STATIC_ASSETS = [
'/',
'/manifest.json',
'/assets/css/style.css',
'/assets/js/app.js',
'/assets/js/bible-api.js',
'/assets/images/icon.svg',
'/assets/images/logo.svg'
];
// Install event - cache static assets
self.addEventListener('install', event => {
event.waitUntil(
caches.open(STATIC_CACHE)
.then(cache => {
console.log('Caching static assets');
return cache.addAll(STATIC_ASSETS);
})
.then(() => self.skipWaiting())
);
});
// Activate event - clean up old caches
self.addEventListener('activate', event => {
event.waitUntil(
caches.keys()
.then(cacheNames => {
return Promise.all(
cacheNames
.filter(name => name !== STATIC_CACHE && name !== API_CACHE)
.map(name => caches.delete(name))
);
})
.then(() => self.clients.claim())
);
});
// Fetch event - serve from cache, fallback to network
self.addEventListener('fetch', event => {
const { request } = event;
const url = new URL(request.url);
// Skip non-GET requests
if (request.method !== 'GET') {
return;
}
// Handle Bible API requests
if (url.hostname === 'bible.helloao.org') {
event.respondWith(
caches.open(API_CACHE)
.then(cache => {
return cache.match(request)
.then(cached => {
if (cached) {
return cached;
}
return fetch(request)
.then(response => {
if (response.ok) {
cache.put(request, response.clone());
}
return response;
});
});
})
);
return;
}
// Handle static assets
if (url.pathname.startsWith('/assets/')) {
event.respondWith(
caches.match(request)
.then(cached => {
if (cached) {
return cached;
}
return fetch(request)
.then(response => {
if (response.ok) {
const clone = response.clone();
caches.open(STATIC_CACHE)
.then(cache => cache.put(request, clone));
}
return response;
});
})
);
return;
}
// Network first for HTML pages
event.respondWith(
fetch(request)
.then(response => {
return response;
})
.catch(() => {
return caches.match(request)
.then(cached => {
if (cached) {
return cached;
}
// Return offline page if available
if (request.headers.get('accept').includes('text/html')) {
return caches.match('/');
}
});
})
);
});
// Handle background sync for progress updates
self.addEventListener('sync', event => {
if (event.tag === 'sync-progress') {
event.waitUntil(syncProgress());
}
});
async function syncProgress() {
// This would sync any offline progress updates
// Implementation would depend on IndexedDB storage of offline actions
console.log('Syncing progress...');
}
// Handle push notifications (future feature)
self.addEventListener('push', event => {
if (event.data) {
const data = event.data.json();
self.registration.showNotification(data.title, {
body: data.body,
icon: '/assets/images/icon.svg',
badge: '/assets/images/icon.svg'
});
}
});