我在一个service-worker.js文件上,试图缓存来自渐进式Web应用程序的API端点的JSON数据。
这将在缓存中创建一个天气文件,但其内容是我的HTML页面。如果我console.log(数据),我会看到我需要的对象。
我尝试过cache.add(JSON.stringify(数据))和cache.addAll(数据),但都没有用。
self.addEventListener('install', event => {
event.waitUntil(
caches.open('weather')
.then(function(cache) {
fetch('/api/weathercurrent')
.then(function(response) {
return response.json();
})
.then(function(data) {
cache.add(data);
})
})
)
});发布于 2018-09-23 11:03:08
解决方案比我想象的要简单得多。我认为因为数据是JSON,所以我需要使用请求和响应处理程序。
event.waitUntil(
caches.open('weathercurrent')
.then(cache => cache.add('/api/weathercurrent'))
);https://stackoverflow.com/questions/52462409
复制相似问题