header expire on javascript files not working
Asked Answered
D

4

6

I can't get the expire header on JS files to work properly.. It works fine on eg CSS files!?

Could anybody tell me what Im doing wrong? :(

htaccess

ExpiresActive on
ExpiresDefault A0
ExpiresByType image/gif A29030400
ExpiresByType image/jpeg A29030400
ExpiresByType image/png A29030400
ExpiresByType text/css A29030400
ExpiresByType text/javascript A29030400
ExpiresByType application/x-javascript A29030400

header on CSS file

Cache-Control   max-age=29030400
Connection  Keep-Alive
Date    Sun, 14 Oct 2012 12:06:45 GMT
Etag    "be-4cbcf2ad48b40"
Expires Sun, 15 Sep 2013 12:06:45 GMT
Keep-Alive  timeout=5, max=97
Server  Apache/2.4.2 (Win64) PHP/5.4.3

header on JS file

Cache-Control   max-age=0
Connection  Keep-Alive
Date    Sun, 14 Oct 2012 12:06:45 GMT
Etag    "1ad-4cc00d069f493"
Expires Sun, 14 Oct 2012 12:06:45 GMT
Keep-Alive  timeout=5, max=99
Server  Apache/2.4.2 (Win64) PHP/5.4.3
Dichroite answered 14/10, 2012 at 12:13 Comment(3)
As a test, are the values in the htaccess file those actually being shown in the header for the CSS? EG: if you change the A29030400 to something else, do you see that reflected in the served CSS header? Also, as another test, what happens if you try formatting the htaccess in a different way: ... ExpiresByType text/css A29030400 <filesMatch "\.(js)$"> ExpiresDefault A29030400 </filesMatch> ?Semidome
add an answer.. the reg exp solution with filesMatch works! :DDichroite
Glad it helped, answer added.Semidome
S
7

Try wrapping an ExpiresDefault in a <filesMatch tag instead of using ExpiresByType :

<filesMatch "\.(js)$"> 
    ExpiresDefault A29030400 
</filesMatch>
Semidome answered 17/10, 2012 at 13:55 Comment(0)
M
9

(can't comment yet...) I run the following settings and it works.

ExpiresByType text/js "access plus 1 years"
ExpiresByType text/javascript "access plus 1 years"
ExpiresByType application/javascript "access plus 1 years"
ExpiresByType application/x-javascript "access plus 1 years" 

Should work

Cheers Robin

Millwork answered 17/10, 2012 at 1:48 Comment(0)
S
7

Try wrapping an ExpiresDefault in a <filesMatch tag instead of using ExpiresByType :

<filesMatch "\.(js)$"> 
    ExpiresDefault A29030400 
</filesMatch>
Semidome answered 17/10, 2012 at 13:55 Comment(0)
S
1

You might need to activate the Expires module from inside your Apache host configuration.

In my case, I added

  ExpiresActive On

right before the first ExpiresByType directive and it started working instantly (after an Apache reload).

I'm not sure why this needs to be explicitly enabled. On other servers it used to work as soon as I enabled the mod with a2enmod (maybe with Apache 2.2, but I can't find a clear pattern)

Sparks answered 5/2, 2016 at 16:27 Comment(0)
F
0

Old question but here's what I learned:

Press F12 in your browser (I'm using Chrome) and go to the "Network" tab and look for the javascript that's not getting cached. Look for the header information there (via a "Headers" tab, for example) and look at the "content-type" in the response header information.

In my case it was "application/javascript", which I hadn't defined specifically in my .htaccess. Once I added that line:

ExpiresByType application/javascript "access plus 2419200 seconds"

Then javascript was getting cached as expected.

Faints answered 11/3, 2018 at 16:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.