Skip to content

Commit b38b71e

Browse files
committed
Fix PWA cache updating so updates will automatically be used
1 parent fc05b0c commit b38b71e

File tree

3 files changed

+77
-7
lines changed

3 files changed

+77
-7
lines changed

.github/workflows/deploy.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: Deploy to GitHub Pages
2+
on:
3+
push:
4+
branches: [main]
5+
6+
jobs:
7+
deploy:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v3
11+
12+
- name: Update Service Worker Cache Version
13+
run: |
14+
sed -i "s/const CACHE_VERSION = '[^']*'/const CACHE_VERSION = 'v$(date +%s)'/" sw.js
15+
16+
- name: Deploy to GitHub Pages
17+
uses: peaceiris/actions-gh-pages@v3
18+
with:
19+
github_token: ${{ secrets.GITHUB_TOKEN }}
20+
publish_dir: ./

index.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,11 @@ <h3>Random Event</h3>
644644
navigator.serviceWorker.register('sw.js')
645645
.then(function(registration) {
646646
console.log('Service Worker registered with scope:', registration.scope);
647+
648+
// Auto-reload when new service worker activates
649+
navigator.serviceWorker.addEventListener('controllerchange', () => {
650+
window.location.reload();
651+
});
647652
})
648653
.catch(function(error) {
649654
console.log('Service Worker registration failed:', error);

sw.js

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
const CACHE_NAME = 'kevins-oracle-v1.0.0';
1+
// Service Worker with proper update handling
2+
const CACHE_VERSION = 'v1.0.1'; // This will be auto-updated by GitHub Actions
3+
const CACHE_NAME = `kevins-oracle-${CACHE_VERSION}`;
4+
25
const urlsToCache = [
36
'./',
47
'./index.html',
58
'./manifest.json',
69
'./sw.js',
10+
'./generators.js',
711
'./icons/icon-72x72.svg',
812
'./icons/icon-96x96.svg',
913
'./icons/icon-128x128.svg',
@@ -14,43 +18,84 @@ const urlsToCache = [
1418
'./icons/icon-512x512.svg'
1519
];
1620

17-
// Install event - cache essential files
21+
// Install event - cache essential files and skip waiting
1822
self.addEventListener('install', event => {
1923
console.log('Service Worker installing...');
24+
25+
// Skip waiting to activate immediately
26+
self.skipWaiting();
27+
2028
event.waitUntil(
2129
caches.open(CACHE_NAME)
2230
.then(cache => {
23-
console.log('Opened cache');
31+
console.log('Opened cache:', CACHE_NAME);
2432
return cache.addAll(urlsToCache);
2533
})
26-
.then(() => self.skipWaiting())
2734
);
2835
});
2936

30-
// Activate event - clean up old caches
37+
// Activate event - clean up old caches and claim clients
3138
self.addEventListener('activate', event => {
3239
console.log('Service Worker activating...');
40+
3341
event.waitUntil(
3442
caches.keys().then(cacheNames => {
3543
return Promise.all(
3644
cacheNames.map(cacheName => {
45+
// Delete all caches that don't match current version
3746
if (cacheName !== CACHE_NAME) {
3847
console.log('Deleting old cache:', cacheName);
3948
return caches.delete(cacheName);
4049
}
4150
})
4251
);
43-
}).then(() => self.clients.claim())
52+
}).then(() => {
53+
// Claim all clients to ensure they use the updated service worker
54+
return self.clients.claim();
55+
})
4456
);
4557
});
4658

4759
// Fetch event - serve from cache, fall back to network
4860
self.addEventListener('fetch', event => {
61+
// Skip non-GET requests and browser extensions
62+
if (event.request.method !== 'GET') return;
63+
4964
event.respondWith(
5065
caches.match(event.request)
5166
.then(response => {
5267
// Return cached version or fetch from network
53-
return response || fetch(event.request);
68+
if (response) {
69+
return response;
70+
}
71+
72+
// Clone the request because it can only be used once
73+
const fetchRequest = event.request.clone();
74+
75+
return fetch(fetchRequest).then(response => {
76+
// Check if we received a valid response
77+
if (!response || response.status !== 200 || response.type !== 'basic') {
78+
return response;
79+
}
80+
81+
// Clone the response because it can only be used once
82+
const responseToCache = response.clone();
83+
84+
// Cache the new response for future requests
85+
caches.open(CACHE_NAME)
86+
.then(cache => {
87+
cache.put(event.request, responseToCache);
88+
});
89+
90+
return response;
91+
});
5492
})
5593
);
94+
});
95+
96+
// Listen for messages from the client
97+
self.addEventListener('message', event => {
98+
if (event.data && event.data.type === 'SKIP_WAITING') {
99+
self.skipWaiting();
100+
}
56101
});

0 commit comments

Comments
 (0)