是否可以在浏览器内部的脱机存储中存储永久数据? 如果有任何解决方案,那么请帮助我,以便我可以解决我的问题。 我阅读了一些教程,但这对我没有用。 预先感谢!
答案 0 :(得分:2)
我们给出了离线存储数据的一般建议:
在告诉您每个浏览器的存储限制后,我将告诉您如何使用这两种方法。
使用缓存API的服务工作者的工作示例是
var CACHE_VERSION = 1;
// Shorthand identifier mapped to specific versioned cache.
var CURRENT_CACHES = {
font: 'font-cache-v' + CACHE_VERSION
};
self.addEventListener('activate', function(event) {
var expectedCacheNames = Object.values(CURRENT_CACHES);
// Active worker won't be treated as activated until promise
// resolves successfully.
event.waitUntil(
caches.keys().then(function(cacheNames) {
return Promise.all(
cacheNames.map(function(cacheName) {
if (!expectedCacheNames.includes(cacheName)) {
console.log('Deleting out of date cache:', cacheName);
return caches.delete(cacheName);
}
})
);
})
);
});
self.addEventListener('fetch', function(event) {
console.log('Handling fetch event for', event.request.url);
event.respondWith(
// Opens Cache objects that start with 'font'.
caches.open(CURRENT_CACHES['font']).then(function(cache) {
return cache.match(event.request).then(function(response) {
if (response) {
console.log('Found response in cache:', response);
return response;
}
console.log('Fetching request from the network');
return fetch(event.request).then(function(networkResponse) {
cache.put(event.request, networkResponse.clone());
return networkResponse;
});
}).catch(function(error) {
// Handles exceptions that arise from match() or fetch().
console.error('Error in fetch handler:', error);
throw error;
});
})
);
});
我不确定如何使用IndexedDB 其他方法是