i'm working on javascript code and i want to cache http GET request to API Pixabay. Problem the caching doesn't seem to work at all. Datas are always null. I can put a sample of code maybe someone will see something wrong about my code :
if ('caches' in window){
const newCache = await window.caches.open('cache-pixabay');
const options = {
method: "GET",
headers: new Headers({
'Accept': 'application/json',
}),
}
let request = new Request(URL, options);
datas = await newCache.match(request);
alert(JSON.stringify(datas));
if(isEmptyObj(datas)) {
alert('iciii');
await newCache.add(request);
datas = await newCache.match(request);
alert(JSON.stringify(datas));
}
} else {
xhttp.open("GET", URL, false);
xhttp.send();
datas = JSON.parse(xhttp.responseText);
}
Alert always displays {} null object Response. Need some tips. It lacks of example on internet.