What is the reason for the suffix "? algumnumero" in the link of some CSS?

Asked

Viewed 105 times

4

I was going through the sources of some websites, and sometimes I see something like this:

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

I’d like to understand why ?2014102701.. Have some use?

It may be something to decrease the probability of error in the request to this document when the site is full?

  • 1

    This forces you not to cache old versions of CSS. I particularly prefer to write already in the file name if it is to do so, instead of saving with the same name.

  • hahaha. Really, Versionar is a good one, since if the site is constant and there is no change in the structure of the file all day then leave cached is better for user experience right..

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 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 every file modification style.css the suffix value is changed to prevent the browser from loading the cache file.

For example, the initial url is:

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

This number in front (?1428260681) is the Unix-time of the last modification of style.css, when editing it a server-side language (or something equivalent) for example 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 it like this:

<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 change the number will be different.

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 cached.

Note that this is a basic example technique, there are many others, for this reason not always the suffixes used will 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.