MaynorSong
MaynorSong

Reputation: 640

How to disable dispatcher cache on page template in AEM?

I have pages of template type "account" that brings a special header if some cookies are present. However when i delete the cookies and refresh the page, the special header is still there and only goes away when I republish the page. This is due the dispatcher. I want to disable the dispatcher on pages created from that template. I have seen that the dispatcher can be disabled with this code on JSP.

response.setHeader("Dispatcher", "no-cache");

But not sure where to use this on a non JSP project.

Upvotes: 0

Views: 2887

Answers (4)

Sharath Madappa
Sharath Madappa

Reputation: 3402

It isn't right to disable cache outright to handle this situation, you should also be looking at options to make pages created out of this template cache-friendly.

Some options are

  1. If there are no security constraints around the data in header or cookie manipulate the header via javascript client-side
  2. Use Sling Dynamic include to load the header via SSI or Ajax

https://sling.apache.org/documentation/bundles/dynamic-includes.html

Upvotes: 0

Andrey Shilov
Andrey Shilov

Reputation: 66

I guess that it makes sense to set headers in Filter, it will give more flexibility and maintainability.

@Component(service = Filter.class, property = { "sling.filter.scope=request", "sling.filter.pattern=(.*)/<your-website>/(.*)", })
public class CachingPolicyFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // Nothing to do here
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
       // check template/resourceType
       // add header
     filterChain.doFilter(request, response);
    }

        
    @Override
    public void destroy() {
        // Nothing to do here
    }
}

Optionally configure templates via OSGI Config

Upvotes: 0

MaynorSong
MaynorSong

Reputation: 640

I found the answer on AEM forum, below: https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/disabling-dispatcher-cache-for-some-pages-with-http-header/qaq-p/396124

I quote the response from Manjunath_K:

If you want to disable caching your pages in dispatcher, you can add rule in dispatcher config file as mentioned here.

https://experienceleague.adobe.com/docs/experience-manager-dispatcher/using/configuring/dispatcher-c...

If you want handle this through AEM backend then below are the 2 options.

  1. To disable dispatcher caching the pages in which specific component is added, set response header in that specific component model class.

    @Model(adaptables = SlingHttpServletRequest.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL) public class ComponentModel {

     @Inject
     private SlingHttpServletResponse response;
    
     @PostConstruct
     protected void init() {
         response.setHeader("Dispatcher", "no-cache");
     }
    

    }

  2. If you have this use case for specific pages not by specific component basis, then create common cache control model class & include call to that model class in page footer level based on page condition check.

@Model(adaptables = SlingHttpServletRequest.class, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL) public class CacheControlModel {

@Inject
private SlingHttpServletResponse response;

@PostConstruct
protected void init() {
    response.setHeader("Dispatcher", "no-cache");
}

}

HTML

Upvotes: 1

Tethich
Tethich

Reputation: 1

Depending on your implementation language you may have a way to set headers on the response object.

If you are in a Sling servlet, either in doGet() or doPost method, you could use method response.setHeader(String name, String value);

Upvotes: 0

Related Questions