fetch(),你如何进行非缓存请求?

cc *_*ung 56 javascript html5 fetch-api

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

换句话说,有fetch()没有可能绕过浏览器的缓存?

bur*_*ses 82

Fetch可以包含一个init对象,其中包含许多您可能希望应用于请求的自定义设置,其中包括一个名为"headers"的选项.

"headers"选项采用Header对象.此对象允许您配置要添加到请求的标头.

通过向标头添加pragma:no-cachecache-control:no-cache,您将强制浏览器检查服务器以查看该文件是否与缓存中已有的文件不同.您还可以使用cache-control:no-store,因为它只是禁止浏览器和所有中间缓存来存储任何版本的返回响应.

这是一个示例代码:

var myImage = document.querySelector('img');

var myHeaders = new Headers();
myHeaders.append('pragma', 'no-cache');
myHeaders.append('cache-control', 'no-cache');

var myInit = {
  method: 'GET',
  headers: myHeaders,
};

var myRequest = new Request('myImage.jpg');

fetch(myRequest, myInit)
  .then(function(response) {
    return response.blob();
  })
  .then(function(response) {
    var objectURL = URL.createObjectURL(response);
    myImage.src = objectURL;
  });
Run Code Online (Sandbox Code Playgroud)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>ES6</title>
</head>
<body>
    <img src="">
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.

  • 那么使用`new Request`并将一些参数传递给`cache`选项呢?我试图使用它,但它不起作用. (4认同)
  • @IsaacLyman,虽然HTTP标头不区分大小写,但我建议您遵循建议的文档,即:"Cache-Control".参考:https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers (3认同)

MJ *_*ili 59

更容易使用缓存模式:

  // 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 */ });
Run Code Online (Sandbox Code Playgroud)

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

  • 这似乎适用于Firefox(54),但不适用于Chrome(60).burnfuses的答案确实有效. (8认同)
  • 我已经对其进行了测试,就今天(2019 年 11 月)而言,此方法似乎适用于 Windows、Linux 和 Android 上的 Opera、Chrome 和 FireFox。burningfuses 方法至少在 Opera 上失败。 (4认同)
  • 与获胜答案不同,这尊重 CORS。 (2认同)

Agu*_*ndo 10

你可以'Cache-Control': 'no-cache'像这样在头文件中设置::

return fetch(url, {
  headers: {
    'Cache-Control': 'no-cache'
  }
}).then(function (res) {
  return res.json();
}).catch(function(error) {
  console.warn('Failed: ', error);
});
Run Code Online (Sandbox Code Playgroud)


bba*_*ker 8

没有一个解决方案对我来说似乎很有效,但是这个相对干净(AFAICT)的黑客确实有效(改编自https://webmasters.stackexchange.com/questions/93594/prevent-browser-from-caching-text-file) :

  const URL = "http://example.com";
  const ms = Date.now();
  const data = await fetch(URL+"?dummy="+ms)
    .catch(er => game_log(er.message))
    .then(response => response.text());
Run Code Online (Sandbox Code Playgroud)

这只是添加了一个虚拟参数,该参数在每次调用查询时都会发生变化。无论如何,如果其他解决方案似乎有效,我建议使用这些解决方案,但在我的测试中,它们在我的情况下不起作用(例如那些使用Cache-Controlpragram)。

  • 谢谢你!这是唯一对我有用的事情。 (3认同)