12

How can I cache a few specific JavaScript & CSS files. I found advice from this site to put this in my .htaccess file

AddOutputFilter DEFLATE css js
ExpiresActive On
ExpiresByType application/x-javascript A2592000

But it is incomplete. What is .htaccess and how do I create it, where to store it, in my web root folder?

What is the meaning of the following statements:

AddOutputFilter DEFLATE css js
ExpiresActive On
ExpiresByType application/x-javascript A2592000

I dont want to cache all my Javascript & CSS files rather just a few specific files which will never change.

How can I do this?

2
  • 2
    What web server are you using? .htaccess is a control file for the Apache web server. If you are using IIS it has no meaning. Commented Sep 24, 2011 at 18:29
  • i will use IIS...so please advise me what should i do. what i need to write in my asp.net file to cache few specific js & css file. plzz discuss in detail. thanks Commented Sep 24, 2011 at 18:31

3 Answers 3

25

You can configure IIS to cache specific files by extension. For example:

Select the folder where your css/js files reside and then click on Output Caching. enter image description here

Then add the file extensions that you want to cache:

enter image description here

I don't think you can specify which ones to cache on a per file basis unless you write an http handler module to add the appropriate headers for each file independently, but from IIS this is how is done.

Then you can verify that you are getting 304 responses using firebug / fiddler or your tool of choice.

I hope this is helpful.

Sign up to request clarification or add additional context in comments.

3 Comments

can i cache js & css file by http handler....if so can point me any good article for this.
@user750398 look at this article: blog.sb2.fr/post/2008/11/21/…
Output Caching is not the same as controlling browser cache. They are quite different. Output Caching allows IIS to internally cache dynamic page content so that it can serve it to the client faster. It has no effect on the client's browser cache.
1

That's cool i would anyways add a file revisions and make IIS cache css + js files. As Icarus specified.

Then if you update the files on the server just add a new revision number this could also be a timestamp.

Comments

0
  1. AddOutputFilter DEFLATE: output is compressed(usually gzip) before sent to server in this case css & js your css and js.
  2. ExpiresActive On: Sets Expires and cache control headers to On
  3. ExpiresByType: Set the expire rule for the content type in this case application/x-javascript to a month A2592000 => 24 * 3600 * 30 its in ms.

I would recommend to instead of handling specific js via .htaccess you should add a revisions number to your js files, ala:

<script src="link/to/file.js?rev=xxx"></script>

Then when you update your files just update the rev number. And your clients will automatically be served the new files.

Resources:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.