Thursday, July 12, 2012

How to Use Dispatcher with Mapped content

Use Case: You are using /etc/map or resource resolution to map your content and dispatcher flush is not working any more.
More information about Sling mapping can be found here http://sling.apache.org/site/mappings-for-resource-resolution.html

I have seen a lot of customer using sling mapping or resource resolver setting to map /content/<There site> to / to shorten URL. But as soon as they do that, They claim that dispatcher flush is not working. And here is reason why,

1) Dispatcher flush does not take mapping or resource resolver rule in to account to flush cache or invalidate static file.

To understand it better, Here is use case,

1) From example your site URL is somedomain.com/content/mysite/en/us/survey.html and then you shorten it to somedomain.com/en/us/survey.html by mapping rules or resource resolver rule.

2) Suppose your document root is /docroot/htdocs

3) Now when some one try to access page somedomain.com/en/us/survey.html your page will get cached under /docroot/htdocs/en/us/survey.html

4) But when you will activate this page from author page under somedomain.com/content/mysite/en/us/survey.html will get flush, As dispatcher flush agent has no idea about mapping rules.

Resolution:

Now in order to avoid this problem, You should have mod_rewrite rule along with resource resolver and mapping rules.

Here is basic example of rules you want in your mod_rewrite (These rules will differ from site to site)

RewriteRule ^/$ <Your home page>

RewriteCond %{REQUEST_URI} !^/apps/(.*) [NC]
RewriteCond %{REQUEST_URI} !^/etc(.*) [NC]
RewriteCond %{REQUEST_URI} !^/libs(.*) [NC]
RewriteCond %{REQUEST_URI} !^/content(.*) [NC]
RewriteCond %{REQUEST_URI} !^/system(.*) [NC]
RewriteCond %{REQUEST_URI} !^/dam(.*) [NC]
RewriteRule ^/(.*) /content/mysite/$1 [PT]
RewriteRule ^/(.*)\?(.*) /content/mysite/$1 [PT]

Above rule make sure that content at right path is getting flushed.

Some more trick (But will not work 100% see http://forums.adobe.com/thread/1082213 for detail):

You can also do something like

LoadModule headers_module modules/mod_headers.so
RequestHeader edit CQ-Handle /content/mysite(/.*) $1 early

Another solution at replication agent level By David to solve this issue,

http://adobe-consulting-services.github.io/acs-aem-commons/features/dispatcher-flush-rules.html

code for above is found here https://github.com/Adobe-Consulting-Services/acs-aem-commons/tree/master/bundle/src/main/java/com/adobe/acs/commons/replication

Q: Why I will use etc/map or resource resolution if these rules are enough for mapping ?
A: Mod_rewriter rules will take care of just mapping and not link rewriting. You need /etc/map or resource resolver rules for link rewriting.

Q: Why dispatcher flush do not take mapping rules in to account ?
A: There is already an enhancement request for this.

Q: I don't want to write these rewrite rules, How else I can handle this ?
A: One option is set your stat file level to 0 (Which is default), This might invalidate all resource under invalidation tag of dispatcher.any upon activation. But again if you watch your dispatcher log carefully, wrong file will get evicted on activation. But you still see updated content due to invalidation.

Q: How about vanity URL ?
A: Well thats problematic. You might want to stick with stat file or write mod_rewrite rules for them as well. 

On side note, understanding /statfileslevel is very important to improve performance of your website.

5 comments:

  1. With the use of Adobe, mapping will be easier. Basically because, it's user-friendly and the terms of set up of it isn't that hard to configure and customize.

    ReplyDelete
  2. Hi Sir, You are really good writer.


    It helps me to know about this about on wemblog.com.It is very helpful in using social media sites. You have been shown importance topic in your content. It will inspire me always. This is very nice post! I will bookmark this blog as I have No Pay No Stay Provides Services For Eviction Process, Eviction Service, Houston Eviction Texas, Vacate Notices, File For Eviction, Property Management, Sell Your Property And More.


    Thank You Very Much For a Nice & Cool Article.

    ReplyDelete
  3. In this same vein (and i suppose you could use it for this use case as well), we built out an OSS Dispatcher Flush Rules implementation that lets you map flushing of arbitrary paths based on the replication of some "origin resource"

    Basically, you can setup any number of rules in the following manner:

    regex-matching-replicated-resource=/some/other/path/to/flush

    http://adobe-consulting-services.github.io/acs-aem-commons/features/dispatcher-flush-rules.html

    Use it wholesale or checkout the source on github to inspire your own solution.

    ReplyDelete
    Replies
    1. Hello David,

      Thank you very much for sharing that article. Very useful.

      Yogesh

      Delete
    2. Hi David,

      This is very is very userful, Thank you.

      Regards,
      Rajesh

      Delete