diff --git a/config/static_caching.php b/config/static_caching.php index e190b9729d..4e62ed35e9 100644 --- a/config/static_caching.php +++ b/config/static_caching.php @@ -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' => 120, // The amount of time a Proxy/CDN may cache this response for. + 'stale_while_revalidate' => 120, // How long may a stale cache be used while fetching fresh content in the background. + /* |-------------------------------------------------------------------------- | Exclusions diff --git a/src/StaticCaching/Middleware/Cache.php b/src/StaticCaching/Middleware/Cache.php index 80f86e5ce1..555fa3e790 100644 --- a/src/StaticCaching/Middleware/Cache.php +++ b/src/StaticCaching/Middleware/Cache.php @@ -20,6 +20,7 @@ use Statamic\StaticCaching\NoCache\Session; use Statamic\StaticCaching\Replacer; use Statamic\StaticCaching\ResponseStatus; +use Statamic\StaticCaching\UrlExcluder; class Cache { @@ -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); } @@ -125,6 +130,7 @@ private function attemptToGetCachedResponse($request) $this->makeReplacements($response); $response->setStaticCacheResponseStatus(ResponseStatus::HIT); + $response->makeCacheControlCacheable(); return $response; } diff --git a/src/StaticCaching/ServiceProvider.php b/src/StaticCaching/ServiceProvider.php index 5a025affe4..d98137ed4f 100644 --- a/src/StaticCaching/ServiceProvider.php +++ b/src/StaticCaching/ServiceProvider.php @@ -3,6 +3,7 @@ namespace Statamic\StaticCaching; use Illuminate\Http\Request; +use Illuminate\Http\Response; use Illuminate\Support\Facades\Blade; use Illuminate\Support\Facades\Event; use Illuminate\Support\ServiceProvider as LaravelServiceProvider; @@ -91,6 +92,14 @@ public function boot() return app(Cacher::class)->getUrl($this); }); + Response::macro('makeCacheControlCacheable', function () { + $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;