javascript - fetch(),你如何发出非缓存请求?

标签 javascript html fetch-api

使用 fetch('somefile.json'),可以请求从服务器而不是浏览器缓存中获取文件吗?

换句话说,使用fetch(),是否可以绕过浏览器的缓存?

最佳答案

更容易使用缓存模式:

  // Download a resource with cache busting, to bypass the cache
  // completely.
  fetch("some.json", {cache: "no-store"})
    .then(function(response) { /* consume the response */ });

  // Download a resource with cache busting, but update the HTTP
  // cache with the downloaded resource.
  fetch("some.json", {cache: "reload"})
    .then(function(response) { /* consume the response */ });

  // Download a resource with cache busting when dealing with a
  // properly configured server that will send the correct ETag
  // and Date headers and properly handle If-Modified-Since and
  // If-None-Match request headers, therefore we can rely on the
  // validation to guarantee a fresh response.
  fetch("some.json", {cache: "no-cache"})
    .then(function(response) { /* consume the response */ });

  // Download a resource with economics in mind!  Prefer a cached
  // albeit stale response to conserve as much bandwidth as possible.
  fetch("some.json", {cache: "force-cache"})
    .then(function(response) { /* consume the response */ });

引用:https://hacks.mozilla.org/2016/03/referrer-and-cache-control-apis-for-fetch/

关于javascript - fetch(),你如何发出非缓存请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29246444/

相关文章:

javascript - 我怎样才能让我的javascript读取表单值?

javascript - 在 Javascript 中使用 Ruby 的 << 连接运算符

jquery - 防止鼠标点击时输入模糊

html - 需要帮助将 div 放在彼此的顶部并将最后一个放在底部

javascript - 无法从前端 JavaScript 访问跨源响应 header

javascript - 获取 API 下载进度指示器?

javascript - 在 CodeIgniter 中的 onChange 事件上提交特定表单

javascript - 如何使用 Ramda 深度映射对象

javascript - 使用 Angular 2+ 在 IFrame 上加载

javascript - fetch api 从服务器获取错误消息而不是通用消息