Skip to content
Merged
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
5 changes: 5 additions & 0 deletions src/StaticCaching/Middleware/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Statamic\Facades\StaticCache;
use Statamic\Statamic;
use Statamic\StaticCaching\Cacher;
use Statamic\StaticCaching\Cachers\AbstractCacher;
use Statamic\StaticCaching\Cachers\ApplicationCacher;
use Statamic\StaticCaching\Cachers\FileCacher;
use Statamic\StaticCaching\Cachers\NullCacher;
Expand Down Expand Up @@ -199,6 +200,10 @@ private function shouldBeCached($request, $response)
return false;
}

if ($this->cacher instanceof AbstractCacher && $this->cacher->isExcluded($this->cacher->getUrl($request))) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm checking the AbstractCacher here because the isExcluded method doesn't exist on the Cacher interface.

return false;
}

return true;
}

Expand Down
68 changes: 68 additions & 0 deletions tests/StaticCaching/FullMeasureStaticCachingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,72 @@ public function it_should_add_the_javascript_if_there_is_a_csrf_token()
$this->assertTrue(file_exists($this->dir.'/about_.html'));
$this->assertEquals('<html><body>STATAMIC_CSRF_TOKEN<script>js here</script></body></html>', file_get_contents($this->dir.'/about_.html'));
}

#[Test]
public function excluded_pages_should_have_real_csrf_token()
{
config(['statamic.static_caching.exclude' => [
'urls' => ['/about'],
]]);

$this->withFakeViews();
$this->viewShouldReturnRaw('layout', '<html><body>{{ template_content }}</body></html>');
$this->viewShouldReturnRaw('default', '{{ csrf_token }}');

$this->createPage('about');

$response = $this
->get('/about')
->assertOk();

// The response should have the real CSRF token, not the placeholder.
$this->assertEquals('<html><body>'.csrf_token().'</body></html>', $response->getContent());

// The page should not be cached.
$this->assertFalse(file_exists($this->dir.'/about_.html'));
}

#[Test]
public function excluded_pages_should_have_nocache_regions_replaced()
{
config(['statamic.static_caching.exclude' => [
'urls' => ['/about'],
]]);

app()->instance('example_count', 0);

(new class extends \Statamic\Tags\Tags
{
public static $handle = 'example_count';

public function index()
{
$count = app('example_count');
$count++;
app()->instance('example_count', $count);

return $count;
}
})::register();

$this->withFakeViews();
$this->viewShouldReturnRaw('layout', '<html><body>{{ template_content }}</body></html>');
$this->viewShouldReturnRaw('default', '{{ example_count }} {{ nocache }}{{ example_count }}{{ /nocache }}');

$this->createPage('about');

StaticCache::nocacheJs('js here');
StaticCache::nocachePlaceholder('<svg>Loading...</svg>');

$response = $this
->get('/about')
->assertOk();

// The response should have the nocache regions replaced with rendered content, no placeholders or JS.
$this->assertEquals('<html><body>1 2</body></html>', $response->getContent());
$this->assertStringNotContainsString('<script>', $response->getContent());

// The page should not be cached.
$this->assertFalse(file_exists($this->dir.'/about_.html'));
}
}