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 cache-control header to cached content #11430

Open
wants to merge 5 commits into
base: 5.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 17 additions & 0 deletions config/static_caching.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,23 @@

],

/*
|--------------------------------------------------------------------------
| Cache Control
|--------------------------------------------------------------------------
|
| Here you may choose what information gets sent in the cache-control
| header when cache gets hit in PHP.
| Ages are in seconds.
|
| Note that when using the "full" strategy you must also set this in
| Your webserver, e.g.: max-age=60, public, s-maxage=60, stale-while-revalidate=60
*/

'max_age' => 120, // The amount of time the browser may cache this response for.
'shared_max_age' => null, // The amount of time a Proxy/CDN may cache this response for. null will use max_age.
'stale_while_revalidate' => 120, // How long may a stale cache be used while fetching fresh content in the background.

/*
|--------------------------------------------------------------------------
| Exclusions
Expand Down
6 changes: 6 additions & 0 deletions src/StaticCaching/Middleware/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Statamic\StaticCaching\NoCache\Session;
use Statamic\StaticCaching\Replacer;
use Statamic\StaticCaching\ResponseStatus;
use Statamic\StaticCaching\UrlExcluder;

class Cache
{
Expand Down Expand Up @@ -83,6 +84,10 @@ private function handleRequest($request, Closure $next)
$this->makeReplacementsAndCacheResponse($request, $response);

$this->nocache->write();

if(!app(UrlExcluder::class)->isExcluded($request->normalizedFullUrl())) {
$response->makeCacheControlCacheable();
}
} elseif (! $response->isRedirect()) {
$this->makeReplacements($response);
}
Expand Down Expand Up @@ -125,6 +130,7 @@ private function attemptToGetCachedResponse($request)
$this->makeReplacements($response);

$response->setStaticCacheResponseStatus(ResponseStatus::HIT);
$response->makeCacheControlCacheable();

return $response;
}
Expand Down
8 changes: 8 additions & 0 deletions src/StaticCaching/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ public function boot()
return app(Cacher::class)->getUrl($this);
});

Response::macro('makeCacheControlCacheable', function () {
indykoning marked this conversation as resolved.
Show resolved Hide resolved
$this
->setMaxAge(config('statamic.static_caching.max_age', 60))
->setSharedMaxAge(config('statamic.static_caching.shared_max_age', config('statamic.static_caching.max_age', 60)))
->setStaleWhileRevalidate(config('statamic.static_caching.stale_while_revalidate', 60))
->setEtag(md5($this->getContent() ?: ''));
});

Request::macro('fakeStaticCacheStatus', function (int $status) {
$url = '/__shared-errors/'.$status;
$this->pathInfo = $url;
Expand Down
Loading