how to use Service Worker to cache cross domain resources if the response is 404?

前端 未结 2 1724
南旧
南旧 2021-02-14 17:02

w3:

6.2 Cross-Origin Resources and CORS¶
Applications tend to cache items that come from a CDN or other origin. It is possible to request

2条回答
  •  一生所求
    2021-02-14 17:14

    The mode of a Request (allegedly) defaults to "no-cors". (I say "allegedly" because I believe I've seen situations in which an implicitly created Request used in fetch() results in a CORS-enabled Response.)

    So you should be explicit about opting in to CORS if you know that your server supports it:

    var corsRequest = new Request(url, {mode: 'cors'});
    fetch(corsRequest).then(response => ...); // response won't be opaque.
    

    Given a properly configured remote server, a CORS-enabled Request will result in a Response that has a type of "cors". Unlike an "opaque" Response, a "cors" Response will expose the underlying status, body, etc.

提交回复
热议问题