您的位置:首页 > 其它

[PWA] 11. Serve skeleton cache for root

2016-05-18 03:15 134 查看
Intead of cache the root floder, we want to cache skeleton instead.

self.addEventListener('install', function (event) {
event.waitUntil(
caches.open(staticCacheName).then(function (cache) {
return cache.addAll([
'/skeleton',
'js/main.js',
'css/main.css',
'imgs/icon.png',
'https://fonts.gstatic.com/s/roboto/v15/2UX7WLTfW3W8TclTUvlFyQ.woff',
'https://fonts.gstatic.com/s/roboto/v15/d-6IYplOFocCacKzxwXSOD8E0i7KZn-EPnyo3HZu7kw.woff'
]);
})
);
});


Respond to requests for the root page with thepage skeleton from the cache:

self.addEventListener('fetch', function (event) {
// use the page skeleton from the cache
let requestUrl = new URL(event.request.url);
if(requestUrl.origin === location.origin){
if(requestUrl.pathname === '/'){
event.respondWith(
caches.match('/skeleton')
);
return;
}
}

event.respondWith(
caches.match(event.request).then(function (response) {
return response || fetch(event.request);
})
);
});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: