Thursday, 14 June 2018

RedirectTarget property is not working in AEM 6.2

I tried to redirect a page to another using redirectTarget page property in wcmmode=disabled in author instance.

This redirectTarget property is working on Geomertrixx site and not on we-retail site.

Root Cause:
Geomertrixx site is created using template /libs/foundation/components/page and we-retail site is created using wcm/foundation/components/page.

Page component under foundation path is having code to redirect to another page, where as page component under wcm is not having code to redirect. In page.jsp we have code to redirect response.sendRedirect(redirectPath); which we don't have it in page.html.

Try these:
  • Try running AEM on publish runmode. 
  • Try creating a page with sling:resourceType: foundation/components/redirect. 

Saturday, 9 June 2018

Access ClientLibs under apps folder through ClientLibraryProxyServlet

AEM provides ClientLibs feature to organize JS and CSS. Clientlibs can be created as 

  1. Approach 1: Place all files under /etc/designs/{project}/clientlibs path.
  2. Approach 2: Creating clientlibs under each component. Disadvantage with this approach is it increases number of calls to load clientlibs throuhout the page. This doesn't follow best practices as CSS will not be loaded in head section.
  3. Approach 3: Place clientlibs under /apps/project/. 
/apps folder is restricted to access by dispatcher. So we need to enable ClientLibraryProxyServlet  to access the clientlibs from apps folder.  Below are the steps to enable clientLibrary Proxy.

  • Create a new client library under /apps/... with new category. Let's say myapp.all
  • Add a embed property and  include all modular component library. (firstcomp,secondcomp)
  • Add boolean allowProxy = 'true'
  • On the headlibs.jsp include the above 
  • Then the clientlib will then be proxied via  there by passing access control set on the clientlib.

    • Configure dispatcher.any to allow for /etc.clientlibs/

Friday, 25 May 2018

Rename JSession Cookie name in AEM

JSessionID cookie name can be renamed by following below steps.


1. Go to http://localhost:4502/system/console/configMgr/org.apache.felix.http and log in as admin. We can do this by using an osgiConfig node for specific runmmode.

2. Change the value for Session Cookie Name (org.eclipse.jetty.servlet.SessionCookie))

3. Changing this property will cause AEM to restart itself (< 2 seconds)

Saturday, 21 April 2018

aemsync tool

AEM sync tool pushed code to multiple AEM instances instantly when there is a file change.

Prerequisites:

Install npm on your machine.

Installation

Run below npm command to install aemsync

npm install aemsync -g
The tool pushes code changes to AEM instance(s) upon a file change.
  • There is no vault dependency.
  • It can push to multiple instances at the same time (e.g. author and publish).
  • IDE/editor agnostic.
  • Works on Windows, Linux and Mac.

Wednesday, 14 March 2018

String encoding issue

Sometimes we may get a scenario where string need to be decoded. We may get request in correct format, but we need to make sure its correctly decoded.

package com.kishore.encode;

import java.nio.charset.StandardCharsets;

public class CharsetEncoding {
    public static void main(String[] args) {
        String name = "(Kishore PROâ„¢)";
        byte[] bytes = name.getBytes(StandardCharsets.ISO_8859_1);
        name = new String(bytes, StandardCharsets.UTF_8);
        System.out.println("name ::" + name);
    }
}

Output: Kishore PRO™

Sunday, 18 February 2018

Ajax reqeuts getting cached in IE and Firefox

Usecase: Debug ajax cache issues in IE and Firefox.

Issue: Ajax requests return wrong or same data in IE and Firefox, since the requests get cached in the browser. Requests work for the first time, from next time browser will give the same result.

Root Cause: When a GET request is made to a web service, IE will automatically cache the responses from GET requests. Once IE has successfully made a GET request, it will no longer make a new AJAX call until the cache expires on that object.

Solutions:

  1. Use POST: Use POST requests instead of GET requests in your application. However, this is bad practise, simply because POST requests should only be used when you are submitting data or modifying a resource on the server.
  2. Response Headers: You can prevent caching by sending additional headers along with your response. By specifying the “Cache-Control” header with a value of “no-cache,no-store” and returning it with the web service response you can instruct the browser not to cache the result. For example in C#:HttpContext.Current.Response.AddHeader("Cache-Control","no-cache,no-store");
  3. Cache Buster: A cache-buster is a dynamic parameter that you append to a request which makes each request unique, most commonly a random number or the current date/time ticks.
    1. var url = '/get/userDetails?buster='+new Date().getTime();
  4. Disable Cache for JQuery AJAX Request:  If you’re using JQuery to perform your Ajax requests, you can add cache: false to the parameter list like so:
$.ajax({
    url: url,
    cache: false,
    dataType: 'json',
    type: "GET",
    data: data,
    success: func,
    error: ajaxError
});
If you want to globally setup cache for all JQuery AJAX requests use below code.
$.ajaxSetup({ cache: false });