Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make the browser see CSS and Javascript changes?

CSS and Javascript files don't change very often, so I want them to be cached by the web browser. But I also want the web browser to see changes made to these files without requiring the user to clear their browser cache. Also want a solution that works well with a version control system such as Subversion.


Some solutions I have seen involve adding a version number to the end of the file in the form of a query string.

Could use the SVN revision number to automate this for you: ASP.NET Display SVN Revision Number

Can you specify how you include the Revision variable of another file? That is in the HTML file I can include the Revision number in the URL to the CSS or Javascript file.

In the Subversion book it says about Revision: "This keyword describes the last known revision in which this file changed in the repository".

Firefox also allows pressing CTRL+R to reload everything on a particular page.

To clarify I am looking for solutions that don't require the user to do anything on their part.

like image 553
grom Avatar asked Aug 06 '08 09:08

grom


People also ask

How do I force the browser to reload cached CSS and JavaScript files?

But, if you're not using it, and you just need to reload that one CSS or JS file occasionally in your own browser... just open it in its own tab and hit SHIFT-reload (or CTRL-F5)!

How do I force a browser to load new CSS?

Anytime you make changes to CSS, JavaScript and are viewing the page you've updated - you will see this concern. You can force a refresh by pressing CTRL+F5 on the browser and that will get the latest version.


2 Answers

I found that if you append the last modified timestamp of the file onto the end of the URL the browser will request the files when it is modified. For example in PHP:

function urlmtime($url) {    $parsed_url = parse_url($url);    $path = $parsed_url['path'];     if ($path[0] == "/") {        $filename = $_SERVER['DOCUMENT_ROOT'] . "/" . $path;    } else {        $filename = $path;    }     if (!file_exists($filename)) {        // If not a file then use the current time        $lastModified = date('YmdHis');    } else {        $lastModified = date('YmdHis', filemtime($filename));    }     if (strpos($url, '?') === false) {        $url .= '?ts=' . $lastModified;    } else {        $url .= '&ts=' . $lastModified;    }     return $url; }  function include_css($css_url, $media='all') {    // According to Yahoo, using link allows for progressive     // rendering in IE where as @import url($css_url) does not    echo '<link rel="stylesheet" type="text/css" media="' .         $media . '" href="' . urlmtime($css_url) . '">'."\n"; }  function include_javascript($javascript_url) {    echo '<script type="text/javascript" src="' . urlmtime($javascript_url) .         '"></script>'."\n"; } 
like image 91
grom Avatar answered Oct 09 '22 15:10

grom


Some solutions I have seen involve adding a version number to the end of the file in the form of a query string.

<script type="text/javascript" src="funkycode.js?v1"> 

You could use the SVN revision number to automate this for you by including the word LastChangedRevision in your html file after where v1 appears above. You must also setup your repository to do this.

I hope this further clarifies my answer?

Firefox also allows pressing CTRL + R to reload everything on a particular page.

like image 27
GateKiller Avatar answered Oct 09 '22 17:10

GateKiller