All the JavaScript files hosted from my server are not being cached - just those from 3rd party CDNs. The information on caching is bountiful, but I've been unable to create something that works. I'm wanting to cache based on last-modified time. Setting an expiry time isn't ideal because the JavaScript is included programmatically and I don't want to have to update a bunch of controllers every time a JavaScript file is updated. I've read that eTags don't work well/at all when the content is gzipped.

I have an .htaccess file in my /public directory. All my JavaScript is delivered from /public/js.

My .htaccess file looks like this:

Header set Cache-Control "no-cache must-revalidate"
Header unset Pragma
Header unset Expires

# This redirects any non-real-file (ie: images, css, js) to index.php
RewriteEngine On
RewriteRule ^(documentation) - [L] # Don't redirect requests for documentation pages
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]

I don't think the Rewrite stuff is relevant, but I'm here asking for help, so what do I know?

For app.js, here are the request headers from Firefox's Dev tools:

Host: my.website.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0
Accept: */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
Referer: https://my.website.com/path/to/page
Cookie: __unam=94c97c-15c6ae7197c-519465cd-470; _ga=GA1.2.1026744241.1497457052; __utma=87648711.1026744241.1497457052.1497548871.1522878163.3; programs#lang=en; lastSearchType=keywords; lastSearchType-keywords=.; sid=blah123; PHPSESSID=blah234
Connection: keep-alive
If-Modified-Since: Thu, 11 Oct 2018 17:16:03 GMT
If-None-Match: "1897-577f7201f9595-gzip"
Cache-Control: max-age=0

These are the response headers:

HTTP/1.1 200 OK
Date: Thu, 11 Oct 2018 19:58:31 GMT
Server: Apache/2.4.25 (Debian)
Last-Modified: Thu, 11 Oct 2018 17:16:03 GMT
ETag: "1897-577f7201f9595-gzip"
Accept-Ranges: bytes
Vary: Accept-Encoding
Content-Encoding: gzip
Cache-Control: no-cache must-revalidate
Content-Length: 2709
Keep-Alive: timeout=5, max=90
Connection: Keep-Alive
Content-Type: application/javascript

What needs to change to get my local files cached?

  • 1
    Do you want the browser to check in with the server every time to see if the file has been modified before it uses its cached version? – Maximillian Laumeister Oct 11 '18 at 22:31
  • @MaximillianLaumeister Yes. I realize that's not an ideal outcome, but it's the best solution for my situation. – Quasipickle Oct 12 '18 at 5:55
  • I'm curious to see if anybody else knows the real answer to this problem. However if you are including the JS programmatically, a workaround (that would actually improve your cache performance) is to use a long caching period with a content-based cachebusting URL such as /public/js/app.js?<sha256-hash-of-app.js>. – Maximillian Laumeister Oct 12 '18 at 15:41
  • 1
    I thought of that but that would require me to rewrite/extend the asset manager that is part of the Phalcon framework. That's certainly possible, but I thought there HAS to be a way to actually use last-modified caching. – Quasipickle Oct 12 '18 at 15:48
  • One thing I will say is that the Last-Modified response header looks pretty fishy to me. AFAIK it should be returning some date in the past, whereas right now it's effectively saying "this file was last modified the same second you sent your request". – Maximillian Laumeister Oct 12 '18 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.