Search code examples
aemdispatcher

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.


Solution

  • 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