Skip to content

[5.x] Add cache-control header to cached content #11430

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

Open
wants to merge 9 commits into
base: 5.x
Choose a base branch
from

Conversation

indykoning
Copy link
Contributor

@indykoning indykoning commented Feb 7, 2025

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

@duncanmcclean duncanmcclean changed the title Add cache-control header to cached content [5.x] Add cache-control header to cached content Feb 7, 2025
@indykoning
Copy link
Contributor Author

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
@@ -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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Set cache control header for full page cache
2 participants