Yii2 service worker for offline cache PWA

Is there a way to implement a service worker to make your application working offline as described Here at developers.google.com on assets like this:

        namespace app\assets;

        use yii\web\AssetBundle;
        use yii\web\View;

        class AppAsset extends AssetBundle {

          public $sourcePath = '@app/media';

          public $baseUrl = '@app';

          public $js
            = [
              'js/sw.js', //serviceWorker for pwa
              'fonts/fontawesome/js/all.min.js',
              'js/bootstrap-input-spinner.js',
              'js/bootstrap-input-spinner_object.js',
            ];

          public $css
            = [
              'css/styles.css',
            ];

          public $depends
            = [
              'yii\web\JqueryAsset',
              'yii\bootstrap4\BootstrapAsset',
              'yii\bootstrap4\BootstrapPluginAsset',
            ];

          public $jsOptions
            = [
              'position' => View::POS_END,
            ];
        }

Hello Muitosefala,

When it comes to Service Worker, it’s more related to client side scripts i.e JS powered!. The way how you design matters here.

You can use the browsers “IndexedDB” to store the user data temporarily when they’re in offline once they connected to internet just save to your server and flush the local IndexedDB data!. These all should be done through JS and must be registered during service worker registration.

For more info about IndexedDB you can refer here : https://developers.google.com/web/ilt/pwa/working-with-indexeddb

Thanks for your answer!