1

There are a lot of questions concerning how NOT to cache, but no questions on how to cache JavaScript in IE(8).

I tried all 4 options under Browsing History -> Settings, but on each request the same thing happens: IE successfully downloads the newest version of the .js file. I've checked the log at C:\inetpub\logs\LogFiles\ and on every request I make, I can see that jsFile.js is requested:

2013-11-28 13:55:29 10.120.59.9 GET /developmentMachine1/js/jsFile.js - 80 - 10.241.247.58 Mozilla/4.0+(compatible;+MSIE+7.0;+Windows+NT+6.1;+WOW64;+Trident/5.0;+SLCC2;+.NET+CLR+2.0.50727;+.NET+CLR+3.5.30729;+.NET+CLR+3.0.30729;+Media+Center+PC+6.0;+.NET4.0C;+.NET4.0E;+InfoPath.3;+BRI/2) 304 0 0 62

How can I cache that file?

Edit: I'm using ASP to generate the page.

Edit 2: I solved this by adding the web.config file with the following contents in the js folder of my app.

<configuration>
   <system.webServer>
      <httpProtocol>
         <customHeaders>
            <add name="Cache-control" value="private" />
         </customHeaders>
      </httpProtocol>
   </system.webServer>
</configuration>
Robotronx
  • 1,728
  • 2
  • 21
  • 43
  • 4
    `304`? Seem to be working fine. – Qantas 94 Heavy Nov 28 '13 at 14:09
  • 1
    There are like a lot of links and answers in this link: http://stackoverflow.com/questions/311062/caching-javascript-files – Henk Jansen Nov 28 '13 at 14:09
  • I just changed the file, requested it again and it said 200. I guess it does cache correctly. But what's intriguing is that my IE setting is to look for never version of the requested page Never. I thought that meant it will never request the .js also. – Robotronx Nov 28 '13 at 14:14
  • 1
    your server has to return a `cache` header to instruct the browser to cache it when serving your javascript files. For more information: http://stackoverflow.com/questions/4480304/how-to-set-http-headers-for-cache-control – Khanh TO Nov 28 '13 at 14:16
  • 1
    The browser settings doesn't really matter, it's how the file is served that matters, and it should have headers that say when the file expires (how long it will be cached), otherwise it won't be cached at all. – adeneo Nov 28 '13 at 14:18
  • @adeneo: so, my browser will (currently) request this .js no matter what, but since the file hasn't changed, the server will just return 304 and the browser will use the version it stored locally? – Robotronx Nov 28 '13 at 14:39
  • Yes, that's generally how it works. – adeneo Nov 28 '13 at 14:50
  • @KhanhTO: But I'm still unclear on how to add the necessary cache header into my JavaScript files. I know I should add to cache on the browser, but where do I put it? It won't let me put it in the .js, browser throws an error. – Robotronx Nov 28 '13 at 15:51
  • 1
    It depends on the web server you're using. There are other ways to do this. The idea is to intercept the .js request and append the `Cache-control` header to the response. – Khanh TO Nov 29 '13 at 13:24
  • I'm using IIS7. I did it by adding `web.config` file in the js folder. – Robotronx Nov 29 '13 at 15:42

0 Answers0