Skip to content

Commit

Permalink
Added support for callout-homepage revalidation
Browse files Browse the repository at this point in the history
Added support for callout-homepage revalidation
  • Loading branch information
ericdrosas87 committed Oct 22, 2024
1 parent ca7a9b4 commit 589f9f3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
30 changes: 21 additions & 9 deletions src/NextBuilds.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ class NextBuilds extends Plugin
*/
public string $schemaVersion = '1.0.0';

private string $homeUri = "__home__";

/**
* @var bool
*/
Expand Down Expand Up @@ -98,8 +100,14 @@ function(StatusChangeEvent $event) {
!($entry->duplicateOf && $entry->getIsCanonical() && !$entry->updatingFromDerivative) &&
!ElementHelper::rootElement($entry)->isProvisionalDraft &&
$newStatus == Entry::STATUS_LIVE) {

if($entry->type->handle == "callout") {
$this->request->buildPagesFromEntry($this->homeUri, false);
} else if ($entry->uri != null) {
$revalidateMenu = ($entry->type->handle == "pages");
$this->request->buildPagesFromEntry($entry, $revalidateMenu);
$this->request->buildPagesFromEntry($entry->uri, $revalidateMenu);
}

}
}
);
Expand Down Expand Up @@ -138,11 +146,12 @@ function (ModelEvent $event) {
!ElementHelper::isDraftOrRevision($entry) &&
!($entry->duplicateOf && $entry->getIsCanonical() && !$entry->updatingFromDerivative) &&
!ElementHelper::rootElement($entry)->isProvisionalDraft &&
!$entry->resaving
!$entry->resaving &&
$entry->uri != null
) {
$revalidateMenu = ($entry->type->handle == "pages");
Craft::$app->onAfterRequest(function() use ($entry, $revalidateMenu) {
$this->request->buildPagesFromEntry($entry, $revalidateMenu);
$this->request->buildPagesFromEntry($entry->uri, $revalidateMenu);
});
}
}
Expand All @@ -157,11 +166,12 @@ function (Event $event) {
$this->settings->activeSections[$entry->section->handle] &&
!ElementHelper::isDraftOrRevision($entry) &&
!($entry->duplicateOf && $entry->getIsCanonical() && !$entry->updatingFromDerivative) &&
!ElementHelper::rootElement($entry)->isProvisionalDraft
!ElementHelper::rootElement($entry)->isProvisionalDraft &&
$entry->uri != null
) {
$revalidateMenu = ($entry->type->handle == "pages");
Craft::$app->onAfterRequest(function() use ($entry, $revalidateMenu) {
$this->request->buildPagesFromEntry($entry, $revalidateMenu);
$this->request->buildPagesFromEntry($entry->uri, $revalidateMenu);
});
}
}
Expand All @@ -188,11 +198,12 @@ function (MoveElementEvent $event) {
$this->settings->activeSections[$handle] &&
!ElementHelper::isDraftOrRevision($entry) &&
!($entry->duplicateOf && $entry->getIsCanonical() && !$entry->updatingFromDerivative) &&
!ElementHelper::rootElement($entry)->isProvisionalDraft
!ElementHelper::rootElement($entry)->isProvisionalDraft &&
$entry->uri != null
) {
$revalidateMenu = ($handle == "pages");
Craft::$app->onAfterRequest(function() use ($entry, $revalidateMenu) {
$this->request->buildPagesFromEntry($entry, $revalidateMenu);
$this->request->buildPagesFromEntry($entry->uri, $revalidateMenu);
});
}
}
Expand All @@ -207,11 +218,12 @@ function (Event $event) {
$this->settings->activeSections[$entry->section->handle] &&
!ElementHelper::isDraftOrRevision($entry) &&
!($entry->duplicateOf && $entry->getIsCanonical() && !$entry->updatingFromDerivative) &&
!ElementHelper::rootElement($entry)->isProvisionalDraft
!ElementHelper::rootElement($entry)->isProvisionalDraft &&
$entry->uri != null
) {
$revalidateMenu = ($entry->type->handle == "pages");
Craft::$app->onAfterRequest(function() use ($entry, $revalidateMenu) {
$this->request->buildPagesFromEntry($entry, $revalidateMenu);
$this->request->buildPagesFromEntry($entry->uri, $revalidateMenu);
});
}
}
Expand Down
10 changes: 4 additions & 6 deletions src/services/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
use Craft;
use lsst\nextbuilds\NextBuilds;
use craft\base\Component;
use craft\elements\Entry;
use craft\helpers\Session;
use GuzzleHttp\Client;

/**
Expand All @@ -27,18 +25,18 @@ class Request extends Component
// =========================================================================

/**
* @param Entry $entry
* @param String uri
* @return void
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function buildPagesFromEntry(Entry $entry, string $revalidateMenu)
public function buildPagesFromEntry(string $uri, bool $revalidateMenu)
{
$settings = NextBuilds::getInstance()->getSettings();
$client = new Client();

$endpoint = $this->getSettingsData($settings->nextApiBaseUrl) . self::NEXT_ENDPOINT_REVALIDATE;
$params = [
'uri' => $entry->uri,
'uri' => $uri,
'secret' => $this->getSettingsData($settings->nextSecretToken)
];
if ($revalidateMenu) {
Expand All @@ -51,7 +49,7 @@ public function buildPagesFromEntry(Entry $entry, string $revalidateMenu)
$client->request('GET', $requestUrl, []);
$isConsoleRequest = Craft::$app->getRequest()->getIsConsoleRequest();
if (!$isConsoleRequest) {
Craft::$app->session->setNotice('Requesting revalidation: ' . $entry->uri);
Craft::$app->session->setNotice('Requesting revalidation: ' . $uri);
}
} catch (\Exception $exception) {
Craft::error($exception->getMessage(), "REVALIDATE_STATUS");
Expand Down

0 comments on commit 589f9f3

Please sign in to comment.