Caching issue with the AddHandler application / x-httpd-php application

Using .htaccess, I install a PHP handler for all of my .css and js to output user agent based code:

AddHandler application/x-httpd-php .css .js 

For instance:

 <?PHP if ($CurrentBrowser == 'msie') { ?> .bind('selectstart', function(event) { ... }) <?PHP } ?> 

Thus, my code files are dynamically created, but can be considered static files. This is because, as soon as they were compiled for the first time, browsers can return them from the cache and reuse them until I change their contents. This is why I use fingerprints / versions and for a long time on them:

 [INDEX.PHP] <script type="application/javascript" src="<?PHP echo GetVersionedFile('/script.js'); ?>"></script> <script type="application/javascript" src="/script.1316108341.js"></script> [.HTACCESS] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule "^(.+)\.(\d+)\.(css|js)$" $1.$3 [L] 

The problem is that these files, even if I send them with the correct header, are never cached by any browser (I never get the code 304, always 200). This is my server response log:

 [CHROME] Request URL:http://127.0.0.1:8888/script.1316108341.js Request Method:GET Status Code:200 OK ----- Cache-Control:max-age=31536000, public Connection:Keep-Alive Content-Encoding:gzip Content-Length:6150 Content-Type:application/javascript Date:Thu, 15 Sep 2011 21:41:25 GMT Expires:Fri, 14 Sep 2012 21:41:25 GMT Keep-Alive:timeout=5, max=100 Server:Apache/2.2.17 (Win32) PHP/5.3.6 Vary:Accept-Encoding X-Powered-By:PHP/5.3.6 [MOZILLA] Request URL:http://127.0.0.1:8888/script.1316108341.js Request Method:GET Status Code:200 OK ----- Date Thu, 15 Sep 2011 21:43:26 GMT Server Apache/2.2.17 (Win32) PHP/5.3.6 X-Powered-By PHP/5.3.6 Content-Encoding gzip Vary Accept-Encoding Cache-Control max-age=31536000, public Expires Fri, 14 Sep 2012 21:43:26 GMT Content-Type application/javascript Content-Length 6335 Keep-Alive timeout=5, max=100 Connection Keep-Alive ----- Last Modified Thu Sep 15 2011 23:43:26 GMT+0200 (= time i loaded the page) (???) Last Fetched Thu Sep 15 2011 23:43:26 GMT+0200 (= time i loaded the page) (???) Expires Fri Sep 14 2012 23:43:26 GMT+0200 Data Size 6335 Fetch Count 10 Device disk 

What could be the problem? How can I force cache these files? Thank you very much!

+1
source share
1 answer

Because requests for PHP and CSS files are handled by PHP, your PHP code with its conditional expressions is executed every time.

Apache / PHP has no idea whether the content is cacheable or whether it needs to be regenerated, so every time it executes your PHP code.

If you send the last modified header or use the version / fingerprint management method, then your responsibility in your PHP script is to check the fingerprint or version and determine how valid it is. If so, you can send the 304 Not Modified header and stop further processing. You can also check the request headers for the Last-Modified tag and use this method.

Another approach would be to cache the response for different browsers and dates to a file so that you can serve this file for users for the first time, rather than regenerating it with php. You can then check the modification time of this file to determine if you can send the 304 header.

This SitePoint article explains several ways to use PHP for caching. Hope this helps.

+5
source

Source: https://habr.com/ru/post/918228/


All Articles