首页 > 其他 > 详细

PWA相关代码

时间:2019-09-27 18:22:28      阅读:95      评论:0      收藏:0      [点我收藏+]

sw.js 文件

 

let CacheName = ‘plus-v1‘;
var filesToCache = [
];

self.addEventListener(‘install‘, function (e) {
    console.log(‘[ServiceWorker] Install‘);
    e.waitUntil(
        caches.open(CacheName).then(function (cache) {
            console.log(‘[ServiceWorker] Caching app shell‘);
            return cache
                .addAll(filesToCache)
                .then(function () {
                    return self.skipWaiting();
                })
                .catch(function (error) {
                    console.log(‘Failed to cache:‘, error);
                });
        })
    );
});

self.addEventListener(‘activate‘, function (e) {
    console.log(‘[ServiceWorker] Activate‘);
    e.waitUntil(
        caches
        .keys()
        .then(function (keyList) {
            return Promise.all(
                keyList.map(function (key) {
                    console.log(`key-----------${key}`);
                    if (key !== CacheName) {
                        console.log(‘[ServiceWorker] Removing old cache‘, key);
                        return caches.delete(key);
                    }
                })
            );
        })
        .then(function () {
            return self.clients.claim();
        })
    );
});

self.addEventListener(‘fetch‘, function (event) {
    console.log(‘[Service Worker] Fetch‘, event.request.url);
    event.respondWith(
        caches.match(event.request).then(function (response) {
            // 如果 Service Worker有自己的返回,就直接返回,减少一次 http 请求
            if (response) {
                return response;
            }
            // 如果 service worker 没有返回,那就得直接请求真实远程服务
            var requestToCache = event.request.clone(); // 把原始请求拷过来
            return fetch(requestToCache).then(function (httpRes) {
                // http请求的返回已被抓到,可以处置了。

                // 请求失败了,直接返回失败的结果就好了。
                if (!httpRes || httpRes.status !== 200) {
                    return httpRes;
                }

                // 请求成功的话,将请求缓存起来。
                var responseToCache = httpRes.clone();
                // 选择性缓存数据
                if (
                    /\.js$|\.css$|\.jpg$|\.webp$|\.svg$|\.png$/.test(requestToCache.url) &&
                    !/sw/.test(requestToCache.url)
                ) {
                    caches.open(CacheName).then(function (cache) {
                        cache.put(requestToCache, responseToCache);
                    });
                }
                return httpRes;
            });
        })
    );
});

 

PWA相关代码

原文:https://www.cnblogs.com/xiaozhumaopao/p/11599366.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!