I have a task. I have a server call to get list of images. Along with other information I have to add this information that weather it was loaded from cache or from server.
Is there a way that I can check the headers in the api call? If it is from cache I get status code 200 but I get extra information like this (from cache). If it is not from cache I only get status code 200.
Image 1 is for the those which is not loaded from cache
The next image is for those which are loaded from cache.
I have got that the answer has been already given or it is duplicate. I have tried that solution and it doesn't seem to work. Also it is chrome specific, I am looking for something more general that should work in all browsers. I hope this explains why I didn't went for that solution. I have searched it before asking here.
Looking forward for responses.
How can I determine if images are being cached with cloudflare? Your browser's Dev Tools (F12 in Chrome) have a Network tab where you can inspect each request. You'll see a CF-Cache-Status for them if they're a HIT or not. GTMetrix and WebPageTest Waterfall views also show the response headers.
The image is considered completely loaded if any of the following are true: Neither the src nor the srcset attribute is specified. The srcset attribute is absent and the src attribute, while specified, is the empty string ( "" ). The image resource has been fully fetched and has been queued for rendering/compositing.
In Google's search box, type the website or page you're trying to see. Beside the URL, click the down arrow. Select "Cached". You are now viewing the cached page.
Cached data are files, scripts, images, and other multimedia stored on your device after opening an app or visiting a website for the first time. This data is then used to quickly gather information about the app or website every time revisited, reducing load time.
You will have to handle your load bindings selectively. You can verify load by testing the Image object property complete
.
For example:
$('.images_to_load').each(function(index, elem)
{
if (!elem.complete) {
$(elem).on('load', function(){
console.log('image loaded from server');
});
}
else {
console.log('image loaded from cache');
}
});
How to use this code: It is important that you have image tags (without the src attribute) with class images_to_load
on your page when executing above code. After adding the src attribute (the url) you will be able to find out if an image has been loaded from cache or not.
With vanilla JS
const image = document.querySelector('.my-image-class');
if (image.complete) {
image.addEventListener('load', () => {
// Do stuff when image wasn't loaded from cache
});
} else {
// Do stuff when image has been loaded trough cache
}
Don't forget to remove the event listener when it isn't needed anymore.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With