Skip to content

Commit

Permalink
Determine source IDs from structure ID
Browse files Browse the repository at this point in the history
  • Loading branch information
bencroker committed Feb 26, 2024
1 parent d30e57e commit 0610875
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

- Reverted deleting expired URIs from the database immediately when refreshing expired cache, as it can cause cached pages not to be refreshed ([#624](https://github.com/putyourlightson/craft-blitz/issues/624)).
- Tracked element queries that have invalid params or that cannot be executed are now deleted when refreshing the cache.
- Tracked entry queries can now determine source IDs from a structure ID.
- Optimised the params that are stored on tracked element queries.
- Made the ordering of items displayed in the Blitz diagnostics utility deterministic.

Expand Down
22 changes: 21 additions & 1 deletion src/services/GenerateCacheService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use craft\behaviors\CustomFieldBehavior;
use craft\db\ActiveRecord;
use craft\elements\db\ElementQuery;
use craft\elements\Entry;
use craft\events\CancelableEvent;
use craft\events\EagerLoadElementsEvent;
use craft\events\PopulateElementEvent;
Expand All @@ -21,6 +22,7 @@
use craft\helpers\StringHelper;
use craft\models\Section;
use craft\records\Element as ElementRecord;
use craft\records\Section as SectionRecord;
use craft\services\Elements;
use craft\web\View;
use putyourlightson\blitz\behaviors\BlitzCustomFieldBehavior;
Expand Down Expand Up @@ -351,7 +353,14 @@ public function saveElementQuerySources(ElementQuery $elementQuery, int $queryId
$sourceIds = $sourceIdAttribute ? $elementQuery->{$sourceIdAttribute} : null;

if (empty($sourceIds)) {
return;
// Get the source ID from the structure, if one is set.
if ($elementQuery->elementType === Entry::class && $elementQuery->structureId !== null) {
$sourceIds = $this->_getSourceIdsFromStructureId($elementQuery->structureId);
}

if (empty($sourceIds)) {
return;
}
}

// Normalize source IDs
Expand Down Expand Up @@ -633,6 +642,17 @@ private function _shouldTrackElementQueriesOfType(string $elementType): bool
return true;
}

/**
* Returns the source IDs associated with a structure ID.
*/
private function _getSourceIdsFromStructureId(int $structureId): array
{
return SectionRecord::find()
->select('id')
->where(['structureId' => $structureId])
->column();
}

/**
* Adds related element IDs with all statuses so that disabled elements will
* trigger a refresh whenever enabled.
Expand Down
File renamed without changes.
20 changes: 15 additions & 5 deletions tests/pest/Feature/GenerateCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
->toHaveRecordCount(1, ['elementId' => $entry->id]);
});

test('Element cache record is saved with eager loaded custom fields', function() {
test('Element cache record is saved with eager-loaded custom fields', function() {
$entry = Entry::find()->with(['relatedTo'])->one();
Blitz::$plugin->generateCache->addElement($entry);
Blitz::$plugin->generateCache->save(createOutput(), createSiteUri());
Expand All @@ -131,7 +131,7 @@
->toHaveRecordCount(1, ['elementId' => $entry->id]);
});

test('Element cache record is saved with eager loaded custom fields in variable', function() {
test('Element cache record is saved with eager-loaded custom fields in variable', function() {
$entry = createEntryWithRelationship();
Craft::$app->elements->eagerLoadElements(Entry::class, [$entry], ['relatedTo']);
Blitz::$plugin->generateCache->addElement($entry);
Expand All @@ -153,12 +153,12 @@
->toHaveRecordCount(1, ['elementId' => $entry->id]);
});

test('Element cache record is saved with eager loaded custom fields for preloaded single', function() {
test('Element cache record is saved with eager-loaded custom fields for preloaded single', function() {
Craft::$app->config->general->preloadSingles = true;
$singleSectionHandle = App::env('TEST_SINGLE_SECTION_HANDLE');
$entry = Entry::find()->section($singleSectionHandle)->one();
Craft::$app->getView()->setTemplatesPath(Craft::getAlias('@putyourlightson/blitz/test/templates'));
Craft::$app->view->renderTemplate('_eager.twig');
Craft::$app->view->renderTemplate('eager.twig');

Blitz::$plugin->generateCache->save(createOutput(), createSiteUri());

Expand All @@ -181,7 +181,7 @@
->toContain($enabledEntry->id, $disabledEntry->id);
});

test('Element cache records are saved with all statuses for eager loaded relation field queries', function() {
test('Element cache records are saved with all statuses for eager-loaded relation field queries', function() {
$enabledEntry = createEntry();
$disabledEntry = createEntry(enabled: false);
$entry = createEntryWithRelationship([$enabledEntry, $disabledEntry]);
Expand Down Expand Up @@ -465,6 +465,16 @@
->toHaveRecordCount(0);
});

test('Entry query source records are saved when only a structure ID is set', function() {
$section = Craft::$app->getSections()->getSectionByHandle(App::env('TEST_STRUCTURE_SECTION_HANDLE'));
Blitz::$plugin->generateCache->addElementQuery(Entry::find()->structureId($section->structureId));

expect(ElementQuerySourceRecord::class)
->toHaveRecordCount(1)
->and(ElementQuerySourceRecord::find()->one()->sourceId)
->toBe($section->id);
});

test('Element query attribute records are saved', function() {
$elementQuery = Entry::find()->title('x');
Blitz::$plugin->generateCache->addElementQuery($elementQuery);
Expand Down

0 comments on commit 0610875

Please sign in to comment.