Not much use this metatag, it usually only works for HTML, which the Pagespeed Insights suggests is to cache files called via <script>
, <link>
, <img>
, <video>
, background-image: url(...)
like images, javascript, css, fonts, video, etc.
Note:
The use of no-cache means that it will not store in the cache and what you want is the opposite of
<meta http-equiv="Cache-control" content="no-cache">
Answering the question, yes it is very advantageous to use cache, this will decrease the consumption of your server making the response time better and avoiding possible server crashes when it has a high volume of accesses, as there will be fewer requests due to the "client side cache".
It is possible to do it using Apache (for your other questions I believe you use PHP and this usually uses Apache servers), you can then make use of .htaccess
, create the file .htaccess
in the root folder of your website (for example public_html
or /etc/www
) and put the following content into it:
#Cache de 1 mês a partir da data de acesso do arquivo
<FilesMatch "\.(?i:ico|gif|jpe?g|png|css|js|svg)$">
ExpiresDefault "access plus 1 mouth"
</FilesMatch>
This will cache 1 month for ico, gif, jpg, jpeg, png, css and js files.
In addition to the cache there is an HTTP code called 304, to use it you need to compare your browser cache with the server file through the modification date or through Etag as I explained in this question:
In the back end the server or application compares the header of the request (or Etag) and if there is no change the 304 Not modified
is issued and no data is sent in the reply, to avoid downloading the file again if there are no changes, then the browser reuses the cache of the local machine (browser).
So your . htaccess could look like this to use the 304:
# Trabalha o if-modified-since com arquivos de imagem
<FilesMatch "\.(?i:ico|gif|jpe?g|png|css|js|svg)$">
# Cache para um mês
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 1 mouth"
</IfModule>
# Remove Etag para previnir o uso do mesmo
# Pois iremos trabalhar com if-modified-since e last-modifed
FileETag None
</FilesMatch>
You can also set a different expiration time for each file type, let’s assume that your site updates 1 time a week the images, but the css and js take rarely and the favicon will hardly be updated, can do so:
# Ativa o cache
<FilesMatch "\.(?i:ico|gif|jpe?g|png|css|js|svg)$">
<IfModule mod_expires.c>
ExpiresActive On
</IfModule>
FileETag None
</FilesMatch>
# Cache de 1 semana pras imagens
<FilesMatch "\.(?i:gif|jpg|jpeg|png)$">
<IfModule mod_expires.c>
ExpiresDefault "access plus 1 mouth"
</IfModule>
</FilesMatch>
# Cache de 1 ano pros icones (geralmente favicon)
<FilesMatch "\.(?i:ico)$">
<IfModule mod_expires.c>
ExpiresDefault "access plus 1 year"
</IfModule>
</FilesMatch>
# Cache de 6 meses pra arquivos js e css
<FilesMatch "\.(?i:css|js)$">
<IfModule mod_expires.c>
ExpiresDefault "access plus 6 mouths"
</IfModule>
</FilesMatch>
# Remove o Etag no final
<FilesMatch "\.(?i:ico|gif|jpg|jpeg|png|css|js|svg)$">
FileETag None
</FilesMatch>
Note that I used access plus
, this means that the cache time is counted like this for example:
date and time of access (file request) + 6 months = date the cache will expire
There is the modified plus
that works like this:
date and time of last file modification on server + 6 months = date that will expire the cache
I didn’t use the modified plus
because if the file is old (not modified) it does not cache.
You still haven’t chosen an answer to your question
– durtto