I have a web app, and recently converted it into a PWA, so I'm new to service workers and pwa in general. Hoping someone has gone through what I'm facing, but I have not found anything on StackOverflow or Google after searching for the last few days.
My web app uses AJAX to post content and on success do a location.reload(true) to show the latest content (or provides access to new page content) at the same scroll point.
However once I created the PWA, the location.reload(true) seems to be loading cached content. Is there any way to get this to work with fresh content from the server?
I checked that this is only behaving this way for iOS PWA/iOS Safari and Android PWA/ Android Chrome. The location.reload is still working for Firefox/Chrome mobile web for iOS and Firefox for Android (iOS Safari and Android Chrome was behaving correctly before the PWA conversion).
Here's my service worker:
var cacheName = 'dev-pwa';
var filesToCache = [
'/',
'index.php',
'style.css',
'js/main.js'
];
/* Start the service worker and cache all of the app's content */
self.addEventListener('install', function(e) {
e.waitUntil(
caches.open(cacheName).then(function(cache) {
return cache.addAll(filesToCache);
})
);
});
/* Serve cached content when offline */
self.addEventListener('fetch', function(e) {
e.respondWith(
caches.match(e.request).then(function(response) {
return response || fetch(e.request);
})
);
e.waitUntil(update(e.request));
});
function update(request) {
return caches.open(cacheName).then(function (cache) {
return fetch(request).then(function (response) {
return cache.put(request, response);
});
});
}
location.reload(true)? What happens if you remove the index.php entry from the list of cached pages? - chingucodinglocation.reload(true)) while mobile browsers still will go through your service worker. A workaround would be to clear the cache of your service worker or disable if when callinglocation.reload(true). - chingucoding