如何缓存POST请求?我能够缓存所有GET请求,但无法缓存POST。我正在为我的应用程序实现脱机功能。
我尝试了以下代码,但是没有运气
self.addEventListener('fetch', function(event) {
event.respondWith(caches.match(event.request).then(function(response) {console.log('fetch' +response);
// caches.match() always resolves
// but in case of success response will have value
if (response !== undefined) {
return response;
} else {
return fetch(event.request).then(function (response) {
// response may be used only once
// we need to save clone to put one copy in cache
// and serve second one
let responseClone = response.clone();
caches.open('v1').then(function (cache) {
cache.put(event.request, responseClone);
});
return response;
}).catch(function () {
return caches.match('/sw-test/gallery/myLittleVader.jpg');
});
}
}));
});