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.