Skip to content

Commit df4f864

Browse files
committed
Support Laraval Cloud caching
1 parent d1eba48 commit df4f864

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

app/Http/Controllers/ProxyController.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,19 @@ public function __invoke(Request $request)
3838
});
3939

4040
return Response::make($response['body'], 200, [
41+
'Cache-Control' => $this->edgeCacheControlHeader(),
4142
'Content-Type' => $response['contentType'],
43+
'Vary' => 'Host',
4244
]);
4345
}
4446

47+
private function edgeCacheControlHeader(): string
48+
{
49+
$ttl = (int) Config::get('pinkary.cache_ttl');
50+
51+
return "public, max-age={$ttl}, s-maxage={$ttl}";
52+
}
53+
4554
private function replacePinkaryDomainWithHostDomain(string $body, Request $request): string
4655
{
4756
$paths = [

tests/Feature/ProxyTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@
4444
$testResponse->assertSee('https://pinkary.com/@kapersoft');
4545
$testResponse->assertDontSee('http://localhost/@kapersoft');
4646
$testResponse->assertHeader('Content-Type', 'text/html; charset=UTF-8');
47+
$cacheControl = (string) $testResponse->headers->get('Cache-Control');
48+
expect($cacheControl)->toContain('public')
49+
->and($cacheControl)->toContain('max-age=3600')
50+
->and($cacheControl)->toContain('s-maxage=3600');
51+
$testResponse->assertHeader('Vary', 'Host');
4752
assertSame([
4853
'body' => $responseBody = <<<'HTML'
4954
<link rel="shortcut icon" href="http://localhost/img/ico.svg">
@@ -72,6 +77,11 @@
7277
$testResponse->assertStatus(200);
7378
$testResponse->assertSee('pinkary profile picture');
7479
$testResponse->assertHeader('Content-Type', 'image/png');
80+
$cacheControl = (string) $testResponse->headers->get('Cache-Control');
81+
expect($cacheControl)->toContain('public')
82+
->and($cacheControl)->toContain('max-age=3600')
83+
->and($cacheControl)->toContain('s-maxage=3600');
84+
$testResponse->assertHeader('Vary', 'Host');
7585
assertSame([
7686
'body' => 'pinkary profile picture',
7787
'contentType' => 'image/png',

0 commit comments

Comments
 (0)