Fetch API 是基于Promise 设计
mode 是 Service Worker 实现中最重要的参数,它的值可能是:
cors: 采用跨域资源共享的方式去获取。要求对方服务器启用了 CORS 响应头,可以读取响应内容。否则会跨域失败。
no-cors:采用非跨域资源共享的方式去获取。不要求对方的 CORS 设置,不可读取响应内容。
same-origin:采用同域的方式去获取,如果 URL 跨域就会失败。
cache: 缓存模式,可取: default, no-store, reload, no-cache, force-cache, only-if-cached。
"default" Fetch will inspect the HTTP cache on the way to the network. If there is a fresh response it will be used. If there is a stale response a conditional request will be created, and a normal request otherwise. It then updates the HTTP cache with the response. [HTTP]
"no-store" Fetch behaves as if there is no HTTP cache at all.
"reload" Fetch behaves as if there is no HTTP cache on the way to the network. Ergo, it creates a normal request and updates the HTTP cache with the response.
"no-cache" Fetch creates a conditional request if there is a response in the HTTP cache and a normal request otherwise. It then updates the HTTP cache with the response.
"force-cache" Fetch uses any response in the HTTP cache matching the request, not paying attention to staleness. If there was no response, it creates a normal request updates the HTTP cache with the response.
网友评论