Force Service Worker Update on First Visit from User, not Second Visit from User
up vote
2
down vote
favorite
I've recently started to delve into service workers and the related strategies. There seems to be some confusion in my mind, or if I'm understanding them correctly, something of a flaw in the entire API — the issue being that when you make a change to your serviceworker.js
file (i.e. change a version number in this file because you've made changes to your website), the user has to then visit the site twice to see the changes? Once to register and install the new service worker, and a second time to activate it (i.e access the new cache).
Now in the real world if there is an issue on your site, whether it be broken link, a javascript error, or the wrong info in a blog post etc, when a user comes back to that site for the first after you've made the fix, there's no way of them seeing this fix unless they come back a second time? Even though you've made the changes?
I mean, this can't be true...surely?
CODE THAT I'M USING
var preCacheList = [
"/", "index.html", "work.html", "contact.html", "style.css", "js/main.js"
];
var CACHE_STATIC_NAME = 'static-v1.4';
var CACHE_DYNAMIC_NAME = 'dynamic-v1.4';
self.addEventListener('install', function(event)
console.log('[Service Worker] Installing Service Worker ...', event);
event.waitUntil(
caches.open(CACHE_STATIC_NAME)
.then(function(cache)
console.log('[Service Worker] Precaching App Shell');
cache.addAll(preCacheList);
)
)
);
self.addEventListener('activate', function(event)
console.log('[Service Worker] Activating Service Worker ....', event);
event.waitUntil(
caches.keys()
.then(function(keyList)
return Promise.all(keyList.map(function(key)
if (key !== CACHE_STATIC_NAME && key !== CACHE_DYNAMIC_NAME)
console.log('[Service Worker] Removing old cache.', key);
return caches.delete(key);
));
)
);
return self.clients.claim();
);
self.addEventListener('fetch', function(event)
event.respondWith(
caches.match(event.request)
.then(function(response)
if (response)
return response;
else
return fetch(event.request)
.then(function(res)
return caches.open(CACHE_DYNAMIC_NAME)
.then(function(cache)
cache.put(event.request.url, res.clone());
return res;
)
)
.catch(function(err)
);
)
);
);
javascript html caching service-worker
add a comment |
up vote
2
down vote
favorite
I've recently started to delve into service workers and the related strategies. There seems to be some confusion in my mind, or if I'm understanding them correctly, something of a flaw in the entire API — the issue being that when you make a change to your serviceworker.js
file (i.e. change a version number in this file because you've made changes to your website), the user has to then visit the site twice to see the changes? Once to register and install the new service worker, and a second time to activate it (i.e access the new cache).
Now in the real world if there is an issue on your site, whether it be broken link, a javascript error, or the wrong info in a blog post etc, when a user comes back to that site for the first after you've made the fix, there's no way of them seeing this fix unless they come back a second time? Even though you've made the changes?
I mean, this can't be true...surely?
CODE THAT I'M USING
var preCacheList = [
"/", "index.html", "work.html", "contact.html", "style.css", "js/main.js"
];
var CACHE_STATIC_NAME = 'static-v1.4';
var CACHE_DYNAMIC_NAME = 'dynamic-v1.4';
self.addEventListener('install', function(event)
console.log('[Service Worker] Installing Service Worker ...', event);
event.waitUntil(
caches.open(CACHE_STATIC_NAME)
.then(function(cache)
console.log('[Service Worker] Precaching App Shell');
cache.addAll(preCacheList);
)
)
);
self.addEventListener('activate', function(event)
console.log('[Service Worker] Activating Service Worker ....', event);
event.waitUntil(
caches.keys()
.then(function(keyList)
return Promise.all(keyList.map(function(key)
if (key !== CACHE_STATIC_NAME && key !== CACHE_DYNAMIC_NAME)
console.log('[Service Worker] Removing old cache.', key);
return caches.delete(key);
));
)
);
return self.clients.claim();
);
self.addEventListener('fetch', function(event)
event.respondWith(
caches.match(event.request)
.then(function(response)
if (response)
return response;
else
return fetch(event.request)
.then(function(res)
return caches.open(CACHE_DYNAMIC_NAME)
.then(function(cache)
cache.put(event.request.url, res.clone());
return res;
)
)
.catch(function(err)
);
)
);
);
javascript html caching service-worker
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I've recently started to delve into service workers and the related strategies. There seems to be some confusion in my mind, or if I'm understanding them correctly, something of a flaw in the entire API — the issue being that when you make a change to your serviceworker.js
file (i.e. change a version number in this file because you've made changes to your website), the user has to then visit the site twice to see the changes? Once to register and install the new service worker, and a second time to activate it (i.e access the new cache).
Now in the real world if there is an issue on your site, whether it be broken link, a javascript error, or the wrong info in a blog post etc, when a user comes back to that site for the first after you've made the fix, there's no way of them seeing this fix unless they come back a second time? Even though you've made the changes?
I mean, this can't be true...surely?
CODE THAT I'M USING
var preCacheList = [
"/", "index.html", "work.html", "contact.html", "style.css", "js/main.js"
];
var CACHE_STATIC_NAME = 'static-v1.4';
var CACHE_DYNAMIC_NAME = 'dynamic-v1.4';
self.addEventListener('install', function(event)
console.log('[Service Worker] Installing Service Worker ...', event);
event.waitUntil(
caches.open(CACHE_STATIC_NAME)
.then(function(cache)
console.log('[Service Worker] Precaching App Shell');
cache.addAll(preCacheList);
)
)
);
self.addEventListener('activate', function(event)
console.log('[Service Worker] Activating Service Worker ....', event);
event.waitUntil(
caches.keys()
.then(function(keyList)
return Promise.all(keyList.map(function(key)
if (key !== CACHE_STATIC_NAME && key !== CACHE_DYNAMIC_NAME)
console.log('[Service Worker] Removing old cache.', key);
return caches.delete(key);
));
)
);
return self.clients.claim();
);
self.addEventListener('fetch', function(event)
event.respondWith(
caches.match(event.request)
.then(function(response)
if (response)
return response;
else
return fetch(event.request)
.then(function(res)
return caches.open(CACHE_DYNAMIC_NAME)
.then(function(cache)
cache.put(event.request.url, res.clone());
return res;
)
)
.catch(function(err)
);
)
);
);
javascript html caching service-worker
I've recently started to delve into service workers and the related strategies. There seems to be some confusion in my mind, or if I'm understanding them correctly, something of a flaw in the entire API — the issue being that when you make a change to your serviceworker.js
file (i.e. change a version number in this file because you've made changes to your website), the user has to then visit the site twice to see the changes? Once to register and install the new service worker, and a second time to activate it (i.e access the new cache).
Now in the real world if there is an issue on your site, whether it be broken link, a javascript error, or the wrong info in a blog post etc, when a user comes back to that site for the first after you've made the fix, there's no way of them seeing this fix unless they come back a second time? Even though you've made the changes?
I mean, this can't be true...surely?
CODE THAT I'M USING
var preCacheList = [
"/", "index.html", "work.html", "contact.html", "style.css", "js/main.js"
];
var CACHE_STATIC_NAME = 'static-v1.4';
var CACHE_DYNAMIC_NAME = 'dynamic-v1.4';
self.addEventListener('install', function(event)
console.log('[Service Worker] Installing Service Worker ...', event);
event.waitUntil(
caches.open(CACHE_STATIC_NAME)
.then(function(cache)
console.log('[Service Worker] Precaching App Shell');
cache.addAll(preCacheList);
)
)
);
self.addEventListener('activate', function(event)
console.log('[Service Worker] Activating Service Worker ....', event);
event.waitUntil(
caches.keys()
.then(function(keyList)
return Promise.all(keyList.map(function(key)
if (key !== CACHE_STATIC_NAME && key !== CACHE_DYNAMIC_NAME)
console.log('[Service Worker] Removing old cache.', key);
return caches.delete(key);
));
)
);
return self.clients.claim();
);
self.addEventListener('fetch', function(event)
event.respondWith(
caches.match(event.request)
.then(function(response)
if (response)
return response;
else
return fetch(event.request)
.then(function(res)
return caches.open(CACHE_DYNAMIC_NAME)
.then(function(cache)
cache.put(event.request.url, res.clone());
return res;
)
)
.catch(function(err)
);
)
);
);
javascript html caching service-worker
javascript html caching service-worker
asked Nov 9 at 19:18
The Chewy
583521
583521
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
I found the solution to this — you need to add the skipWaiting()
method in the initial installation event.
I've re-posted the code from my question above with a comment on this line:
var preCacheList = [
"/", "index.html", "work.html", "contact.html", "style.css", "js/main.js"
];
var CACHE_STATIC_NAME = 'static-v1.4';
var CACHE_DYNAMIC_NAME = 'dynamic-v1.4';
self.addEventListener('install', function(event)
console.log('[Service Worker] Installing Service Worker ...', event);
event.waitUntil(
caches.open(CACHE_STATIC_NAME)
.then(function(cache)
console.log('[Service Worker] Precaching App Shell');
cache.addAll(preCacheList);
)
)
return self.skipWaiting(); // THIS IS THE LINE THAT OVER-RIDES THE SECOND RELOAD.
);
self.addEventListener('activate', function(event)
console.log('[Service Worker] Activating Service Worker ....', event);
event.waitUntil(
caches.keys()
.then(function(keyList)
return Promise.all(keyList.map(function(key)
if (key !== CACHE_STATIC_NAME && key !== CACHE_DYNAMIC_NAME)
console.log('[Service Worker] Removing old cache.', key);
return caches.delete(key);
));
)
);
return self.clients.claim();
);
self.addEventListener('fetch', function(event)
event.respondWith(
caches.match(event.request)
.then(function(response)
if (response)
return response;
else
return fetch(event.request)
.then(function(res)
return caches.open(CACHE_DYNAMIC_NAME)
.then(function(cache)
cache.put(event.request.url, res.clone());
return res;
)
)
.catch(function(err)
);
)
);
);
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
I found the solution to this — you need to add the skipWaiting()
method in the initial installation event.
I've re-posted the code from my question above with a comment on this line:
var preCacheList = [
"/", "index.html", "work.html", "contact.html", "style.css", "js/main.js"
];
var CACHE_STATIC_NAME = 'static-v1.4';
var CACHE_DYNAMIC_NAME = 'dynamic-v1.4';
self.addEventListener('install', function(event)
console.log('[Service Worker] Installing Service Worker ...', event);
event.waitUntil(
caches.open(CACHE_STATIC_NAME)
.then(function(cache)
console.log('[Service Worker] Precaching App Shell');
cache.addAll(preCacheList);
)
)
return self.skipWaiting(); // THIS IS THE LINE THAT OVER-RIDES THE SECOND RELOAD.
);
self.addEventListener('activate', function(event)
console.log('[Service Worker] Activating Service Worker ....', event);
event.waitUntil(
caches.keys()
.then(function(keyList)
return Promise.all(keyList.map(function(key)
if (key !== CACHE_STATIC_NAME && key !== CACHE_DYNAMIC_NAME)
console.log('[Service Worker] Removing old cache.', key);
return caches.delete(key);
));
)
);
return self.clients.claim();
);
self.addEventListener('fetch', function(event)
event.respondWith(
caches.match(event.request)
.then(function(response)
if (response)
return response;
else
return fetch(event.request)
.then(function(res)
return caches.open(CACHE_DYNAMIC_NAME)
.then(function(cache)
cache.put(event.request.url, res.clone());
return res;
)
)
.catch(function(err)
);
)
);
);
add a comment |
up vote
0
down vote
accepted
I found the solution to this — you need to add the skipWaiting()
method in the initial installation event.
I've re-posted the code from my question above with a comment on this line:
var preCacheList = [
"/", "index.html", "work.html", "contact.html", "style.css", "js/main.js"
];
var CACHE_STATIC_NAME = 'static-v1.4';
var CACHE_DYNAMIC_NAME = 'dynamic-v1.4';
self.addEventListener('install', function(event)
console.log('[Service Worker] Installing Service Worker ...', event);
event.waitUntil(
caches.open(CACHE_STATIC_NAME)
.then(function(cache)
console.log('[Service Worker] Precaching App Shell');
cache.addAll(preCacheList);
)
)
return self.skipWaiting(); // THIS IS THE LINE THAT OVER-RIDES THE SECOND RELOAD.
);
self.addEventListener('activate', function(event)
console.log('[Service Worker] Activating Service Worker ....', event);
event.waitUntil(
caches.keys()
.then(function(keyList)
return Promise.all(keyList.map(function(key)
if (key !== CACHE_STATIC_NAME && key !== CACHE_DYNAMIC_NAME)
console.log('[Service Worker] Removing old cache.', key);
return caches.delete(key);
));
)
);
return self.clients.claim();
);
self.addEventListener('fetch', function(event)
event.respondWith(
caches.match(event.request)
.then(function(response)
if (response)
return response;
else
return fetch(event.request)
.then(function(res)
return caches.open(CACHE_DYNAMIC_NAME)
.then(function(cache)
cache.put(event.request.url, res.clone());
return res;
)
)
.catch(function(err)
);
)
);
);
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
I found the solution to this — you need to add the skipWaiting()
method in the initial installation event.
I've re-posted the code from my question above with a comment on this line:
var preCacheList = [
"/", "index.html", "work.html", "contact.html", "style.css", "js/main.js"
];
var CACHE_STATIC_NAME = 'static-v1.4';
var CACHE_DYNAMIC_NAME = 'dynamic-v1.4';
self.addEventListener('install', function(event)
console.log('[Service Worker] Installing Service Worker ...', event);
event.waitUntil(
caches.open(CACHE_STATIC_NAME)
.then(function(cache)
console.log('[Service Worker] Precaching App Shell');
cache.addAll(preCacheList);
)
)
return self.skipWaiting(); // THIS IS THE LINE THAT OVER-RIDES THE SECOND RELOAD.
);
self.addEventListener('activate', function(event)
console.log('[Service Worker] Activating Service Worker ....', event);
event.waitUntil(
caches.keys()
.then(function(keyList)
return Promise.all(keyList.map(function(key)
if (key !== CACHE_STATIC_NAME && key !== CACHE_DYNAMIC_NAME)
console.log('[Service Worker] Removing old cache.', key);
return caches.delete(key);
));
)
);
return self.clients.claim();
);
self.addEventListener('fetch', function(event)
event.respondWith(
caches.match(event.request)
.then(function(response)
if (response)
return response;
else
return fetch(event.request)
.then(function(res)
return caches.open(CACHE_DYNAMIC_NAME)
.then(function(cache)
cache.put(event.request.url, res.clone());
return res;
)
)
.catch(function(err)
);
)
);
);
I found the solution to this — you need to add the skipWaiting()
method in the initial installation event.
I've re-posted the code from my question above with a comment on this line:
var preCacheList = [
"/", "index.html", "work.html", "contact.html", "style.css", "js/main.js"
];
var CACHE_STATIC_NAME = 'static-v1.4';
var CACHE_DYNAMIC_NAME = 'dynamic-v1.4';
self.addEventListener('install', function(event)
console.log('[Service Worker] Installing Service Worker ...', event);
event.waitUntil(
caches.open(CACHE_STATIC_NAME)
.then(function(cache)
console.log('[Service Worker] Precaching App Shell');
cache.addAll(preCacheList);
)
)
return self.skipWaiting(); // THIS IS THE LINE THAT OVER-RIDES THE SECOND RELOAD.
);
self.addEventListener('activate', function(event)
console.log('[Service Worker] Activating Service Worker ....', event);
event.waitUntil(
caches.keys()
.then(function(keyList)
return Promise.all(keyList.map(function(key)
if (key !== CACHE_STATIC_NAME && key !== CACHE_DYNAMIC_NAME)
console.log('[Service Worker] Removing old cache.', key);
return caches.delete(key);
));
)
);
return self.clients.claim();
);
self.addEventListener('fetch', function(event)
event.respondWith(
caches.match(event.request)
.then(function(response)
if (response)
return response;
else
return fetch(event.request)
.then(function(res)
return caches.open(CACHE_DYNAMIC_NAME)
.then(function(cache)
cache.put(event.request.url, res.clone());
return res;
)
)
.catch(function(err)
);
)
);
);
answered Nov 11 at 16:04
The Chewy
583521
583521
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232039%2fforce-service-worker-update-on-first-visit-from-user-not-second-visit-from-user%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown