Skip to content

Commit

Permalink
Make sure stories loaded by slug are cached
Browse files Browse the repository at this point in the history
  • Loading branch information
Javier Villanueva committed May 23, 2021
1 parent 7e23929 commit 1e6f726
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
8 changes: 6 additions & 2 deletions Block/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ public function getCacheLifetime(): int

public function getIdentities(): array
{
if (!empty($this->getData('story')['id'])) {
if (!empty($this->getSlug())) {
return ["storyblok_slug_{$this->getSlug()}"];
} elseif (!empty($this->getData('story')['id'])) {
return ["storyblok_{$this->getData('story')['id']}"];
}

Expand All @@ -61,7 +63,9 @@ public function getCacheKeyInfo(): array
{
$info = parent::getCacheKeyInfo();

if (!empty($this->getData('story')['id'])) {
if (!empty($this->getSlug())) {
$info[] = "storyblok_slug_{$this->getSlug()}";
} elseif (!empty($this->getData('story')['id'])) {
$info[] = "storyblok_{$this->getData('story')['id']}";
}

Expand Down
17 changes: 4 additions & 13 deletions Test/Unit/Block/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,8 @@ public function testRenderStoryBySlug()
]);

$this->assertEquals('html', $block->toHtml());
$this->assertContains(
"storyblok_{$fixtureStoryArray['story']['id']}",
$block->getIdentities()
);
$this->assertContains(
"storyblok_{$fixtureStoryArray['story']['id']}",
$block->getCacheKeyInfo()
);
$this->assertContains("storyblok_slug_{$storySlug}", $block->getIdentities());
$this->assertContains("storyblok_slug_{$storySlug}", $block->getCacheKeyInfo());
}

public function testDoesNotRenderStoryIfNotFound()
Expand All @@ -275,11 +269,8 @@ public function testDoesNotRenderStoryIfNotFound()
]);

$this->assertEmpty($block->toHtml());
$this->assertEmpty($block->getIdentities());

foreach ($block->getCacheKeyInfo() as $cacheKey) {
$this->assertStringStartsNotWith('storyblok_', $cacheKey);
}
$this->assertContains("storyblok_slug_{$storySlug}", $block->getIdentities());
$this->assertContains("storyblok_slug_{$storySlug}", $block->getCacheKeyInfo());
}

public function testRendersDebugBlockIfTemplateNotFound()
Expand Down

0 comments on commit 1e6f726

Please sign in to comment.