码迷,mamicode.com
首页 > 系统相关 > 详细

[PWA] 11. Serve skeleton cache for root

时间:2016-05-18 06:52:39      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:

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);
        })
    );
});

 

[PWA] 11. Serve skeleton cache for root

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/5503847.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!