Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x] Add an --uncached option to the static warm command #11188

Merged
merged 4 commits into from
Dec 2, 2024
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
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
Loading