fetch

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.

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,991評論 19 139
  • JavaScript 通過XMLHttpRequest(XHR)來執行異步請求,這個方式已經存在了很長一段時間。雖...
    Bruce_zhuan閱讀 2,515評論 1 10
  • 1. 輪播的實現原理是怎樣的?如果讓你來實現,你會抽象出哪些函數(or接口)供使用?(比如 play()) 輪播...
    madpluto閱讀 270評論 0 0
  • 昨天睡的很早,早到醞釀作業時睡著了,一覺到早上8點,起來依然覺得很累。今天回來早,5點到家,躺車上又睡著了,又睡到...
    一個意外的結尾閱讀 157評論 1 0
  • 有時候真的想放棄,不知道堅持干嘛。 只覺得失望。失望的時候想一個人待著。 不能如愿,我也改變不了什么。 是我太強悍...
    風煙云雨閱讀 162評論 0 0