最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use service-worker if resources are on different domains? - Stack Overflow

programmeradmin2浏览0评论

As we know service worker is registered from the root of the project folder but, is there any way to fetch some static resources (css, js) from different domain(s)?

Example :

  • myWebsite // main url (e.g. index.html)
  • abc/css/style.css // css file path
  • xyz/js/jsFile.js //javascript file path

As we know service worker is registered from the root of the project folder but, is there any way to fetch some static resources (css, js) from different domain(s)?

Example :

  • myWebsite. // main url (e.g. index.html)
  • abc./css/style.css // css file path
  • xyz./js/jsFile.js //javascript file path
Share Improve this question edited Nov 24, 2015 at 9:47 Bartłomiej Semańczyk 62k52 gold badges250 silver badges401 bronze badges asked Nov 24, 2015 at 9:41 pritipriti 211 silver badge3 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

Any request that es from the page (except iframe sub-resources) will pass through the service worker. These resources can be from the same or different origin. You can cache these resources so that they are available offline however as a developer you can't manipulate or inspect the contents of them unless the resources have the correct CORS header set.

For example, in your install event, you can Cache a remote file that is not on your domain.

self.addEventListener('install', function(e) {
  e.waitUntil(
    caches.open('airhorner').then(function(cache) {
      return cache.addAll([
        'https://paul.kinlan.me/'
      ]);
    })
  );
});

It is by default possible to fetch and cache cross-origin resources according to the specification:

the [cross-origin] objects stored are Response objects with the type attribute set to "opaque" [...] less expressive API than Responses typed "basic"; the bodies and headers cannot be read or set, nor many of the other aspects of their content inspected

发布评论

评论列表(0)

  1. 暂无评论