serviceWorker可缓存跨域的资源吗?

目前看到的网络上关于serviceWorker的介绍不一例外的都是缓存了和页面同属于一个域下的资源。那要是缓存跨域的呢?这里有一份针对本地资源的配置:

<script>
if ('serviceWorker' in navigator) {
  window.addEventListener('load', function() {
    navigator.serviceWorker.register('serviceworker.js').then(function(registration) {
      console.log('ServiceWorker registration successful with scope: ', registration.scope);
    }).catch(function(err) {
      console.log('ServiceWorker registration failed: ', err);
    });
  });
}
</script>
//serviceworker.js:
var CACHE_NAME = 'hello-v1.3';
var urlsToCache = [
  '/hello/',
  'style.css?v=2017.11.25',
  'css3-mediaqueries.js',
  'iconfont.eot?v=3',
  'iconfont.svg?v=3',
  'iconfont.ttf?v=3',
  'iconfont.woff?v=3'
];

self.addEventListener('install', function(event) {
  event.waitUntil(
    caches.open(CACHE_NAME)
      .then(function(cache) {
        console.log('Opened cache');
        return cache.addAll(urlsToCache);
      })
  );
});

self.addEventListener('fetch', function(event) {
  event.respondWith(
    caches.match(event.request)
      .then(function(response) {
        if (response) {
          return response;
        }
        return fetch(event.request);
      }
    )
  );
});

self.addEventListener('activate', function(event) {
  var cacheWhitelist = 'hello-v';
  event.waitUntil(
    caches.keys().then(function(cacheNames) {
      return Promise.all(
        cacheNames.map(function(cacheName) {
          if (cacheName.indexOf(cacheWhitelist) != -1 && cacheName != CACHE_NAME) {
            return caches.delete(cacheName);
          }
        })
      );
    })
  );
});
阅读 5.5k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题