Skip to content
Open
Show file tree
Hide file tree
Changes from 6 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
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' => 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
Expand Down
8 changes: 8 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 @@ -72,6 +73,7 @@ private function handleRequest($request, Closure $next)
}

if ($response = $this->attemptToServeCachedResponse($request)) {
$response->isNotModified($request);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a check from the Symfony response: https://github.com/symfony/symfony/blob/b5b0cbf0acb520629cc72532f99728e31992a55f/src/Symfony/Component/HttpFoundation/Response.php#L1113
It checks the etags on the request and response to determine if the local cache is still valid after the expiration time.
If it is, it will empty the response body and set the response code to 304 Not Modified. Saving the (Kilo)bytes from having to be downloaded again if they haven't changed.

Laravel also has a middleware that does this https://github.com/laravel/framework/blob/12.x/src/Illuminate/Http/Middleware/CheckResponseForModifications.php however it is not loaded by default.
It causes no issues if the check is called multiple times.

return $response;
}

Expand All @@ -83,6 +85,11 @@ private function handleRequest($request, Closure $next)
$this->makeReplacementsAndCacheResponse($request, $response);

$this->nocache->write();

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

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

return $response;
}
Expand Down
9 changes: 9 additions & 0 deletions src/StaticCaching/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Loading