Skip to content

Commit 7e19893

Browse files
Karen BaneyKaren Baney
authored andcommitted
update caching. move skipwaiting to sw.js
1 parent 7fedbbd commit 7e19893

File tree

3 files changed

+20
-33
lines changed

3 files changed

+20
-33
lines changed

generator/template/src/components/app-manual-update.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export default {
1717
<template>
1818
<div
1919
style="background-color: #FFFF99;"
20-
v-if="updateExists"
20+
v-show="updateExists"
2121
>
2222
Update available
2323
<button @click="refreshApp">

generator/template/src/service-worker/register-service-worker.js

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
import { Workbox } from 'workbox-window'
77

88
const autoUpdate = async (registration) => {
9-
// const updateInterval = 1000 * 60 * 60 // 1 hour
10-
const updateInterval = 100 * 60 * 60 // 1 min // for debugging
9+
const updateInterval = 1000 * 60 * 60 // 1 hour
10+
// const updateInterval = 1000 * 60 // 1 min // for debugging
1111
setInterval(async () => {
1212
try {
1313
/* eslint-disable-next-line no-unused-expressions */
@@ -21,6 +21,7 @@
2121

2222
const manualUpdateAvailable = (registration) => {
2323
// Wires up an event that we can listen to in the app. Example: listen for available update and prompt user to update.
24+
console.log('sw: manualUpdateAvailable dispatching event')
2425
document.dispatchEvent(
2526
new CustomEvent('swUpdated', { detail: registration }))
2627
}
@@ -39,36 +40,11 @@
3940
autoUpdate(registration)
4041

4142
wb.addEventListener('activated', async (event) => {
42-
// if (event.isUpdate) {
43+
console.log('sw: activated event listener hit.')
44+
if (event.isUpdate) {
4345
// event.isUpdate=true means the service worker was already registered and there is a new version available.
44-
4546
// this only triggers self.skipWaiting. It still doesn't force the app to update. See /composables/use-service-worker.ts for updating app.
46-
wb.messageSkipWaiting()
47-
// } else {
48-
// // first time use when event.isUpdate = false
49-
// // service worker should claim the client immediately since its the first install.
50-
// wb.messageSW({ type: 'CLIENTS_CLAIM' })
51-
// console.log('sw: clientsClaim called.')
52-
// }
53-
})
54-
55-
// This code listens for the user's confirmation to update the app.
56-
wb.addEventListener('message', (event) => {
57-
console.log('sw: message event listener hit.')
58-
if (event.data && event.data.type === 'SKIP_WAITING') {
5947
wb.messageSkipWaiting()
60-
console.log('sw: message SKIP_WAITING called.')
61-
}
62-
})
63-
64-
wb.addEventListener('installed', (event) => {
65-
console.log('sw: installed event listener hit.')
66-
if (event.isUpdate) {
67-
// Wires up event that is listened to in the use-service-worker composable. The app-manual-update component then
68-
// prompts the user there is an update available to activate.
69-
// manualUpdateAvailable(registration)
70-
wb.messageSkipWaiting()
71-
console.log('sw: installed new version.')
7248
} else {
7349
// first time use when event.isUpdate = false
7450
// service worker should claim the client immediately since its the first install.
@@ -77,6 +53,10 @@
7753
}
7854
})
7955

56+
wb.addEventListener('installed', (event) => {
57+
console.log('sw: installed event listener hit.')
58+
})
59+
8060
wb.addEventListener('waiting', (event) => {
8161
console.log('sw: waiting event listener hit.')
8262
if (event.isUpdate) {

generator/template/src/sw.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@ registerRoute(
1717
})
1818
)
1919

20-
// Cache CSS, JS, and Web Worker requests with a Stale While Revalidate strategy
20+
// Cache CSS and JS requests with a Stale While Revalidate strategy
2121
registerRoute(
2222
// Check to see if the request's destination is style for stylesheets, script for JavaScript, or worker for web worker
2323
({ request }) =>
2424
request.destination === 'style' ||
25-
request.destination === 'script' ||
26-
request.destination === 'worker',
25+
request.destination === 'script',
2726
// Use a Stale While Revalidate caching strategy
2827
new StaleWhileRevalidate({
2928
// Put all cached files in a cache named 'assets'
@@ -58,3 +57,11 @@ registerRoute(
5857
]
5958
})
6059
)
60+
61+
self.addEventListener('message', (event) => {
62+
console.log('sw root: message event listener hit.')
63+
if (event.data && event.data.type === 'SKIP_WAITING') {
64+
self.skipWaiting()
65+
console.log('sw root: message SKIP_WAITING called.')
66+
}
67+
})

0 commit comments

Comments
 (0)