tonydspaniard / Yii-extensions

My small contribution to the Yii framework community
124 stars 50 forks source link

Suggestion for client side caching #15

Closed mdeweerd closed 10 years ago

mdeweerd commented 11 years ago

Hi Not a bug, but a suggestion.

On site updates, the client's browser often keeps old script versions in cache. To avoid this, I now use the timestamp of a specific file in the runtime directory to adjust the hash. On site updates, I either remove or touch that file so that the asset paths change.

For that, I added the following to EAssetManagerBoost. The name of the file in the runtime directory is 'updateTimeReference'.

protected $refdate =null;

private function getRefFileDate() {
    if(empty($this->refdate)) {
        $name = Yii::app()->getRuntimePath() . '/updateTimeReference';
        if(!file_exists($name)) {
            fclose(fopen($name, 'a+b'));
        }
        $this->refdate= filemtime($name);
    }
    return $this->refdate."#";
}

protected function hash($path) {
    return parent::hash($this->getRefFileDate().$path);
}
tonydspaniard commented 11 years ago

@mdeweerd the extension has now its own repository: https://github.com/tonydspaniard/egmap

make a PR to my repo and will merge it...

Thanks!!