Closed d--j closed 10 months ago
Every site requests emits a warning:
2023-12-23 23:09:02 [web.WARNING] [craft\elements\db\ElementQuery::prepare] Element query executed before Craft is fully initialized. Stack trace: #0 /var/www/html/vendor/yiisoft/yii2/db/QueryBuilder.php(227): craft\elements\db\ElementQuery->prepare() #1 /var/www/html/vendor/yiisoft/yii2/db/Query.php(157): yii\db\QueryBuilder->build() #2 /var/www/html/vendor/yiisoft/yii2/db/Query.php(287): yii\db\Query->createCommand() #3 /var/www/html/vendor/craftcms/cms/src/db/Query.php(275): yii\db\Query->one() #4 /var/www/html/vendor/craftcms/cms/src/elements/db/ElementQuery.php(1604): craft\db\Query->one() #5 /var/www/html/vendor/craftcms/cms/src/elements/User.php(545): craft\elements\db\ElementQuery->one() #6 /var/www/html/vendor/yiisoft/yii2/web/User.php(698): craft\elements\User::findIdentity() #7 /var/www/html/vendor/craftcms/cms/src/web/User.php(500): yii\web\User->renewAuthStatus() #8 /var/www/html/vendor/yiisoft/yii2/web/User.php(199): craft\web\User->renewAuthStatus() #9 /var/www/html/vendor/cloudgrayau/csp/src/Csp.php(84): yii\web\User->getIdentity() #10 /var/www/html/vendor/cloudgrayau/csp/src/Csp.php(47): cloudgrayau\csp\Csp->_registerCSP() #11 /var/www/html/vendor/yiisoft/yii2/base/BaseObject.php(109): cloudgrayau\csp\Csp->init() #12 /var/www/html/vendor/yiisoft/yii2/base/Module.php(161): yii\base\BaseObject->__construct() #13 /var/www/html/vendor/craftcms/cms/src/base/Plugin.php(122): yii\base\Module->__construct() #14 craft\base\Plugin->__construct() #15 /var/www/html/vendor/yiisoft/yii2/di/Container.php(419): ReflectionClass->newInstanceArgs() #16 /var/www/html/vendor/yiisoft/yii2/di/Container.php(170): yii\di\Container->build() #17 /var/www/html/vendor/yiisoft/yii2/BaseYii.php(365): yii\di\Container->get() #18 /var/www/html/vendor/craftcms/cms/src/Craft.php(59): yii\BaseYii::createObject() #19 /var/www/html/vendor/craftcms/cms/src/services/Plugins.php(943): Craft::createObject() #20 /var/www/html/vendor/craftcms/cms/src/services/Plugins.php(228): craft\services\Plugins->createPlugin() #21 /var/www/html/vendor/craftcms/cms/src/base/ApplicationTrait.php(1640): craft\services\Plugins->loadPlugins() #22 /var/www/html/vendor/craftcms/cms/src/web/Application.php(109): craft\web\Application->_postInit() #23 /var/www/html/vendor/yiisoft/yii2/base/BaseObject.php(109): craft\web\Application->init() #24 /var/www/html/vendor/yiisoft/yii2/base/Application.php(204): yii\base\BaseObject->__construct() #25 yii\base\Application->__construct() #26 /var/www/html/vendor/yiisoft/yii2/di/Container.php(419): ReflectionClass->newInstanceArgs() #27 /var/www/html/vendor/yiisoft/yii2/di/Container.php(170): yii\di\Container->build() #28 /var/www/html/vendor/yiisoft/yii2/BaseYii.php(365): yii\di\Container->get() #29 /var/www/html/vendor/craftcms/cms/src/Craft.php(59): yii\BaseYii::createObject() #30 /var/www/html/vendor/craftcms/cms/bootstrap/bootstrap.php(250): Craft::createObject() #31 /var/www/html/vendor/craftcms/cms/bootstrap/web.php(40): require() #32 /var/www/html/web/index.php(11): require() {"memory":2408104}
This is because of
https://github.com/cloudgrayau/csp/blob/fa3bee964ece2ddb4b0e27aabcfde24eb9bbaeaa/src/Csp.php#L84-L92
The call to Craft::$app->getUser()->getIdentity() executes an element query. You should only do that after Craft was fully initialised (and all plugins were loaded).
Craft::$app->getUser()->getIdentity()
You can easily defer the check with
Craft::$app->onInit(function() { if (Craft::$app->getRequest()->getIsSiteRequest()) { $this->_registerCSP(); } elseif (Craft::$app->getRequest()->getIsCpRequest()) { $this->_registerCpUrlRules(); } });
The onInit method was introduced in Craft 4.3.5 so you might want to increase the minimum CraftCMS version for this change. Otherwise use this event.
onInit
The warning was introduced in CraftCMS 4.0.0-RC2 (see https://github.com/craftcms/cms/commit/50f6d35d4d8297d8f7e961f498d8301805c8a06f )
Thanks for reporting. This is now fixed with 1.0.5.
Thanks for the quick fix 🎄
Every site requests emits a warning:
This is because of
https://github.com/cloudgrayau/csp/blob/fa3bee964ece2ddb4b0e27aabcfde24eb9bbaeaa/src/Csp.php#L84-L92
The call to
Craft::$app->getUser()->getIdentity()
executes an element query. You should only do that after Craft was fully initialised (and all plugins were loaded).You can easily defer the check with
The
onInit
method was introduced in Craft 4.3.5 so you might want to increase the minimum CraftCMS version for this change. Otherwise use this event.The warning was introduced in CraftCMS 4.0.0-RC2 (see https://github.com/craftcms/cms/commit/50f6d35d4d8297d8f7e961f498d8301805c8a06f )