2015-03-25 2 views
28

con fetch('somefile.json'), è possibile richiedere che il file venga prelevato dal server e non dalla cache del browser?fetch(), come si effettua una richiesta non memorizzata nella cache?

in altre parole, con fetch(), è possibile aggirare la cache del browser?

+1

Hai un riferimento per recuperare * * in [* ECMA-262 ed 6 bozza *] (https://people.mozilla.org/~jorendorff/es6-draft.html)? Non lo vedo O intendi il [* WHATWG Fetch standard di vita *] (https://fetch.spec.whatwg.org)? – RobG

+0

RobG - utilizza https://developer.mozilla.org/en-US/docs/Web/API/FetchEvent –

risposta

43

Fetch può utilizzare un oggetto init contenente molte impostazioni personalizzate che è possibile applicare alla richiesta, inclusa un'opzione denominata "intestazioni".

L'opzione "intestazioni" occupa un oggetto Header. Questo oggetto ti consente di configurare le intestazioni che vuoi aggiungere alla tua richiesta.

Aggiungendo Pragma: no-cache e una cache-control : no-cache per l'intestazione si forzare il browser per controllare il server per vedere se il file è diverso dal file che ha già in il cache. È inoltre possibile utilizzare il controllo della cache : nessun-store in quanto semplicemente non consente al browser e a tutte le cache intermedie di memorizzare alcuna versione della risposta restituita.

Ecco un esempio di codice:

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>

Spero che questo aiuti. uso

+0

eccezionale! grazie mille –

+4

Che ne dici di usare 'nuova richiesta' e passare alcuni argomenti alle opzioni' cache'? Sto cercando di usarlo, ma non funziona. – Mitar

19

Più facile di modalità di cache:

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

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

+0

Questa è una risposta più appropriata. Puoi gestire intestazioni come "If-Modified-Since" e "If-None-Match" con queste opzioni. – Nigiri

+0

Sembra funzionare in Firefox (54) ma non in Chrome (60). la risposta di burningfuses funziona. – Scimonster

5

È possibile impostare 'Cache-Control': 'no-cache' nell'intestazione come questo ::

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