Issue with Cache Control for Download Assets in AEM | Community
Skip to main content
Level 2
February 10, 2025
Solved

Issue with Cache Control for Download Assets in AEM

  • February 10, 2025
  • 2 replies
  • 689 views

Background

In my project, I am working on caching control for downloadable assets in AEM. I have written a cache rule to handle assets with URLs following these patterns:

 

Example URL for myprojectdownload assets:

https://dev-publish.myproject.com/content/dam/myproject/path/subsi/public/en/downloadsmajor-resolutions-of-board-meetings/112%A0%85.pdf.myprojectdownload.pdf/%85_112%E5%B9%B4

 

Example URL for coredownload assets:

 

https://dev-publish.myproject.com/content/dam/myproject/path/subsi/public/en/downloadsmajor-resolutions-of-board-meetings/112%A0%85.pdf.coredownload.pdf/%85_112%E5%B9%B4

 

Implemented Cache Rule

To ensure these assets are cached correctly, I have added the following LocationMatch rule in the Apache configuration:

 

<LocationMatch "^/content/dam/.*\.(coredownload|myprojectdownload)\.(?i:pdf).*">
Header set Cache-Control "max-age=95400, stale-while-revalidate=1800, stale-if-error=43200, public, immutable" "expr=%{REQUEST_STATUS} < 400"
Header set Age 0
</LocationMatch>

 

Issue Encountered

Despite implementing the above cache rule, the cache control headers are missing from the response. Below is the current response header

 

 

accept-ranges: bytes
age: 0
content-disposition: attachment; filename="112 .pdf"
content-encoding: gzip
content-type: application/pdf
permissions-policy: private-state-token-redemption=()
permissions-policy: private-state-token-issuance=()
permissions-policy: run-ad-auction=()
permissions-policy: join-ad-interest-group=()
vary: Accept-Encoding
x-cache: MISS
x-content-type-options: nosniff
x-frame-options: ORIGIN
x-served-by: cache-BOM

 

Expected Behavior

I expect the response to include the Cache-Control header with the specified directives so that these assets can be cached properly by browsers and CDNs.

 

Questions

  • Why is the Cache-Control header missing, despite being explicitly set in the configuration?
  • How can I ensure that cache control settings are correctly applied to these assets?

    Any insights or recommendations would be greatly appreciated!

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Shiv_Prakash_Patel

Hi @rahulsi13 ,

Below are the answers to your queries.

1. Why is the Cache-Control header missing, despite being explicitly set in the configuration?

  • Incorrect Regular Expression in LocationMatch. The regex is matching filenames before .pdf, but URLs have additional encoded characters, so modify the regex as below:
<LocationMatch "^/content/dam/.*\.(coredownload|myprojectdownload)\.pdf$">
  • The Header set directive requires mod_headers ensure it should be enabled.
// check if it is enabled apachectl -M | grep headers //If not enabled, Activate it a2enmod headers systemctl restart apache2
  • Check if another rule in Apache, Dispatcher, or a CDN (Akamai, Cloudflare) is stripping headers. ensure Header unset Cache-Control should NOT present.
grep -Ri "Header unset Cache-Control" /etc/apache2/
  • Ensure dispatcher.any contains below configurations
/clientheaders { "Cache-Control" }

2. How can I ensure that cache control settings are correctly applied to these assets?

  • Use curl to check the response:
// Curl Command curl -I https://dev-publish.myproject.com/content/dam/myproject/path/subsi/public/en/downloadsmajor-resolutions-of-board-meetings/112.pdf.coredownload.pdf //Expected Output Cache-Control: max-age=95400, stale-while-revalidate=1800, stale-if-error=43200, public, immutable
  • Bypass CDN/Dispatcher to ensure headers are being set at Apache level as below curls, If headers appear, the issue is likely Dispatcher/CDN stripping them
curl -I -H "Pragma: no-cache" https://your-server-ip/content/dam/...
  • Ensure AEM allows custom headers by unchecking "Enable For All Paths" in Apache Sling Content Disposition Filter under AEM OSGi Configuration (/system/console/configMgr).

Regards,

2 replies

RahulSi13Author
Level 2
February 10, 2025

URL are :

 

dev-publish.myproject.com/content/dam/myproject/path/subsi/public/en/downloadsmajor-resolutions-of-board-meetings/112%A0%85.pdf.coredownload.pdf/%85_112%E5%B9%B4

 

dev1-publish.myproject.com/content/dam/myproject/path/subsi/public/en/downloadsmajor-resolutions-of-board-meetings/112%A0%85.pdf.myprojectdownload.pdf/%85_112%E5%B9%B4

RahulSi13Author
Level 2
February 10, 2025
Shiv_Prakash_Patel
Community Advisor
Shiv_Prakash_PatelCommunity AdvisorAccepted solution
Community Advisor
February 10, 2025

Hi @rahulsi13 ,

Below are the answers to your queries.

1. Why is the Cache-Control header missing, despite being explicitly set in the configuration?

  • Incorrect Regular Expression in LocationMatch. The regex is matching filenames before .pdf, but URLs have additional encoded characters, so modify the regex as below:
<LocationMatch "^/content/dam/.*\.(coredownload|myprojectdownload)\.pdf$">
  • The Header set directive requires mod_headers ensure it should be enabled.
// check if it is enabled apachectl -M | grep headers //If not enabled, Activate it a2enmod headers systemctl restart apache2
  • Check if another rule in Apache, Dispatcher, or a CDN (Akamai, Cloudflare) is stripping headers. ensure Header unset Cache-Control should NOT present.
grep -Ri "Header unset Cache-Control" /etc/apache2/
  • Ensure dispatcher.any contains below configurations
/clientheaders { "Cache-Control" }

2. How can I ensure that cache control settings are correctly applied to these assets?

  • Use curl to check the response:
// Curl Command curl -I https://dev-publish.myproject.com/content/dam/myproject/path/subsi/public/en/downloadsmajor-resolutions-of-board-meetings/112.pdf.coredownload.pdf //Expected Output Cache-Control: max-age=95400, stale-while-revalidate=1800, stale-if-error=43200, public, immutable
  • Bypass CDN/Dispatcher to ensure headers are being set at Apache level as below curls, If headers appear, the issue is likely Dispatcher/CDN stripping them
curl -I -H "Pragma: no-cache" https://your-server-ip/content/dam/...
  • Ensure AEM allows custom headers by unchecking "Enable For All Paths" in Apache Sling Content Disposition Filter under AEM OSGi Configuration (/system/console/configMgr).

Regards,

Shiv Prakash