Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/StaticCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use craft\helpers\ElementHelper;
use craft\helpers\StringHelper;
use craft\services\Elements;
use craft\services\Gql;
use craft\utilities\ClearCaches;
use craft\web\UrlManager;
use craft\web\View;
Expand Down Expand Up @@ -62,6 +63,7 @@ class StaticCache extends \yii\base\Component
private Collection $tagsToPurge;
private Collection $fetchUrls;
private bool $collectingCacheInfo = false;
private ?bool $graphqlCaching = null;
Comment thread
timkelty marked this conversation as resolved.

public function init(): void
{
Expand All @@ -78,6 +80,18 @@ public function registerEventHandlers(): void
fn(Event $event) => $this->handleInitWebApplication($event),
);

Event::on(
Gql::class,
Gql::EVENT_BEFORE_EXECUTE_GQL_QUERY,
fn(Event $event) => $this->handleBeforeExecuteGqlQuery($event),
);

Event::on(
Gql::class,
Gql::EVENT_AFTER_EXECUTE_GQL_QUERY,
fn(Event $event) => $this->handleAfterExecuteGqlQuery($event),
);
Comment thread
timkelty marked this conversation as resolved.

Event::on(
View::class,
View::EVENT_BEFORE_RENDER_PAGE_TEMPLATE,
Expand Down Expand Up @@ -164,6 +178,24 @@ private function handleAfterPrepareWebResponse(Event $event): void
$this->addCacheHeadersToWebResponse();
}

private function handleBeforeExecuteGqlQuery(Event $event): void
{
if ($this->collectingCacheInfo) {
// TODO: Remove after https://github.com/craftcms/cms/pull/19508 reaches Cloud's minimum supported Craft version.
$generalConfig = Craft::$app->getConfig()->getGeneral();
$this->graphqlCaching = $generalConfig->enableGraphqlCaching;
Comment thread
timkelty marked this conversation as resolved.
$generalConfig->enableGraphqlCaching = false;
}
}

private function handleAfterExecuteGqlQuery(Event $event): void
{
if ($this->graphqlCaching !== null) {
Craft::$app->getConfig()->getGeneral()->enableGraphqlCaching = $this->graphqlCaching;
$this->graphqlCaching = null;
}
Comment thread
timkelty marked this conversation as resolved.
}

private function handleBeforeRenderPageTemplate(TemplateEvent $event): void
{
/** @var UrlManager $urlManager */
Expand Down
35 changes: 35 additions & 0 deletions tests/unit/StaticCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,29 @@ public function testPostResponsesAreNotCacheable(): void
$this->assertFalse($this->isCacheable($staticCache));
}

public function testGraphqlCachingIsOnlyDisabledWhileCollectingCacheInfo(): void
{
$staticCache = new StaticCache();
$generalConfig = Craft::$app->getConfig()->getGeneral();
$enableGraphqlCaching = $generalConfig->enableGraphqlCaching;
Comment thread
timkelty marked this conversation as resolved.

try {
$generalConfig->enableGraphqlCaching = true;
$this->handleBeforeExecuteGqlQuery($staticCache);
$this->assertTrue($generalConfig->enableGraphqlCaching);

$collectingCacheInfo = new ReflectionProperty($staticCache, 'collectingCacheInfo');
$collectingCacheInfo->setValue($staticCache, true);
Comment thread
timkelty marked this conversation as resolved.
$this->handleBeforeExecuteGqlQuery($staticCache);
$this->assertFalse($generalConfig->enableGraphqlCaching);

$this->handleAfterExecuteGqlQuery($staticCache);
$this->assertTrue($generalConfig->enableGraphqlCaching);
} finally {
$generalConfig->enableGraphqlCaching = $enableGraphqlCaching;
}
}

public function testStaticCacheDirectivesPreferCdnCacheControl(): void
{
$staticCache = new StaticCache();
Expand Down Expand Up @@ -602,6 +625,18 @@ private function staticCacheDirectives(StaticCache $staticCache): Collection
return $method->invoke($staticCache);
}

private function handleBeforeExecuteGqlQuery(StaticCache $staticCache): void
{
$method = new ReflectionMethod($staticCache, 'handleBeforeExecuteGqlQuery');
$method->invoke($staticCache, new \yii\base\Event());
}
Comment thread
timkelty marked this conversation as resolved.

private function handleAfterExecuteGqlQuery(StaticCache $staticCache): void
{
$method = new ReflectionMethod($staticCache, 'handleAfterExecuteGqlQuery');
$method->invoke($staticCache, new \yii\base\Event());
}

private function addCacheHeadersToWebResponse(StaticCache $staticCache): void
{
$method = new ReflectionMethod($staticCache, 'addCacheHeadersToWebResponse');
Expand Down
Loading