How To Force A Client To Reload The Js And Css Files?

I was wondering how to force a client to download Javascript and CSS files in Yii. I have seen that some achieve this by referring to the files by a fake field like http://website.com/r1/style.css where r1 is just something ignored at the server level (through .htaccess for example) but the browser is unaware and would therefore download it again. For instance if you made changes to js or css files you would change r1 to r2.

Does Yii have any solutions or work arounds like this?

The first step is to check which headers your Web server is sending alongside the files.

You will look for cache directives that instructs the browsers to cache the file for too long (wanted in production, but not in development).

You could use Etags or Last-Modified instead during the development.

Once the browser has it cached already, a solution that is similar to the one you listed, but simpler, is appending a query string to the url. For example: http://example.com/style.css?v2

Of course,

I forgot about the style.css?v1 method.

Thank you.

You can also search and read about assets in Yii. It’s quite convenient and you canada use them with any file ( js, css, images, fonts…)

Generally if an asset folder is modified, Yii will publish it again with a new id.

http://www.yiiframework.com/wiki/148/understanding-assets/

As far as I know a common solution is to add a ?<version> to the script src link.

For instance:

<script type="text/javascript" src="myfile.js?1500"></script>

I assume at this point that there isn’t a better way than find-replace to increment these “version numbers” in all of the script tags?

You might have a version control system do that for you? Most version control systems have a way to automatically inject the revision number on check-in for instance.

It would look something like this:

<script type="text/javascript" src="myfile.js?$$REVISION$$"></script>

Of course, there are always better solutions like this one.

http header for cache control should work fine

I use special folders, my css and js are in a folder named c:


c/js

c/css

c/img

I have a bash script that at each update create a new symlink to this folder with the hg version:


c125=>c

And a simple function create all the link correctly.