Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check browser's cache for a js file

How can I check for a javascript file in user's cache. If he refreshed the page or visits the site after sometime. I need not download that js file again. Does the js files get cleaned up after a site is closed.

like image 970
Rakesh Avatar asked Feb 11 '09 08:02

Rakesh


People also ask

How do you check if JS file is cached?

To determine if the Javascript (or any element) was cached, what you want to look for is the "Size" column. Data that is browser-cached will show "(from cache)" as the Size value. If you see anything else for Size, that means it was loaded over the network and wasn't loaded from the browser cache.

Are JS files cached in the browser?

JavaScript and CSS files are usually cached in the browser after the first access. The browser cache is used to store web application assets like images, CSS, and JS code on your computer's hard drive.

How do I check my browser cache data?

Tap Chrome menu > Settings. Tap (Advanced) Privacy. From the "Time Range" drop-down menu, select All Time. Check Cookies and Site data and Cached Images and Files.

How do I get JavaScript cache?

You can cache a resource using three methods add , addAll , set . add() and addAll() method automatically fetches a resource, and caches it, whereas in set method we will fetch a data and set the cache.


2 Answers

Whether a javascript file is cached depends on how your web server is setup, how the users browser is setup and also how any HTTP proxy servers between your server and the user are setup. The only bit you can control is how your server is setup.

If you want the best chance of your javascript being cached then you server needs to be sending the right HTTP headers with the javascript file. Exactly how you do that depends on what web server you are using.

Here are a couple of links that might help:

Apache - http://httpd.apache.org/docs/2.0/mod/mod_expires.html

IIS - http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/0fc16fe7-be45-4033-a5aa-d7fda3c993ff.mspx?mfr=true

like image 98
andynormancx Avatar answered Oct 02 '22 20:10

andynormancx


The browser will automatically look after what it has in it's own cache. There are various mechanisms you can use for controlling it however.

Look into the various HTTP caching headers, such as:

  • Last-Modified
  • Expires
  • ETag

The Expires header is the most critical of these when it comes to client-side caching. If you set a far future Expires header (eg, 10 years) the browser will not (in theory) not look to the server again for that file. Of course then you need a method of changing the file name when the contents of the the file change. Most people manage this by adding a build number to the file path.

like image 38
Andy Hume Avatar answered Oct 02 '22 20:10

Andy Hume