I have created a filter for asset requests to DAM. For certain assets, I want to completely disable caching in the dispatcher and AEMaaCS CDN.
Is it enough to simply set the headers from Java, or do I need to also apply some logic in the dispatcher?
For instance, I am doing something like this:
public static void removeResponseCaching(SlingHttpServletResponse response) {
response.setHeader("Surrogate-Control", "no-store");
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0");
response.setHeader("Dispatcher", "no-cache");
response.setHeader("Pragma", "no-cache");
response.setHeader("Expires", "0");
}
However, I am noticing that cache control headers are still coming back for certain requests even though this code is being executed.
I am trying something like this in my vhost:
<LocationMatch "^/content/.*\.coreimg.*\.(?i:jpe?g|png|gif|svg)$">
<If "%{HTTP:Cache-Control} in { 'no-cache' }">
Header unset Cache-Control
Header unset Expires
Header set Expires 0
Header set Age 0
Header set Cache-Control "no-cache"
Header set Surrogate-Control "private"
</If>
<Else>
Header set Cache-Control "max-age=2592000,stale-while-revalidate=43200,stale-if-error=43200,public,immutable" "expr=%{REQUEST_STATUS} < 400"
Header set Age 0
</Else>
</LocationMatch>
If I make a request to the file and supply Cache-Control: no-cache on the request, then the response contains the proper Cache-Control: no-cache header. However, if the request doesn't have the Cache-Control directive, it seems that the Cache-Control header is being set to
@dylanmccurry : If you don't want to cache certain requests based on their glob patterns, you should create a rule in your dispatcher configuration. Please refer: https://experienceleague.adobe.com/en/docs/experience-manager-learn/ams/dispatcher/understanding-cac...
Views
Replies
Total Likes
Can you create a glob pattern based on header values?
Views
Replies
Total Likes
@dylanmccurry : glob pattern should be based on content (dam) path and not on header values.
You can refer other reply below from @arunpatidar and try with it for your use-case.
Views
Replies
Total Likes
Thanks, I've found that I can do this from my vhost file:
# Files from DAM: cache mutable resources for max 24h on CDN and background refresh after 12h to avoid MISS
<LocationMatch "^/content/dam/.*$">
# Unset existing Cache-Control and Expires headers if the request status is less than 400 and X-Dispatcher is not 'no-cache'
Header unset Cache-Control "expr=%{REQUEST_STATUS} < 400 && %{resp:X-Dispatcher} != 'no-cache'"
Header unset Expires "expr=%{REQUEST_STATUS} < 400 && %{resp:X-Dispatcher} != 'no-cache'"
# Set Cache-Control header for browser caching for a max of 1 day
Header always set Cache-Control "max-age=86400,stale-while-revalidate=14400,stale-if-error=43200,public,immutable" "expr=%{REQUEST_STATUS} < 400 && %{resp:X-Dispatcher} != 'no-cache'"
# Set Surrogate-Control header for CDN caching for a max of 3 days
Header always set Surrogate-Control "max-age=259200,stale-while-revalidate=86400,stale-if-error=172800" "expr=%{REQUEST_STATUS} < 400 && %{resp:X-Dispatcher} != 'no-cache'"
</LocationMatch>
# HTML pages: Cache for 5min with background refresh 15m on browser and 4h on CDN to avoid MISS, also incl. requests with query parameter
# Cache-Control headers will be added for any response which doesn't have an X-Dispatcher header set to 'no-cache'
<LocationMatch "^/content/.*\.html$">
# Unset existing Cache-Control and Expires headers if the request status is less than 400 and X-Dispatcher is not 'no-cache'
Header unset Cache-Control "expr=%{REQUEST_STATUS} < 400 && %{resp:X-Dispatcher} != 'no-cache'"
Header unset Expires "expr=%{REQUEST_STATUS} < 400 && %{resp:X-Dispatcher} != 'no-cache'"
# Set Cache-Control header for browser caching for a max of 5 minutes
Header always set Cache-Control "max-age=300,stale-while-revalidate=900" "expr=%{REQUEST_STATUS} < 400 && %{resp:X-Dispatcher} != 'no-cache'"
# Set Surrogate-Control header for CDN caching for a max of 5.5 minutes
Header always set Surrogate-Control "stale-while-revalidate=330,stale-if-error=930" "expr=%{REQUEST_STATUS} < 400 && %{resp:X-Dispatcher} != 'no-cache'"
# Error page caching by checking request status. Set it to a low value (e.g., 1 - 2 mins) in case content is being published
Header always set Cache-Control "max-age=60,stale-while-revalidate=120" "expr=%{REQUEST_STATUS} == 404"
Header always set Surrogate-Control "stale-while-revalidate=120,stale-if-error=120" "expr=%{REQUEST_STATUS} == 404"
</LocationMatch>
# If X-Dispatcher is set to no-cache, do not cache the response
Header unset Expires "expr=%{resp:X-Dispatcher} == 'no-cache'"
Header unset Cache-Control "expr=%{resp:X-Dispatcher} == 'no-cache'"
Header unset Surrogate-Control "expr=%{resp:X-Dispatcher} == 'no-cache'"
Header always set Cache-Control "private" "expr=%{resp:X-Dispatcher} == 'no-cache'"
Header always set Surrogate-Control "private" "expr=%{resp:X-Dispatcher} == 'no-cache'"
So the general idea is that certain requests squelch caching by adding an 'X-Dispatcher: no-cache' header to the response.
If you skip caching images, it will impact performance heavily. so reconsider caching again.
But in order to deny caching for dispatcher add a rule something like below : more info
/0005 {
/glob "/content/.*.coreimg.*.(svg|pdf)"
/type "deny"
}
to disabled CDN cache add a cache-control header from vhost file , more info
<LocationMatch "^/content/.*\.(jpeg|jpg)$">
Header set Cache-Control "max-age=0"
Header set Age 0
</LocationMatch>
Views
Like
Replies