diff --git a/docs/page-caching.md b/docs/page-caching.md index 4057fda1..11dc4fe1 100644 --- a/docs/page-caching.md +++ b/docs/page-caching.md @@ -53,9 +53,12 @@ Pages which can be cached have default time-to-live (TTL) values within the cach - Success responses (`200`): 300 seconds (5 minutes) - Client errors (`400`, except `404`): 10 seconds -- Not found errors (`404`): 300 seconds (5 minutes) +- Not found errors (`404`) generated by nginx before the request reaches WordPress: 300 seconds (5 minutes) - Server errors (`5XX`): 300 seconds (5 minutes) +WordPress-generated 404 responses are not cached by default. WordPress sends `no-cache` headers for these responses, and Altis does +not cache responses with a `Cache-Control: no-cache` header. + Time-to-live values should be balanced between achieving high cache hit rates and ensuring fresh content is served. This can be adjusted by sending @@ -68,6 +71,20 @@ For example, to specify a lifetime of one hour for a specific page: header( 'Cache-Control: max-age=' . HOUR_IN_SECONDS ); ``` +For high-traffic 404 pages generated by WordPress, you can override WordPress' default `no-cache` headers and allow the CDN to cache +the response by sending an explicit `Cache-Control` header: + +```php +add_action( 'template_redirect', function () { + if ( ! is_404() ) { + return; + } + + $lifetime = 5 * MINUTE_IN_SECONDS; + header( 'Cache-Control: s-maxage=' . $lifetime . ', max-age=' . $lifetime . ', must-revalidate' ); +} ); +``` + (Note that headers should be sent before any output occurs on the page.) ## Developer Considerations