# Random Number Appended to Images,JS and CSS files

**URL:** <https://www.ruby-forum.com/t/random-number-appended-to-images-js-and-css-files/68934>\
**Category:** Rails\
**Created:** [September 17, 2006, 1:35am UTC](https://www.ruby-forum.com/t/random-number-appended-to-images-js-and-css-files/68934 "2006-09-17T01:35:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [September 17, 2006, 1:35am UTC](https://www.ruby-forum.com/t/random-number-appended-to-images-js-and-css-files/68934/1 "2006-09-17T01:35:12Z")

</div>

I am really curious… what is up with the random number being  
appended to the query string of calls to JS, CSS and Image files? At  
first I thought it was just for development mode so that files would  
not be cached but when I went to my site in production mode the random  
number where appended as well. Does anyone know the design decision  
behind this? Or if the number really is random?

Is their a function to get that random number for when I create my own  
helpers?

Thanks for your insite.

Your Friend,

John K.

–  
John K.  
removed\_email\_address@domain.invalid

[http://www.kopanas.com](http://www.kopanas.com)

> **[Canadian University Software Engineering Conference - CUSEC](http://cusec.net/)**
>
> Canadian University Software Engineering Conference is a yearly conference created by students for students.

[http://www.soen.info](http://www.soen.info)

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [September 17, 2006, 2:33am UTC](https://www.ruby-forum.com/t/random-number-appended-to-images-js-and-css-files/68934/2 "2006-09-17T02:33:16Z")

</div>

> I am really curious… what is up with the random number being  
> appended to the query string of calls to JS, CSS and Image files? At  
> first I thought it was just for development mode so that files would  
> not be cached but when I went to my site in production mode the random  
> number where appended as well. Does anyone know the design decision  
> behind this? Or if the number really is random?
> 
> Is their a function to get that random number for when I create my own helpers?

Quote from  
[http://api.rubyonrails.com/files/vendor/rails/actionpack/CHANGELOG.html](http://api.rubyonrails.com/files/vendor/rails/actionpack/CHANGELOG.html)  
(_1.12.1_ (April 6th, 2006)):

Added automated timestamping to AssetTagHelper methods for  
stylesheets, javascripts, and images when Action Controller is run  
under Rails [DHH]. Example:

image\_tag(“rails.png”) # =\> ‘ ![Rails](https://www.ruby-forum.com/images/rails.png?1143664135)’

â?¦to avoid frequent stats (not a problem for most people), you can set  
RAILS\_ASSET\_ID in the ENV to avoid stats:

ENV[“RAILS\_ASSET\_ID”] = “2345”  
image\_tag(“rails.png”) # =\> ‘ ![Rails](https://www.ruby-forum.com/images/rails.png?2345)’

This can be used by deployment managers to set the asset id by  
application revision

=== end of quote ===

## Regards, Rimantas

[http://rimantas.com/](http://rimantas.com/)

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [September 17, 2006, 4:11am UTC](https://www.ruby-forum.com/t/random-number-appended-to-images-js-and-css-files/68934/3 "2006-09-17T04:11:19Z")

</div>

On Sep 16, 2006, at 6:54 PM, John K. wrote:

> Thanks Rimantas… I appreciate it. Stilld does not explain why  
> though. I am a big why type of guy. Anyone know why?

```
The reason for the timestamps on assets is to deal with browser

```

caching. Browsers tend to cache css and js for a while when they have  
the same name and url. This just forces the browser to reload the css  
or js or image when you make changes to it. This way your users don’t  
get stale assets that don’t match the html like they should. Its  
especially helpful in dev mode when often you are making frequent  
changes to the css and js and you want them to reload in the browser  
without the need for you to flush your browsers cache.

-Ezra

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [September 17, 2006, 4:11am UTC](https://www.ruby-forum.com/t/random-number-appended-to-images-js-and-css-files/68934/4 "2006-09-17T04:11:31Z")

</div>

John K. wrote:

> Thanks Rimantas… I appreciate it. Stilld does not explain why  
> though. I am a big why type of guy. Anyone know why?

I know one helpful thing that results from this, it tends to force the  
browser to download a new copy of whatever is being downloaded.

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [September 17, 2006, 3:59am UTC](https://www.ruby-forum.com/t/random-number-appended-to-images-js-and-css-files/68934/5 "2006-09-17T03:59:19Z")

</div>

Thanks Rimantas… I appreciate it. Stilld does not explain why  
though. I am a big why type of guy. Anyone know why?

On 9/16/06, Rimantas L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> (_1.12.1_ (April 6th, 2006)):
> 
> –  
> [http://rimantas.com/](http://rimantas.com/)
> 
> >

–  
John K.  
removed\_email\_address@domain.invalid

[http://www.kopanas.com](http://www.kopanas.com)

> **[Canadian University Software Engineering Conference - CUSEC](http://cusec.net/)**
>
> Canadian University Software Engineering Conference is a yearly conference created by students for students.

[http://www.soen.info](http://www.soen.info)

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [September 17, 2006, 5:29am UTC](https://www.ruby-forum.com/t/random-number-appended-to-images-js-and-css-files/68934/6 "2006-09-17T05:29:54Z")

</div>

John K. wrote:

> I understand this is very helpful in dev mode but is this a good thing  
> in production mode? Downloading the CSS file every single time the  
> user accesses a page of yours slows things down doesn’t it. Isn’t one  
> of the benefits of putting CSS in a seperate file the fact that it can  
> be cached?
> 
> Do people tend to turn this off for production?

I thought it was appended with the date the file was last saved. I could  
definitely be wrong though.

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [September 17, 2006, 5:27am UTC](https://www.ruby-forum.com/t/random-number-appended-to-images-js-and-css-files/68934/7 "2006-09-17T05:27:24Z")

</div>

I understand this is very helpful in dev mode but is this a good thing  
in production mode? Downloading the CSS file every single time the  
user accesses a page of yours slows things down doesn’t it. Isn’t one  
of the benefits of putting CSS in a seperate file the fact that it can  
be cached?

Do people tend to turn this off for production?

On 9/16/06, Shane P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
John K.  
removed\_email\_address@domain.invalid

[http://www.kopanas.com](http://www.kopanas.com)

> **[Canadian University Software Engineering Conference - CUSEC](http://cusec.net/)**
>
> Canadian University Software Engineering Conference is a yearly conference created by students for students.

[http://www.soen.info](http://www.soen.info)

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [September 17, 2006, 6:05am UTC](https://www.ruby-forum.com/t/random-number-appended-to-images-js-and-css-files/68934/8 "2006-09-17T06:05:26Z")

</div>

You know what… you might be right. Refreshing the page does not get  
me a new timestamp.

On 9/16/06, Shane P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I thought it was appended with the date the file was last saved. I could  
> definitely be wrong though.
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
John K.  
removed\_email\_address@domain.invalid

[http://www.kopanas.com](http://www.kopanas.com)

> **[Canadian University Software Engineering Conference - CUSEC](http://cusec.net/)**
>
> Canadian University Software Engineering Conference is a yearly conference created by students for students.

[http://www.soen.info](http://www.soen.info)
