April 1, 2020
Estimated Post Reading Time ~

How to cache Error page in CQ

Use Case:
1) You want to serve your error page from dispatcher
2) Error pages are creating a lot of load on publish instance

Prerequisite:
http://sling.apache.org/site/errorhandling.html
http://dev.day.com/docs/en/cq/current/developing/customizing_error_handler_pages.html
http://dev.day.com/docs/en/cq/current/deploying/dispatcher/disp_config.html
Assumption: You are using apache as webserver

Solution:
1) Create your custom error handler using pre-requisite doc
2) Set status code in your error handler
for example:
for 404 in /apps/sling/servlet/errorhandler/404.jsp use response.setStatus(404) do not do any redirect. In fact you can have just one line (In publish).
3) Then in your dispatcher set DispatcherPassError to 1

<IfModule disp_apache2.c>
# All your existing configuration
DispatcherPassError 1
</IfModule>

4) Then configure your error document in httpd.conf (Or your custom configuration file), Something like

<LocationMatch \.html$>
ErrorDocument 404 /404.html
#ErrorDocument <ANY OTHER ERROR CODE> <PATH IN YOUR DOCROOT>
</LocationMatch>

Above rule mean, For any .html request if you get 404 response show /404.html page. All other extension will be handled by dispatcher 404 response (As shown below). This is a good approach in case you don't want to load your 404 (With all CSS and JS) for all other extensions.

You can have customize error file as well, Something like

<LocationMatch "/india">
ErrorDocument 404 /404india.html
#ErrorDocument <ANY OTHER ERROR CODE> <PATH IN YOUR DOCROOT>
</LocationMatch>


With the above configuration if someone requests a page, that may lead to any of the above error codes, in that case, the page will be served by CQ dispatcher and not by Publish instance. This will also avoid unnecessary load on publish instance.


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.