Enh: Added option to disable PWA/ServiceWorker Support (#4796)

This commit is contained in:
Lucas Bartholemy 2021-01-25 15:17:45 +01:00 committed by GitHub
parent a337048f0b
commit 9f1885dbe1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 6 deletions

View File

@ -34,3 +34,4 @@
- Enh #4416: Added replay to sub comments - Enh #4416: Added replay to sub comments
- Enh #4571: humhub/libs/Html::containerLink() now adds a "data-guid" attribute - Enh #4571: humhub/libs/Html::containerLink() now adds a "data-guid" attribute
- Enh #4787: Always enable Space Membership Web Notifications - Enh #4787: Always enable Space Membership Web Notifications
- Enh #4796: Added option to disable PWA/ServiceWorker support

View File

@ -21,7 +21,6 @@ use humhub\modules\web\pwa\controllers\ServiceWorkerController;
*/ */
class Module extends \humhub\components\Module class Module extends \humhub\components\Module
{ {
/** /**
* @inheritdoc * @inheritdoc
*/ */
@ -32,6 +31,12 @@ class Module extends \humhub\components\Module
*/ */
public $security; public $security;
/**
* @since 1.8
* @var boolean Disable Service Worker and PWA Support
*/
public $enableServiceWorker = true;
/** /**
* @inheritdoc * @inheritdoc
*/ */

View File

@ -41,7 +41,12 @@ class ManifestController extends Controller
public function actionIndex() public function actionIndex()
{ {
$this->handleIcons(); $this->handleIcons();
$this->handlePwa();
/** @var Module $module */
$module = Yii::$app->getModule('web');
if ($module->enableServiceWorker !== false) {
$this->handlePwa();
}
return $this->asJson($this->manifest); return $this->asJson($this->manifest);
} }

View File

@ -12,7 +12,7 @@ use Yii;
use yii\base\WidgetEvent; use yii\base\WidgetEvent;
use yii\helpers\Url; use yii\helpers\Url;
use humhub\components\Widget; use humhub\components\Widget;
use humhub\modules\ui\Module; use humhub\modules\web\Module;
use humhub\modules\ui\view\components\View; use humhub\modules\ui\view\components\View;
/** /**
@ -30,6 +30,7 @@ class LayoutHeader extends Widget
*/ */
public static function registerHeadTags(View $view) public static function registerHeadTags(View $view)
{ {
$view->registerMetaTag(['name' => 'theme-color', 'content' => Yii::$app->view->theme->variable('primary')]); $view->registerMetaTag(['name' => 'theme-color', 'content' => Yii::$app->view->theme->variable('primary')]);
$view->registerMetaTag(['name' => 'application-name', 'content' => Yii::$app->name]); $view->registerMetaTag(['name' => 'application-name', 'content' => Yii::$app->name]);
@ -41,25 +42,33 @@ class LayoutHeader extends Widget
$view->registerLinkTag(['rel' => 'manifest', 'href' => Url::to(['/web/pwa-manifest/index'])]); $view->registerLinkTag(['rel' => 'manifest', 'href' => Url::to(['/web/pwa-manifest/index'])]);
/** @var Module $module */
$module = Yii::$app->getModule('web');
if ($module->enableServiceWorker !== false) {
static::registerServiceWorker($view);
}
}
private static function registerServiceWorker(View $view)
{
$cacheId = Yii::$app->cache->getOrSet('service-worker-cache-id', function () { $cacheId = Yii::$app->cache->getOrSet('service-worker-cache-id', function () {
return time(); return time();
}); });
$serviceWorkUrl = Url::to(['/web/pwa-service-worker/index', 'v' => $cacheId]); $serviceWorkUrl = Url::to(['/web/pwa-service-worker/index', 'v' => $cacheId]);
$rootPath = Yii::getAlias('@web') . '/'; $rootPath = Yii::getAlias('@web') . '/';
$view->registerJs(<<<JS $view->registerJs(<<<JS
if ('serviceWorker' in navigator) { if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('$serviceWorkUrl', { scope: '$rootPath' }) navigator.serviceWorker.register('$serviceWorkUrl', { scope: '$rootPath' })
.then(function (registration) { .then(function (registration) {
if (typeof afterServiceWorkerRegistration === "function") { if (typeof afterServiceWorkerRegistration === "function") {
afterServiceWorkerRegistration(registration); afterServiceWorkerRegistration(registration);
} }
}) })
} }
JS JS
, View::POS_READY, 'serviceWorkerInit'); , View::POS_READY, 'serviceWorkerInit');
} }
} }