Skip to content

Set cache control header for full page cache #1309

Open
statamic/cms
#11430
@indykoning

Description

@indykoning

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions