使用服务人员对POST请求的缓存响应

时间:2019-03-19 06:48:38

标签: javascript service-worker progressive-web-apps indexeddb offline-caching

我正在使用/api/v1的调用来从后端获取数据。 API方法是POST,我按如下所示传递参数:

{
  "act":"get",
  "nm":"dataset1",
  "lk":0
}

响应包含一个JSON字符串。

我要使我的应用程序可以脱机使用,因此我需要使用服务工作者来缓存此API请求。这是我的服务人员:

'use strict';

const VERSION = 'v1';
const PRECACHE = `precache-${VERSION}`;
const RUNTIME = `runtime-${VERSION}`;
const enableRuntimeCache = true;
const mode = 'cache-update';

const PRECACHE_URLS = [
    '/'
];

let NetworkOnline = true;


self.addEventListener('install', event => {
    event.waitUntil(
        caches.open(PRECACHE).then(cache => {
            cache.addAll(PRECACHE_URLS);
        }).then(self.skipWaiting())
    );
});

self.addEventListener('activate', event => {

    const currentCaches = [PRECACHE, RUNTIME];
    event.waitUntil(
        caches.keys().then(cacheNames => {
            return cacheNames.filter(cacheName => !currentCaches.includes(cacheName));
        }).then(cachesToDelete => {
            return Promise.all(cachesToDelete.map(cacheToDelete => {
                return caches.delete(cacheToDelete);
            }));
        }).then(() => {
            self.clients.claim();
        })
    );

});


self.addEventListener('fetch', event => {

    if (event.request.url.startsWith(self.location.origin)) {
        event.respondWith(fromCache(event.request));

        if (isOnline()) {
            if (mode === 'cache-update') {
                if (event.request.method !== 'POST') {
                    event.waitUntil(
                            update(event.request)
                                 /*.then(refresh)*/
                            .catch(errorHandler)
                    );
                }
            }
        }
    }

});

/*function setFromCache(request) {

    console.log(self);
    updateFromCache(true);
}*/

function fromCache(request) {
    return caches.match(request).then(cachedResponse => {
        if (cachedResponse) {
            return cachedResponse;
        }

        if (isOnline()) {
            if (enableRuntimeCache) {
                 if (request.method !== 'POST') {
                    return caches.open(RUNTIME).then(cache => {
                        return fetch(request).then(response => {
                            return cache.put(request, response.clone()).then(() => {
                                return response;
                            });
                        }).catch(errorHandler);
                    });
                } else {
                    return fetch(request).then(response => {
                        console.log(response);
                        return response;
                    });
                }
            } else {
                return fetch(request).then(response => {
                    return response;
                });
            }
        }

    });
}

function update(request) {

        let asset = request.url.substr(request.url.lastIndexOf('/') + 1);
        let openCache = (PRECACHE_URLS.some(val => val.indexOf(asset) >= 0)) ? PRECACHE : RUNTIME;

        return caches.open(openCache).then(cache => {
            return fetch(`${request.url }?${new Date().valueOf()}`).then(response => {
                return cache.put(request, response.clone()).then(() => {
                    return response;
                });
            }).catch(errorHandler);
        });

}


function refresh(response) {
    return self.clients.matchAll().then(clients => {
        clients.forEach(client => {
            var message = {
                type: 'refresh',
                url: response.url,
                eTag: response.headers.get('ETag')
            };

            client.postMessage(JSON.stringify(message));
        });
    });
}

function isOnline() {
    return self.navigator.onLine;
}

function errorHandler(error) {
    if (error instanceof TypeError) {
        if (error.message.includes('Failed to fetch')) {
            console.error('(FtF) Error caught:', error);
        } else {
            console.error('Error caught:', error);
        }
    }
}

我需要将POST请求的Response缓存到indexDB,并且其行为应与服务工作者相同。

能请你帮我吗?

0 个答案:

没有答案