How to disable CDN caching

Learn how to disable the caching of HTTP responses in AEM as a Cloud Service’s CDN. The caching of responses is controlled by Cache-Control, Surrogate-Control, or Expires HTTP response cache headers.

These cache headers are typically set in AEM Dispatcher vhost configurations using mod_headers, but can also be set in custom Java™ code running in AEM Publish itself.

Default caching behavior

Review the default caching behavior for AEM Publish and Author when an AEM Project Archetype based AEM project is deployed.

Disable caching

Turning off caching can have a negative impact on the performance of your AEM as a Cloud Service instance, so exercise caution when turning off default caching behavior.

However there are some scenarios where you may want to disable caching, such as:

  • Developing a new feature and want to see the changes immediately.
  • Content is secure (meant only for authenticated users) or dynamic (shopping cart, order details) and should not be cached.

To disable caching, you can update the cache headers in two ways.

  1. Dispatcher vhost configuration: Only available for AEM Publish.
  2. Custom Java™ code: Available for both AEM Publish and Author.

Let’s review each of these options.

Dispatcher vhost configuration

This option is the recommended approach for disabling caching however it is only available for AEM Publish. To update the cache headers, use the mod_headers module and <LocationMatch> directive in the Apache HTTP Server’s vhost file. The general syntax is as follows:

<LocationMatch "$URL$ || $URL_REGEX$">
    # Removes the response header of this name, if it exists. If there are multiple headers of the same name, all will be removed.
    Header unset Cache-Control
    Header unset Expires

    # Instructs the CDN to not cache the response.
    Header set Cache-Control "private"
</LocationMatch>

Example

To disable the CDN caching of the CSS content types for some troubleshooting purposes, follow these steps.

Note that, to bypass the existing CSS cache, a change in the CSS file is required to generate a new cache key for the CSS file.

  1. In your AEM project, locate the desired vhsot file from dispatcher/src/conf.d/available_vhosts directory.

  2. Update the vhost (e.g wknd.vhost) file as follows:

    code language-none
    <LocationMatch "^/etc.clientlibs/.*\.(css)$">
        # Removes the response header of this name, if it exists. If there are multiple headers of the same name, all will be removed.
        Header unset Cache-Control
        Header unset Expires
    
        # Instructs the CDN to not cache the response.
        Header set Cache-Control "private"
    </LocationMatch>
    

    The vhost files in dispatcher/src/conf.d/enabled_vhosts directory are symlinks to the files in dispatcher/src/conf.d/available_vhosts directory, so make sure to create symlinks if not present.

  3. Deploy the vhost changes to desired AEM as a Cloud Service environment using the Cloud Manager - Web Tier Config Pipeline or RDE Commands.

Custom Java™ code

This option is available for both AEM Publish and Author. To update the cache headers, use the SlingHttpServletResponse object in custom Java™ code (Sling servlet, Sling servlet filter). The general syntax is as follows:

response.setHeader("Cache-Control", "private");
recommendation-more-help
4859a77c-7971-4ac9-8f5c-4260823c6f69