diff --git a/src/StaticCaching/DefaultInvalidator.php b/src/StaticCaching/DefaultInvalidator.php index 3e0ed222c2a..6a77d450597 100644 --- a/src/StaticCaching/DefaultInvalidator.php +++ b/src/StaticCaching/DefaultInvalidator.php @@ -22,6 +22,7 @@ class DefaultInvalidator implements Invalidator { protected $cacher; protected $rules; + protected $refreshing = false; public function __construct(Cacher $cacher, $rules = []) { @@ -32,14 +33,18 @@ public function __construct(Cacher $cacher, $rules = []) public function invalidate($item) { if ($this->rules === 'all') { - $this->cacher->flush(); + $this->refreshing + ? $this->cacher->refreshUrls($this->cacher->getUrls()->all()) + : $this->cacher->flush(); return; } $urls = $this->getItemUrls($item); - $this->cacher->invalidateUrls($urls); + $this->refreshing + ? $this->cacher->refreshUrls($urls) + : $this->cacher->invalidateUrls($urls); } public function refresh($item) @@ -50,15 +55,14 @@ public function refresh($item) return; } - if ($this->rules === 'all') { - $this->cacher->refreshUrls($this->cacher->getUrls()->all()); + $previous = $this->refreshing; + $this->refreshing = true; - return; + try { + $this->invalidate($item); + } finally { + $this->refreshing = $previous; } - - $urls = $this->getItemUrls($item); - - $this->cacher->refreshUrls($urls); } protected function getItemUrls($item) diff --git a/tests/StaticCaching/DefaultInvalidatorTest.php b/tests/StaticCaching/DefaultInvalidatorTest.php index d56fb552f61..c13315a7892 100644 --- a/tests/StaticCaching/DefaultInvalidatorTest.php +++ b/tests/StaticCaching/DefaultInvalidatorTest.php @@ -1020,4 +1020,27 @@ public function it_recaches_when_background_recache_token_is_enabled() $this->assertNull($invalidator->refresh($entry)); } + + #[Test] + public function it_calls_the_custom_invalidate_method_when_background_recache_is_enabled() + { + config()->set('statamic.static_caching.background_recache', true); + + $cacher = tap(Mockery::mock(Cacher::class), function ($cacher) { + $cacher->shouldReceive('refreshUrls')->never(); + $cacher->shouldReceive('invalidateUrls')->once()->with([ + 'http://localhost/custom', + ]); + }); + + $invalidator = new class($cacher, []) extends Invalidator + { + public function invalidate($item) + { + $this->cacher->invalidateUrls(['http://localhost/custom']); + } + }; + + $invalidator->refresh(new \stdClass); + } }