Skip to content

Commit

Permalink
[5.x] Add an --uncached option to the static warm command (#11188)
Browse files Browse the repository at this point in the history
Co-authored-by: Jason Varga <[email protected]>
  • Loading branch information
arthurperton and jasonvarga authored Dec 2, 2024
1 parent b114722 commit c596273
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Console/Commands/StaticWarm.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Illuminate\Console\Command;
use Illuminate\Http\Request as HttpRequest;
use Illuminate\Routing\Route;
use Illuminate\Support\Collection;
use Statamic\Console\EnhancesCommands;
Expand Down Expand Up @@ -36,6 +37,7 @@ class StaticWarm extends Command
{--u|user= : HTTP authentication user}
{--p|password= : HTTP authentication password}
{--insecure : Skip SSL verification}
{--uncached : Only warm uncached URLs}
';

protected $description = 'Warms the static cache by visiting all URLs';
Expand Down Expand Up @@ -178,6 +180,10 @@ private function uris(): Collection
->merge($this->additionalUris())
->unique()
->reject(function ($uri) use ($cacher) {
if ($this->option('uncached') && $cacher->hasCachedPage(HttpRequest::create($uri))) {
return true;
}

Site::resolveCurrentUrlUsing(fn () => $uri);

return $cacher->isExcluded($uri);
Expand Down
17 changes: 17 additions & 0 deletions tests/Console/Commands/StaticWarmTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

use Facades\Tests\Factories\EntryFactory;
use Illuminate\Support\Facades\Queue;
use Mockery;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Console\Commands\StaticWarmJob;
use Statamic\Facades\Collection;
use Statamic\StaticCaching\Cacher;
use Tests\PreventSavingStacheItemsToDisk;
use Tests\TestCase;

Expand Down Expand Up @@ -41,6 +43,21 @@ public function it_warms_the_static_cache()
->assertExitCode(0);
}

#[Test]
public function it_only_visits_uncached_urls_when_the_eco_option_is_used()
{
$mock = Mockery::mock(Cacher::class);
$mock->shouldReceive('hasCachedPage')->times(2)->andReturn(true, false);
$mock->allows('isExcluded')->andReturn(false);
app()->instance(Cacher::class, $mock);

config(['statamic.static_caching.strategy' => 'half']);

$this->artisan('statamic:static:warm', ['--uncached' => true])
->expectsOutput('Visiting 1 URLs...')
->assertExitCode(0);
}

#[Test]
public function it_doesnt_queue_the_requests_when_connection_is_set_to_sync()
{
Expand Down

0 comments on commit c596273

Please sign in to comment.