Fetch API 是基于Promise 設(shè)計
mode 是 Service Worker 實現(xiàn)中最重要的參數(shù),它的值可能是:
cors: 采用跨域資源共享的方式去獲取顽冶。要求對方服務(wù)器啟用了 CORS 響應(yīng)頭缩多,可以讀取響應(yīng)內(nèi)容尸折。否則會跨域失敗笋庄。
no-cors:采用非跨域資源共享的方式去獲取彤灶。不要求對方的 CORS 設(shè)置旷太,不可讀取響應(yīng)內(nèi)容展懈。
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.