[6.x] Add cache-control header to cached content#11430
[6.x] Add cache-control header to cached content#11430indykoning wants to merge 12 commits intostatamic:6.xfrom
Conversation
|
This PR should now be ready to merge now, it is currently in use without any issues 🙂 |
Calling this check triggers the ETag matching, and sets the notModified response code if a match is found. If https://github.com/laravel/framework/blob/12.x/src/Illuminate/Http/Middleware/CheckResponseForModifications.php is in the middleware stack it would do it too, it is not in the stack by default. And checking multiple times causes no issues
| } | ||
|
|
||
| if ($response = $this->attemptToServeCachedResponse($request)) { | ||
| $response->isNotModified($request); |
There was a problem hiding this comment.
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.
|
Seeing the way half cache operates i've decided it's the best choice to hold off on that for now, and focus on using it for full page cache for the current scope. Since half strategy cache replaces the CSRF token and nocache tags before serving the response, nocache content will most-likely get cached. we could work around it by using a shared max age of 0. meaning your browser will get allowed to cache, but your CDN/Proxies do not. This is of no issue for full strategy as we're already considering that the entire file will be statically cached and no replacements can be done server-side. So all replacements for nocache and CSRF tokens are done client-side. I've updated the PR to focus on the full strategy |
Closes statamic/ideas#1309
Half and full caching strategies will get these improvements if these are pages that can be cached.
This PR will add cache-control public headers for pages that can be/will be/have been cached or have been served from cache if the webserver wasn't configured properly or query params did not match.
It can improve experienced speed and reduce server load by using local copies of statically cached content. And if combined with a properly configured CDN which supports caching pages can reduce server load as the CDN will serve the page and occasionally update it's cache.
(If you're using a CDN in-between you might not even need the full strategy anymore)Edit: ^ That is not true (yet) since it still requires the csrf token + session, cookies will be sent. Which means CDNs will not cache unless specifically instructed to ignore the cookies