Hello. There is a site on codeighniher , funny in the following. If I change something in styles or js , in separate files, then in order to get the change I need to refresh the page with clearing the cache ( ctrl+f5 in chrome).
If you just refresh the page, then there are no changes on the site.
If you write styles in the html itself, then the information is easily changed by a simple f5 .
What could be the problem? Where to dig? For every occasion I throw off the htaccess file

 RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond $1 !^(index.php|js|images|stylesheets|robots.txt) RewriteRule ^(.*)$ index.php?/$1 [L] AddDefaultCharset utf-8 <Files *> Header set Cache-Control: "private, pre-check=0, post-check=0, max-age=0" Header set Expires: 0 Header set Pragma: no-cache </Files> 
  • Show better what headers really give, and not a config. This will eliminate a lot of questions. Tab network if in Chrome. - Total Pusher
  • And where to find these headlines, opened the network, but there a lot of kapez happened - duddeniska
  • And you filter on .js or .css. You need to watch Response Headers - show them. cache-control: no-cache, no-store, must-revalidate — that's what's on SO. Although your headers due to max-age=0 should also be banned. Waiting for headlines - Total Pusher
  • Request Method: GET Status Code: 200 OK (from memory cache) Remote Address: 195.210.46.23:80 Referrer Policy: no-referrer-when-downgrade Cache-Control: max-age = 315360000 - duddeniska
  • Now we need to understand why Cache-Control: max-age=315360000 . What web server cost? - Total Pusher

1 answer 1

If Nginx in front of Apache and configured to quickly return statics (CSS, JS, images), and Apache control is not even transferred. Therefore, the settings from .htaccess do not affect the headers, and Nginx often set up for long-term caching:

 location ~* ^.+\.(jpg|jpeg|gif|png|css|js|bmp|txt)$ { access_log off; expires 14d; # кеширование в браузере на 14 дней break; } 

The first thing to find out is exactly which response headers are sent from the web server. This can be done in the Network tab in the Chrome Developer Console or Сеть in Firefox. Next, we filter by the .css or .js mask and pay attention to the headers:

  • Expires (the flaw is that the client may have the wrong time) and
  • Cache-Control (deprived of this disadvantage).

In the Chrome developer tools, in the Size column, the inscriptions from disk cache and from memory cache just indicate that the resource is being loaded from the cache. And the option Disable cache completely disables the cache, resources are always loaded from the server, but only while the developer tools are open.

Via curl:

 curl -I "http://site.ru/js/scrips.js" 

To prevent caching, it should be:

 Cache-Control: no-cache, no-store, must-revalidate 

or

 Cache-Control: max-age=0 

or Expires indicates a date in the past (minus 1 year, for example).

To understand whether Apache is involved in the formation of the server's response, you can put an additional header, through the same .htaccess :

 # Ставим заголовок для понимания Header set BackServer "Apache" 

If it is not possible to configure the web server (as in a given question), the file should contain the date of its modification in the form of a GET parameter, for example, like this:

 <script src="/js/scrips.js?<?=date('ymdHis', filemtime($_SERVER['DOCUMENT_ROOT'] . '/js/scrips.js'))?>"></script> 

Caching in this case will also work, but if you change the file, the GET parameter will change, which will force the browser to download the new version.