PWA Console Error "Uncaught (in promise) TypeError: Failed to fetch"

时间:2018-06-04 17:33:11

标签: javascript pwa

I'm working on my first PWA, The console is throwing lots of erros

"Uncaught (in promise) TypeError: Failed to fetch"

Any help, would really appreciated it. Thanks

const cacheName = 'Service-workers-cache';

addEventListener('fetch', fetchEvent => {
    const request = fetchEvent.request;
    if (request.method !== 'GET') {
        return;
    }
    fetchEvent.respondWith(async function () {
        const responseFromFetch = fetch(request);
        fetchEvent.waitUntil(async function () {
            const responseCopy = (await responseFromFetch).clone();
            const myCache = await caches.open(cacheName);
            await myCache.put(request, responseCopy);
        }());
        if (request.headers.get('Accept').includes('text/html')) {
            try {
                return await responseFromFetch;
            }
            catch (error) {
                return caches.match(request);
            }
        } else {
            const responseFromCache = await caches.match(request);
            return responseFromCache || responseFromFetch;

        }
    }());
});

0 个答案:

没有答案