Skip to content

Commit

Permalink
fix: Fixed an issue that caused you to be unable to dynamically inclu…
Browse files Browse the repository at this point in the history
…de/exclude scripts that have body JavaScript via Twig, by unifying the rendering method ([#1334](#1334))
  • Loading branch information
khalwat committed Jun 20, 2023
1 parent c90bff1 commit eae2cc4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 90 deletions.
31 changes: 0 additions & 31 deletions src/Seomatic.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@
use nystudio107\seomatic\variables\SeomaticVariable;
use yii\base\Event;
use yii\base\View as BaseView;
use yii\web\View as YiiView;

/** @noinspection MissingPropertyAnnotationsInspection */

Expand Down Expand Up @@ -703,36 +702,6 @@ static function (DefineGqlTypeFieldsEvent $event) use ($knownInterfaceNames) {
*/
protected function handleSiteRequest(): void
{
// Handler: View::EVENT_BEGIN_BODY
Event::on(
View::class,
YiiView::EVENT_BEGIN_BODY,
static function () {
Craft::debug(
'View::EVENT_BEGIN_BODY',
__METHOD__
);
// The <body> placeholder tag has just rendered, include any script HTML
if (self::$settings->renderEnabled && self::$seomaticVariable) {
self::$plugin->metaContainers->includeScriptBodyHtml(YiiView::POS_BEGIN);
}
}
);
// Handler: View::EVENT_END_BODY
Event::on(
View::class,
YiiView::EVENT_END_BODY,
static function () {
Craft::debug(
'View::EVENT_END_BODY',
__METHOD__
);
// The </body> placeholder tag is about to be rendered, include any script HTML
if (self::$settings->renderEnabled && self::$seomaticVariable) {
self::$plugin->metaContainers->includeScriptBodyHtml(YiiView::POS_END);
}
}
);
// Handler: View::EVENT_END_PAGE
Event::on(
View::class,
Expand Down
38 changes: 28 additions & 10 deletions src/models/MetaScriptContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@

namespace nystudio107\seomatic\models;

use nystudio107\seomatic\Seomatic;
use Craft;
use nystudio107\seomatic\base\NonceContainer;
use nystudio107\seomatic\helpers\ImageTransform as ImageTransformHelper;

use Craft;

use nystudio107\seomatic\Seomatic;
use yii\caching\TagDependency;
use yii\web\View;

Expand Down Expand Up @@ -56,23 +54,24 @@ class MetaScriptContainer extends NonceContainer
public function includeMetaData($dependency)
{
Craft::beginProfile('MetaScriptContainer::includeMetaData', __METHOD__);
$uniqueKey = $this->handle.$dependency->tags[3].$this->dataLayerHash();
$uniqueKey = $this->handle . $dependency->tags[3] . $this->dataLayerHash();
$cache = Craft::$app->getCache();
if ($this->clearCache) {
TagDependency::invalidate($cache, $dependency->tags[3]);
}
$tagData = $cache->getOrSet(
self::CONTAINER_TYPE.$uniqueKey,
self::CONTAINER_TYPE . $uniqueKey,
function () use ($uniqueKey) {
Craft::info(
self::CONTAINER_TYPE.' cache miss: '.$uniqueKey,
self::CONTAINER_TYPE . ' cache miss: ' . $uniqueKey,
__METHOD__
);
$tagData = [];
if ($this->prepForInclusion()) {
/** @var $metaScriptModel MetaScript */
foreach ($this->data as $metaScriptModel) {
if ($metaScriptModel->include) {
// The regular script JS
$js = $metaScriptModel->render();
if (!empty($js)) {
$scenario = $this->scenario;
Expand All @@ -92,6 +91,26 @@ function () use ($uniqueKey) {
);
}
}
// The regular script JS
$bodyJs = $metaScriptModel->renderBodyHtml();
if (!empty($bodyJs)) {
$scenario = $this->scenario;
$metaScriptModel->setScenario('render');
$options = $metaScriptModel->tagAttributes();
$metaScriptModel->setScenario($scenario);
$tagData[] = [
'js' => $bodyJs,
'position' => $metaScriptModel->bodyPosition ?? $this->position,
'nonce' => $metaScriptModel->nonce ?? null,
'tagAttrs' => $options,
];
// If `devMode` is enabled, validate the Meta Script and output any model errors
if (Seomatic::$devMode) {
$metaScriptModel->debugMetaItem(
'Script attribute: '
);
}
}
}
}
}
Expand Down Expand Up @@ -134,9 +153,8 @@ public function render(array $params = []): string
if ($params['renderScriptTags']) {
$html =
'<script>'
.$html
.'</script>'
;
. $html
. '</script>';
}

return $html;
Expand Down
49 changes: 0 additions & 49 deletions src/services/MetaContainers.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,55 +164,6 @@ public function init(): void
}
}

/**
* Include any script body HTML
*
* @param int $bodyPosition
*/
public function includeScriptBodyHtml(int $bodyPosition)
{
Craft::beginProfile('MetaContainers::includeScriptBodyHtml', __METHOD__);
$dependency = $this->containerDependency;
$uniqueKey = $dependency->tags[3] ?? self::GLOBALS_CACHE_KEY;
$uniqueKey .= $bodyPosition;
$scriptData = Craft::$app->getCache()->getOrSet(
self::GLOBALS_CACHE_KEY . $uniqueKey,
function () use ($uniqueKey, $bodyPosition) {
Craft::info(
self::SCRIPTS_CACHE_KEY . ' cache miss: ' . $uniqueKey,
__METHOD__
);
$scriptData = [];
$scriptContainers = $this->getContainersOfType(MetaScriptContainer::CONTAINER_TYPE);
foreach ($scriptContainers as $scriptContainer) {
/** @var MetaScriptContainer $scriptContainer */
if ($scriptContainer->include) {
if ($scriptContainer->prepForInclusion()) {
foreach ($scriptContainer->data as $metaScript) {
/** @var MetaScript $metaScript */
if (!empty($metaScript->bodyTemplatePath)
&& ((int)$metaScript->bodyPosition === $bodyPosition)) {
$scriptData[] = $metaScript->renderBodyHtml();
}
}
}
}
}

return $scriptData;
},
Seomatic::$cacheDuration,
$dependency
);
// Output the script HTML
foreach ($scriptData as $script) {
if (is_string($script) && !empty($script)) {
echo $script;
}
}
Craft::endProfile('MetaContainers::includeScriptBodyHtml', __METHOD__);
}

/**
* Return the containers of a specific type
*
Expand Down

0 comments on commit eae2cc4

Please sign in to comment.