Skip to content

Modify the maximum cache age

Most responses with an HTTP Status Code of 200 have a default cache-control response header value of max-age=300 set by VIP’s page cache. For static asset files, the default cache-control response is set to max-age=31536000 (1 year).

In most cases, the cache-control response header value can be set to a custom value in WordPress and Node.js applications.

Limitations

  • The max-age and s-maxage header values cannot be modified for files that are stored in the VIP File System.
  • The cache-control response header value cannot be modified for responses with status codes 301, 302, 307, 404, or 410

Best practices

Caution

Do not set max-age to less than 1 minute for heavily trafficked resource types. 

  • Routes for site endpoints that rarely change should be set to a higher max-age value, which can improve the cache hit rate.
  • There is no upper limit to the value set for max-age, but increasing this value is only beneficial within a limited range. By default, cached page content is evicted from the cache according to the max-age TTL  and the STALE response, which is set to 12 hours for content that is unable to be refreshed.
  • The page cache also relies on a Least Recently Used (LRU) strategy which will apply if the page cache is low in memory. As a result, setting max-age to a  value beyond a certain range (e.g. > 24 hours) can result in content that is requested less often to occupy valuable space in the page cache, which can lead to more frequent page cache evictions.

Code examples

In this WordPress code example, max-age is set to a custom value for feeds. Note that the example includes an is_user_logged_in() check. This ensures that the cache Time To Live (TTL) headers are not set for logged-in users, as this would trigger browser caching for them.

add_action( 'wp', 'my_cache_maxage' );
/**
 * Hooks the wp action to insert some cache control
 * max-age headers.
 *
 * @param Object wp The WP object, passed by reference
 * @return void
 */
function my_cache_maxage( $wp ) {
    if ( is_feed() ) {
        // Set the max age for feeds to 10 minutes.
        if ( ! is_user_logged_in() ) {
            header( 'Cache-Control: max-age=' . ( 10 * MINUTE_IN_SECONDS ) );         
        }
    }
}

In this Node.js code example, max-age is set to a custom value for an Express.js route.

const express = require( 'express' );

const app = express();

app.get( '/', function ( req, res ) {
  res
    .set( 'cache-control', 'max-age=3600' )
    .send( 'This response will be cached for 1 hour (3600 seconds)' );
});

Last updated: May 29, 2024

Relevant to

  • Node.js
  • WordPress