Skip to content

Set cache control header for full page cache #1309

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
indykoning opened this issue Feb 5, 2025 · 0 comments · May be fixed by statamic/cms#11430
Open

Set cache control header for full page cache #1309

indykoning opened this issue Feb 5, 2025 · 0 comments · May be fixed by statamic/cms#11430

Comments

@indykoning
Copy link

indykoning commented Feb 5, 2025

While setting the correct Nginx config will reduce usefulness we can improve speed by setting a cache control header if our page is served from cache, or can be/will be/has been cached

By default Laravel will not set cache control, causing Symfony to always return cache-control: no-cache, private
https://github.com/symfony/symfony/blob/6f4f04b98f6134996a57d21d1851d558a34dc45c/src/Symfony/Component/HttpFoundation/ResponseHeaderBag.php#L251

Which honestly is a safe default since Laravel cannot know what can and can't be cached. That is dependent on the page.

However, if Statamic can cache the page statically we can assume a browser and CDN could cache the page too.

https://github.com/statamic/cms/blob/779a92a223d16ee16f6af6de0b6a9acfc95be082/src/StaticCaching/Middleware/Cache.php#L111
https://github.com/statamic/cms/blob/779a92a223d16ee16f6af6de0b6a9acfc95be082/src/StaticCaching/Middleware/Cache.php#L84

These places would need to be updated with

->setPublic(); https://github.com/symfony/symfony/blob/6f4f04b98f6134996a57d21d1851d558a34dc45c/src/Symfony/Component/HttpFoundation/Response.php#L604
->setMaxAge();
https://github.com/symfony/symfony/blob/6f4f04b98f6134996a57d21d1851d558a34dc45c/src/Symfony/Component/HttpFoundation/Response.php#L788
->setStaleWhileRevalidate();
https://github.com/symfony/symfony/blob/6f4f04b98f6134996a57d21d1851d558a34dc45c/src/Symfony/Component/HttpFoundation/Response.php#L820

if possible it would be a really nice added bonus to set the Etag
https://github.com/symfony/symfony/blob/6f4f04b98f6134996a57d21d1851d558a34dc45c/src/Symfony/Component/HttpFoundation/Response.php#L949

Here's how laravel implements it if you use their cache control middleware
https://github.com/laravel/framework/blob/43993ed92af54aa8620d8e779a7dcd658f44364c/src/Illuminate/Http/Middleware/SetCacheHeaders.php#L66
aka
->setEtag(md5($response->getContent()))
This will allow the browser and CDN to skip downloading the body if it's still in cache and matches the etag.
having set up my Nginx settings as in statamic/docs#1602 and configuring Cloudlfare to generate/pass Etags my "Content download" of a cached page is 2ms

Image

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 a pull request may close this issue.

1 participant