我正在尝试使用Service Worker预缓存一些静态应用程序外壳文件。我不能使用“ sw-appcache”,因为我没有使用任何构建系统。但是,我尝试使用“ sw-toolbox”,但无法将其用于预缓存。
这是我在服务工作者JS中所做的事情:
self.addEventListener('install', function(event) {
event.waitUntil(
caches.open('gdvs-static').then(function(cache) {
var precache_urls = [
'/',
'/?webapp=true',
'/css/gv.min.css',
'/js/gv.min.js'
];
return cache.addAll(precache_urls);
});
);
});
我也尝试过这个:
importScripts('/path/to/sw-toolbox.js');
self.addEventListener('install', function(event) {
var precache_urls = [
'/',
'/?webapp=true',
'/css/gv.min.css',
'/js/gv.min.js'
];
toolbox.precache(precache_urls);
});
这是我的应用程序的URL:https : //guidedverses-webapp-staging.herokuapp.com/这是我的服务工作者文件的URL:https : //guidedverses-webapp-staging.herokuapp.com/gdvs-sw。 js
我想念什么?
傻我 我忘记将提取处理程序添加到我的服务工作者中。我认为它的工作原理类似于appchache,并且在与缓存URL匹配时自动返回缓存的数据。我低估了服务人员的力量。以下代码对我有用。
this.addEventListener('fetch', function(event) {
console.log(event.request.url);
event.respondWith(
caches.match(event.request).then(function(response) {
return response || fetch(event.request);
})
);
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句