Yii 1.1: timestampclientscript

Avoid navigator cache incoherence by including a timestamp in urls referencing local resources.
4 followers

Extends CClientScript to include a timestamp in references (url) to local files. This timestamp will avoid issues that arise when server files are updated while the navigator's cache still serves the old files.

The client cache will still work, but will be update due to timestamp that changes the URL.

In other words, when you update a js,css,png, ... file that is in a theme directory for instance, the end user will get the update regardless of the state of his local cache, and less dependent on the caching scheme of your hosting provider.

Usage

To use this extension, put the class file where you think it is appropriate, but found by Yii, and then update your configuration:

return array(
 [...]
 'components'=>array(
    [...]
                    'clientScript' => array(
                        'class'=>'TimestampClientScript',
                ),
    [...]
  )
  [...]
);

If you already use other CClientScript replacements, modify TimestampClientScript to extend from your other CClientScript replacement.

The extension also supports script files with parameters (since 1.1). Example:

Yii::app()->clientScript->registerScriptFile(
        Yii::app()->request->baseUrl . '/js/shared/js/html5Forms.js',
        CClientScript::POS_HEAD,
        array(
                'data-webforms2-support' => 'date,validation,number',
                'data-lang' => $lang,
                'defer' => 'defer'
        ));

Operation

This extension determines the local path for the Url and if the local path exists, it will add a time stamp (encoded to make the key shorter).

So your HTML will contain references to local css and js files like this:

<link rel="stylesheet" type="text/css" href="/themes/mytheme/css/main.css?n2kncy">

Total 4 comments

#18796 report it
le_top at 2015/01/10 05:53am
Update to cope with parameters

I just made an update as the initial version did not work when you add parameters to your script files.

Further, this also changes the encoding of the timestamp: to make the URL shorter the number is encoded.

#18199 report it
Rohit Suthar at 2014/09/25 04:29am
It's Great..

it's really nice script..

#17905 report it
le_top at 2014/08/08 06:39pm
Update to script

I improved the script:

  • timestamp added just before integration in the output (so no time spent on duplicates);
  • relative url handling corrected (=path with missing '/' at head);
  • '//' url handling improved;
  • core scripts are also timestamped.
#17853 report it
bandpay at 2014/07/31 05:11am
Very cooly done

Perfect :)

Leave a comment

Please to leave your comment.

Create extension