Caching Minified JS

297 views
Skip to first unread message

Sam Mass

unread,
May 26, 2016, 2:22:14 PM5/26/16
to pagespeed-insights-discuss
Hi all. I've designed a web site for my client and used below .htaccess file:

# BEGIN Expire headers
<ifModule mod_expires.c>
  ExpiresActive On
  ExpiresDefault "access plus 600 seconds"
  ExpiresByType image/x-icon "access plus 2592000 seconds"
  ExpiresByType image/jpeg "access plus 2592000 seconds"
  ExpiresByType image/png "access plus 2592000 seconds"
  ExpiresByType image/gif "access plus 2592000 seconds"
  ExpiresByType application/x-shockwave-flash "access plus 2592000 seconds"
  ExpiresByType text/css "access plus 604800 seconds"
  ExpiresByType text/javascript "access plus 216000 seconds"
  ExpiresByType text/x-javascript "access plus 216000 seconds"
  ExpiresByType application/javascript "access plus 216000 seconds"
  ExpiresByType application/x-javascript "access plus 216000 seconds"
  ExpiresByType text/html "access plus 600 seconds"
  ExpiresByType application/xhtml+xml "access plus 600 seconds"
</ifModule>
# END Expire headers

# BEGIN Cache-Control Headers
<ifModule mod_headers.c>
  <filesMatch ".(ico|jpe?g|png|gif|swf)$">
    Header set Cache-Control "public"
  </filesMatch>
  <filesMatch ".(css)$">
    Header set Cache-Control "public"
  </filesMatch>
  <filesMatch ".(js)$">
    Header set Cache-Control "private"
  </filesMatch>
  <filesMatch ".(min.js)$">
    Header set Cache-Control "private"
  </filesMatch>
  <filesMatch ".(x?html?|php)$">
    Header set Cache-Control "private, must-revalidate"
  </filesMatch>
</ifModule>
# END Cache-Control Headers

The problem is, every file passed "leverage browser cache" warning except "xxxx.min.js" files. I've tried all types of combinations about Javascript seen above) but still, not working. The page speed insights tool sees that I gave this file 2,5 days. But still warns me about caching.

How can I cache minified js file?

I can't give you the web site link (my customer doesn't want me to do). But I can tell you that files are on our own servers.


SUMMARY:

Can't cache .min.js file which is in my own server. htaccess above.

Joshua Marantz

unread,
May 26, 2016, 2:45:22 PM5/26/16
to pagespeed-insights-discuss
Try mod_pagespeed for Apache :)  It does exactly this (and lots more, if you want it to, but you can configure it to do *only* what you just asked for:

   ModPagespeedRewriteLevel PassThrough
   ModPagespeedEnableFilters rewrite_javascript

To get that to work you'll need to change this stanza:

  <filesMatch ".(js)$">
    Header set Cache-Control "private"
  </filesMatch>

Why do you have your JS as cc:private?  For mod_pagespeed to work it's going to (by policy) need your JS to be publicly cacheable, just like a CDN would.  Once done, that will minify, cache-extend gzip, and server-side cache your JS.  If you want to also minify, gzip, and cache your CSS, you can add
   ModPagespeedEnableFilters rewrite_css

For HTML (minification, not cache):
   ModPagespeedEnableFilters collapse_whitespace,remove_comments

See https://developers.google.com/speed/pagespeed/module/ for details, download instructions, etc.

You can then also then get into recompressing images, inlining critical CSS, transcoding jpeg to webp for Chrome, etc.  It's easy to try these one at a time. If this sounds useful and you have more questions, the group for that is https://groups.google.com/forum/#!forum/mod-pagespeed-discuss

-Josh


--
You received this message because you are subscribed to the Google Groups "pagespeed-insights-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pagespeed-insights-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pagespeed-insights-discuss/11d101b2-2555-4688-8c7d-fd1c8b98fff4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages