How to RegisteJS inside cache

I’m facing a problem caused by cache, I have a widget that is inside a fragment cache, that widget must register some assets, but it failed when cache is used.

Example code:

if ($this->beginCache($cacheID, ['variations' => [Yii::$app->language]])){

     /* Other stuff*/

     echo \frontend\widgets\HomeFeaturedEvent::widget(['currentView' => $this]); 

    /* Other stuff*/



How can I run dynamically the requested widget?

You can use asset bundlesif this JS file is used in all the pages.

Otherwise, you can separate the $this->registerJs file from the widget, where the fragment is cached.