Merge pull request #3329 from thelounge/xpaw/sw-error

Send service worker fetch errors to client
This commit is contained in:
Pavel Djundik 2019-08-03 22:27:53 +03:00 committed by GitHub
commit 85025a6840
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 1 deletions

View File

@ -9,7 +9,7 @@
*/ */
(function() { (function() {
const msg = document.getElementById("loading-page-message"); var msg = document.getElementById("loading-page-message");
if (msg) { if (msg) {
msg.textContent = "Loading the app…"; msg.textContent = "Loading the app…";
@ -69,5 +69,19 @@
// Trigger early service worker registration // Trigger early service worker registration
if ("serviceWorker" in navigator) { if ("serviceWorker" in navigator) {
navigator.serviceWorker.register("service-worker.js"); navigator.serviceWorker.register("service-worker.js");
// Handler for messages coming from the service worker
var messageHandler = function ServiceWorkerMessageHandler(event) {
if (event.data.type === "fetch-error") {
window.g_LoungeErrorHandler({
message: `Service worker failed to fetch an url: ${event.data.message}`,
});
// Display only one fetch error
navigator.serviceWorker.removeEventListener("message", messageHandler);
}
};
navigator.serviceWorker.addEventListener("message", messageHandler);
} }
})(); })();

View File

@ -87,6 +87,17 @@ async function networkOrCache(event) {
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.error(e.message, event.request.url); console.error(e.message, event.request.url);
if (event.clientId) {
const client = await clients.get(event.clientId);
if (client) {
client.postMessage({
type: "fetch-error",
message: e.message,
});
}
}
const cache = await caches.open(cacheName); const cache = await caches.open(cacheName);
const matching = await cache.match(event.request); const matching = await cache.match(event.request);