I wanted to convert a web form to work offline. Originally, I would store the form information in a sql database on the web server after the user completed each step. One of the steps included uploading images, for which I implemented a progress bar.
After adding a service worker, I noticed that the progress bar no longer worked (the bar would be displayed, but would never get updated to show how much of the file had been uploaded). I tested in multiple browsers to make sure this wasn't browser dependent and had the same results in all of them (chrome, firefox, edge, safari, mobile versions as well).
Here is the first part of my ajax request.
$.ajax({
type: "POST",
url: url,
datatype: "json",
data: JSON.stringify(data),
xhr: function () {
var xhr = new window.XMLHttpRequest();
xhr.upload.addEventListener("progress", function (evt) {
if (evt.lengthComputable) {
var percentComplete = getCurrentProgress(evt.loaded, evt.total);
$("#progressBarContainer .progress > .progress-bar").css({ "width": percentComplete + "%" });
$("#progressBarContainer .progress > .progress-bar .percent-complete").text(percentComplete + "%");
}
}, false);
return xhr;
}
})
Here is the code in my service worker file to handle fetch events.
self.addEventListener("fetch", event => {
event.respondWith(
caches.open(staticCacheName)
.then(cache => {
return cache.match(event.request)
.then(response => {
if (response) {
return response;
}
else {
return fetch(event.request);
}
});
})
);
});
I was able to find a solution, but couldn't find this question on stackoverflow, so I'm posting and answering my own question. If anyone has any comments for improvement or a better solution, please let me know.