samdark / yii2-cookbook

Yii 2.0 Community Cookbook
1.45k stars 296 forks source link

[WIP] Asset managements in scaled environment #116 #117

Closed petrabarus closed 8 years ago

petrabarus commented 8 years ago

@samdark It seems that I overdo the asset management part. It's more complex than the previous sections.

samdark commented 8 years ago

Is it ready info-wise? I can simplify the wording.

petrabarus commented 8 years ago

Yes I think it's ready. In short there are two simple steps:

  1. Use hashCallback that doesn't depend on time.
  2. Use asset combining.

Yeah,,, I really overdid it. LOL. If you want to simplify the wording, I'd be glad. But I think I need to cover why the steps in this article is too simple and what things it overlooked.

badams commented 8 years ago

@petrabarus Nice write up. I think the wording is fine tbh as this is somewhat of a complex topic, I'll hopefully get around to contributing somethings to it too.

Couple of things I wanted to bring up regarding the asset management. First, we probably want to give further examples, rather than just showing an closure you could show something along the lines of this

$config = [
    'components' => [
       'assetManager' => [
           'hashCallback' => function ($path) {
               return hash('md4', $path);
            }    
       ]
    ]
]; 

The other thing is regarding the cache-busting technique of including Yii::$app->version implies that the user will need to update this version property on each change (or have it updated by the deployment process potentially), I think a better solution is to use the $appendTimestamp property of the AssetManager class like so.

$config = [
    'components' => [
       'assetManager' => [
           'appendTimestamp' => true,
           'hashCallback' => function ($path) {
               return hash('md4', $path);
            }    
       ]
    ]
];  

This will result in asset URLs like this /assets/2445dd2999f135ab39ac4fa59f522464/css/global.min.css?v=1464130501

petrabarus commented 8 years ago

@badams

Yes the $appendTimestamp can work! I forgot that it exists. It won't work well in more complex situation as in mine. But in this case it's good enough. Thanks!

iadj commented 8 years ago

Awesome write up! It's indeed a complex subject that deserves some context.

Another possible (simple) solution might be storing the asset files in the cloud i.e. S3 or Google Cloud Storage, and then defining a path alias like @cloud to the base storage URL. Would that be efficient?

petrabarus commented 8 years ago

Storing asset in S3 or Cloud Storage is a very good solution, but it's more efficient if you do it using build server instead in production server. I think we should also cover about continuous integration. I've started a WIP #118. Let me know if you have any ideas.

petrabarus commented 8 years ago

@badams Updated.

Although I just realized that if we don't set forceCopy in the asset manager, updated asset won't be published because there is already existing old copy.

samdark commented 8 years ago

Merged. Thank you very much!

If you have further changes, please open more pull requests.