Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if the image is loaded from cache or not

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 cacheenter image description here

The next image is for those which are loaded from cache.enter image description here

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.

like image 589
mohsinali1317 Avatar asked Oct 08 '15 07:10

mohsinali1317


People also ask

How do you know if a photo is cached?

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.

How do you know if an image is fully loaded?

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.

How do you check page is cached or not?

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.

Does cache store images?

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.


2 Answers

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.

like image 152
Thariama Avatar answered Sep 22 '22 16:09

Thariama


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.

like image 27
Eertmanhidde Avatar answered Sep 19 '22 16:09

Eertmanhidde