Skip to content

Commit

Permalink
fix: Fixed an issue where the MetaJsonLd container was not properly…
Browse files Browse the repository at this point in the history
… cached, which caused a performance issue as well as storing more data in the cache than necessary
  • Loading branch information
khalwat committed Jul 18, 2023
1 parent 1003e37 commit 43565e6
Showing 1 changed file with 36 additions and 42 deletions.
78 changes: 36 additions & 42 deletions src/models/MetaJsonLdContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@

namespace nystudio107\seomatic\models;

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

use Craft;

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

Expand Down Expand Up @@ -52,16 +50,16 @@ class MetaJsonLdContainer extends NonceContainer
public function includeMetaData($dependency)
{
Craft::beginProfile('MetaJsonLdContainer::includeMetaData', __METHOD__);
$uniqueKey = $this->handle.$dependency->tags[3];
$uniqueKey = $this->handle . $dependency->tags[3] . '-v2';
$cache = Craft::$app->getCache();
if ($this->clearCache) {
TagDependency::invalidate($cache, $dependency->tags[3]);
}
$tagData = $cache->getOrSet(
self::CONTAINER_TYPE.$uniqueKey,
[$jsonLd, $attrs] = $cache->getOrSet(
self::CONTAINER_TYPE . $uniqueKey,
function () use ($uniqueKey) {
Craft::info(
self::CONTAINER_TYPE.' cache miss: '.$uniqueKey,
self::CONTAINER_TYPE . ' cache miss: ' . $uniqueKey,
__METHOD__
);
$tagData = [];
Expand All @@ -71,11 +69,6 @@ function () use ($uniqueKey) {
if ($metaJsonLdModel->include) {
$options = $metaJsonLdModel->tagAttributes();
if ($metaJsonLdModel->prepForRender($options)) {
$jsonLd = $metaJsonLdModel->render([
'renderRaw' => true,
'renderScriptTags' => false,
'array' => false,
]);
$tagData[] = [
'jsonLd' => $metaJsonLdModel,
'position' => View::POS_END
Expand All @@ -86,7 +79,7 @@ function () use ($uniqueKey) {
'JSON-LD property: ',
[
'default' => 'error',
'google' => 'warning',
'google' => 'warning',
]
);
}
Expand All @@ -95,7 +88,35 @@ function () use ($uniqueKey) {
}
}

return $tagData;
// Create a root JSON-LD object
$jsonLdGraph = MetaJsonLd::create('jsonLd', [
'graph' => [],
]);
$jsonLdGraph->type = null;
// Add the JSON-LD objects to our root JSON-LD's graph
$cspNonce = null;
foreach ($tagData as $config) {
$jsonLdGraph->graph[] = $config['jsonLd'];
if (!empty($config['jsonLd']->nonce)) {
$cspNonce = $config['jsonLd']->nonce;
}
}
// Render the JSON-LD object
$jsonLd = $jsonLdGraph->render([
'renderRaw' => true,
'renderScriptTags' => false,
'array' => false,
]);

// Register the tags
$attrs = ['type' => 'application/ld+json'];
if (!empty($cspNonce)) {
$attrs = array_merge($attrs, [
'nonce' => $cspNonce,
]);
}

return [$jsonLd, $attrs];
},
Seomatic::$cacheDuration,
$dependency
Expand All @@ -104,33 +125,6 @@ function () use ($uniqueKey) {
if (ImageTransformHelper::$pendingImageTransforms) {
TagDependency::invalidate($cache, $dependency->tags[3]);
}
// Create a root JSON-LD object
$jsonLdGraph = MetaJsonLd::create('jsonLd', [
'graph' => [],
]);
$jsonLdGraph->type = null;
// Add the JSON-LD objects to our root JSON-LD's graph
$cspNonce = null;
foreach ($tagData as $config) {
$jsonLdGraph->graph[] = $config['jsonLd'];
if (!empty($config['jsonLd']->nonce)) {
$cspNonce = $config['jsonLd']->nonce;
}
}
// Render the JSON-LD object
$jsonLd = $jsonLdGraph->render([
'renderRaw' => true,
'renderScriptTags' => false,
'array' => false,
]);

// Register the tags
$attrs = ['type' => 'application/ld+json'];
if (!empty($cspNonce)) {
$attrs = array_merge($attrs, [
'nonce' => $cspNonce,
]);
}
Seomatic::$view->registerScript(
$jsonLd,
View::POS_END,
Expand Down

0 comments on commit 43565e6

Please sign in to comment.