What is the reason for the suffix "some number" in the CSS link? [duplicate]

4

I was seeing the sources of some sites, and sometimes I see something like this:

<link href="http://css.siteX.com/imagecache/.../ymPrompt.css?2014102701" type="text/css" rel="stylesheet">

I would like to understand why ?2014102701 .. Do you have any use?

Could it be something to reduce the probability of making this document request error when the site is full?

    
asked by anonymous 05.04.2015 / 22:15

1 answer

4

This is used to prevent caching in files that have been updated, in the case for the browser (or client software that supports HTTP) it considers these urls different addresses and therefore for each one will be generated a different cache:

//exemplo.com/static/css/style.css
//exemplo.com/static/css/style.css?
//exemplo.com/static/css/style.css?0000
//exemplo.com/static/css/style.css?0001

Then each modification of the style.css file changes the suffix value to prevent the browser from loading the cache file.

For example, the initial url is:

//exemplo.com/static/css/style.css?1428260681

This number on the front ( ?1428260681 ) is unix-time from the last modification of style.css , when edit it a server-side language (or something equivalent) for example it detects the last modification and adds a new number to the suffix, an example like PHP:

//Pega o arquivo pelo caminho real e retorna a última modificação em segundos
$utlima_modificao = filemtime('/home/user/www/project/static/style.css');

And we use this way:

<link href="static/style.css?<?php echo $utlima_modificao; ?>">

The variable $utlima_modificao will return a number in seconds (unix-time) of the last modification, so if there is a modification the number will be different.

In this way, if there is any modification in the CSS (or JS) file, we can prevent users from accessing the old version that may be in the cache.

Note that this is a basic example technique, there are many others, so the suffixes used will not always be the time in seconds of the last modification.

Reported:

If you want to know how to cache static files, read the link: It is possible to use if-modified-since with "304 not modified "without PHP

    
05.04.2015 / 22:50