PWA - Mailto exclude from cache?

Viewed 50
//error
sw-host.js:50 Uncaught (in promise) TypeError: Failed to execute 'addAll' on 'Cache': Request scheme 'mailto' is unsupported
    at sw-host.js:50

my code for my service worker (host)

let installEvent = null;
let installButton = document.getElementById("install");
let enableButton = document.getElementById("enable");

enableButton.addEventListener("click", function() {
    this.disabled = true;
    startPwa(true);
});

if(localStorage["pwa-enabled"]) {
    startPwa();
}

function startPwa(firstStart) {
    localStorage["pwa-enabled"] = true;

    if(firstStart) {
        location.reload();
    }

    window.addEventListener("load", () => {
        navigator.serviceWorker.register("/src/pwa/service-worker.js")
        .then(registration => {
            console.log("Service Worker is registered", registration);
            enableButton.parentNode.remove();
        })
        .catch(err => {
            console.error("Registration failed:", err);
        });
    });

    window.addEventListener("beforeinstallprompt", (e) => {
        e.preventDefault();
        console.log("Ready to install...");
        installEvent = e;
        document.getElementById("install").style.display = "initial";
    });

    setTimeout(cacheLinks, 500);

    function cacheLinks() {
        caches.open("pwa").then(function(cache) {
            let linksFound = [];
            if (window.location.protocol === 'mailto') {
                return;
              }
            document.querySelectorAll("a").forEach(function(a) {
                linksFound.push(a.href);
            });
            cache.addAll(linksFound);
        });
    }

    if(installButton) {
        installButton.addEventListener("click", function() {
            installEvent.prompt();
        });
    }
}

my service worker (worker)

self.addEventListener("install", function(event) {
    event.waitUntil(
        caches.open("pwa").then(function(cache) {
            return cache.addAll([
                "/src/autoload.php",
                "/src/pwa/sw-host.js",
        "/src/pwa/manifest.json",
            ]);
        })
    );
});

self.addEventListener("fetch", function(event) {
    event.respondWith(
        caches.open("pwa").then(function(cache) {
            return cache.match(event.request).then(function(response) {
                cache.addAll([event.request.url]);

                if(response) {
                    return response;
                }

                return fetch(event.request);
            });
        })
    );
});

The issue is, When i have a Mailto: link, (as it cannot be cached) My PWA errors out fully. how could i exclude mailto links?

when there is a mailto link on any page, it just returns the error (first block).

also, promnt install is another issue but i dont need help with that.

0 Answers
Related