[ask] Is there any downside in using forceCopy in AssetBundle

I’ve seen many example on using AssetBundle. When using AssetBundle for image uploaded from user, I’m using this AssetBundle.




<?php

namespace common\assets;


use yii\web\AssetBundle;


/**

 * Main frontend application asset bundle.

 */

class UploadAsset extends AssetBundle

{

    public $sourcePath = '@common/web';

    public $baseUrl    = '@web';       

}



And in view (gridview) I used this in my view




$image = \common\assets\UploadAsset::register($this);

Html::img($image->baseUrl.'/avatar/'.$model->avatar, ['width' => '70px', 'title' => $model->name, 'alt' => $model->name]);



When I posted first record, it works. But when I posted 2nd record it didn’t create new image in web/assets/ folder.

I know that we can use publishOptions with forceCopy set to true to force AssetManager create image to web/assets/, but I find example that we only use forceCopy in development only.




<?php

namespace common\assets;


use yii\web\AssetBundle;


/**

 * Main frontend application asset bundle.

 */

class UploadAsset extends AssetBundle

{

    public $sourcePath = '@common/web';

    public $baseUrl    = '@web';


    public $publishOptions = [

        'forceCopy'=>true,

    ];        

}



Is there any downside on using forceCopy in production mode?

Yes, it can lead to a huge performance hit because every image file will be copied on each request. There’s no need to use AssetBundle to publish user uploaded content. Simply save it to a web-accessible directory on upload.

Oh I see, it will be a problem if many user access images.

I’m using AssetBundle because image will be used in frontend and backend. Is it wise to upload it twice in frontend/web directory and backend/web directory?

If possible, I’d create a third subdomain, something like static.example.com, with its own webroot directory and save shared assets there.

Oh I never think about that :lol: . Thank you for your help.

Yes, it will be a good idea

Thanks,