14

I am trying to modify my .htaccess file by specifying an expiration for resources. It has worked for images but not for javascript files. When running GTMetrix it still recommends that the javascript files need expiration. I have tried "application/javascript" and "application/x-javascript" but to no avail.

Not sure what I am doing wrong.

Here is my code:

     ## EXPIRES CACHING ##
    <IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpg "access 1 year"
    ExpiresByType image/jpeg "access 1 year"
    ExpiresByType image/gif "access 1 year"
    ExpiresByType image/png "access 1 year"
    ExpiresByType text/css "access 1 month"
    ExpiresByType application/pdf "access 1 month"
    ExpiresByType application/javascript "access 1 week"
    ExpiresByType application/x-shockwave-flash "access 1 month"
    ExpiresByType image/x-icon "access 1 year"
    ExpiresDefault "access 2 days"

    </IfModule>
    ## EXPIRES CACHING ##
kad
  • 179
  • 1
  • 2
  • 10

2 Answers2

23

Adding this will make it work.

ExpiresByType text/x-javascript "access plus 1 month"  
ExpiresByType application/javascript "access plus 1 month"  
ExpiresByType application/x-javascript "access plus 1 month"
Amjad
  • 1,627
  • 5
  • 21
  • 41
15

Using the Network tab in the browsers inspector Chrome/FireFox/Opera, you can check the asset and see what kind of "Content Type" is being served.

In my case it was Content-Type:"text/javascript"

So I added 4 permutations of ExpiresByType to my .htaccess file

ExpiresByType text/javascript "access plus 1 month"
ExpiresByType text/x-javascript "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"  
ExpiresByType application/x-javascript "access plus 1 month"

This solved my problem and I hope it will help others.

Craig London
  • 675
  • 1
  • 9
  • 14