Skip to content

Modify the maximum cache age

For most responses with an HTTP Status Code of 200, the default cache-control response header value set by VIP’s page cache is max-age=300. This can be set to a custom value on WordPress and Node.js applications.

Caution

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

Caching for static assets of WordPress and Node.js environments has a different set of default behaviors.

WordPress

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 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 ) );         
        }
    }
}

Node.js

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: September 13, 2023