Skip to content

[5.x] Add --header option to static warm command #11763

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

Merged
merged 2 commits into from
May 14, 2025
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
28 changes: 26 additions & 2 deletions src/Console/Commands/StaticWarm.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class StaticWarm extends Command
{--include= : Only warm specific URLs}
{--exclude= : Exclude specific URLs}
{--max-requests= : Maximum number of requests to warm}
{--header=* : Set custom header (e.g. "Authorization: Bearer your_token")}
';

protected $description = 'Warms the static cache by visiting all URLs';
Expand Down Expand Up @@ -167,8 +168,10 @@ private function getRelativeUri(int $index): string

private function requests()
{
return $this->uris()->map(function ($uri) {
return new Request('GET', $uri);
$headers = $this->parseHeaders($this->option('header'));

return $this->uris()->map(function ($uri) use ($headers) {
return new Request('GET', $uri, $headers);
})->all();
}

Expand Down Expand Up @@ -374,4 +377,25 @@ protected function additionalUris(): Collection

return $uris->map(fn ($uri) => URL::makeAbsolute($uri));
}

private function parseHeaders($headerOptions): array
{
$headers = [];
if (empty($headerOptions)) {
return $headers;
}
if (! is_array($headerOptions)) {
$headerOptions = [$headerOptions];
}
foreach ($headerOptions as $header) {
if (strpos($header, ':') !== false) {
[$key, $value] = explode(':', $header, 2);
$headers[trim($key)] = trim($value);
} else {
$this->line("<fg=yellow;options=bold>Warning:</> Invalid header format: '$header'. Headers should be in 'Key: Value' format.");
}
}

return $headers;
}
}
19 changes: 19 additions & 0 deletions tests/Console/Commands/StaticWarmTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,25 @@ public static function queueConnectionsProvider()
];
}

#[Test]
public function it_sets_custom_headers_on_requests()
{
config(['statamic.static_caching.strategy' => 'half']);

$mock = Mockery::mock(\GuzzleHttp\Client::class);
$mock->shouldReceive('send')->andReturnUsing(function ($request) {
$this->assertEquals('Bearer testtoken', $request->getHeaderLine('Authorization'));
$this->assertEquals('Bar', $request->getHeaderLine('X-Foo'));

return Mockery::mock(\GuzzleHttp\Psr7\Response::class);
});
$this->app->instance(\GuzzleHttp\Client::class, $mock);

$this->artisan('statamic:static:warm', [
'--header' => ['Authorization: Bearer testtoken', 'X-Foo: Bar'],
])->assertExitCode(0);
}

private function createPage($slug, $attributes = [])
{
$this->makeCollection()->save();
Expand Down