Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
143
rated 0 times [  150] [ 7]  / answers: 1 / hits: 104181  / 9 Years ago, wed, march 25, 2015, 12:00:00

with fetch('somefile.json'), it is possible to request that the file be fetched from the server and not from the browser cache?



in other words, with fetch(), is it possible to circumvent the browser's cache?


More From » html

 Answers
1

Fetch can take an init object containing many custom settings that you might want to apply to the request, this includes an option called "headers".


The "headers" option takes a Header object. This object allows you to configure the headers you want to add to your request.


By adding pragma: no-cache and a cache-control: no-cache to your header you will force the browser to check the server to see if the file is different from the file it already has in the cache. You could also use cache-control: no-store as it simply disallows the browser and all intermediate caches to store any version of the returned response.


Here is a sample code:




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;
});

<!DOCTYPE html>
<html lang=en>
<head>
<meta charset=UTF-8>
<title>ES6</title>
</head>
<body>
<img src=>
</body>
</html>




[#67327] Saturday, March 21, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
sidneyh

Total Points: 118
Total Questions: 108
Total Answers: 105

Location: Mali
Member since Fri, Jun 18, 2021
3 Years ago
;