Service Workers缓存 发表于 2018-05-22 | 之前一篇文章介绍了Service Workers的基本使用。那么现在这篇文章介绍一下Service Workers拦截请求并缓存。 123456789101112131415161718192021222324252627282930313233343536373839var CACHE_VERSION = 1;// Shorthand identifier mapped to specific versioned cache.var CURRENT_CACHES = { font: 'font-cache-v' + CACHE_VERSION};var urlsToCache = [ '/', './main.css', 'http://localhost:11080/gt/register2'];//service worker安装成功后开始缓存所需的资源self.addEventListener('install', function(event) { // Perform install steps event.waitUntil( caches.open(CURRENT_CACHES['font']) .then(function(cache) { console.log('Opened cache'); return cache.addAll(urlsToCache); }) );});//监听浏览器的所有fetch请求,对已经缓存的资源使用本地缓存回复self.addEventListener('fetch', function(event) { event.respondWith( caches.match(event.request) .then(function(response) { //该fetch请求已经缓存 if (response) { return response; } return fetch(event.request); } ) );}); hi you can see me 打赏 微信支付 支付宝