Search code examples
phpjqueryviewyii2assetbundle

Yii2: registerJs() implicitly includes JqueryAsset, how to avoid


In Yii2 there is a $this->registerJs() method available in views for registering inline JS. I use it as described in documentation:

$js = "...prepare js code...";
$this->registerJs($js, View::POS_READY);

The problem is that with View::POS_READY (which is the default for second argument) registerJs() implicitly requires yii\web\JqueryAsset (source code).

But I already included Jquery in my own asset bundle - it is all-min.js, there are Jquery + plugins minified and concatenated in a single file. Yii2 includes JqueryAsset, therefore duplicating Jquery on a resulting page.

How do I tell Yii2 that Jquery is already included or avoid this duplication in some other way?


Solution

  • You can easily customize jquery asset bundle by configuring assetManager in the application components configuration (usually config/web.php).

    You can disable one or multiple asset bundles by associating false with the names of the asset bundles that you want to disable :

        'assetManager' => [
            'bundles' => [
                'yii\web\JqueryAsset' => false,
            ],
        ],
    

    Read more : http://www.yiiframework.com/doc-2.0/guide-structure-assets.html#customizing-asset-bundles