Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Externalizing URL in JavaScript Use Api

Raghava_Joijode
Level 3
Level 3

Hi All,

How can I use Externalizer in JavaScript Use API, I can easily use it using Java API. But I need this in JavaScript Use API to add canonical tag in head section of pages for SEO purpose.

-Thanks,
Raghava

1 Accepted Solution
daniel_henriqu1
Correct answer by
Level 4
Level 4

Hi, Raghava.

You can use the sling global object (it is an instance of the SlingScriptHelper class) to obtain a reference to the Externalizer service.

Untested code:

var externalizer = sling.getService(Packages.com.day.cq.commons.Externalizer); var externalURL = externalizer.externalLink(resolver, resource.getPath());

Regards,

Daniel.

View solution in original post

5 Replies
daniel_henriqu1
Correct answer by
Level 4
Level 4

Hi, Raghava.

You can use the sling global object (it is an instance of the SlingScriptHelper class) to obtain a reference to the Externalizer service.

Untested code:

var externalizer = sling.getService(Packages.com.day.cq.commons.Externalizer); var externalURL = externalizer.externalLink(resolver, resource.getPath());

Regards,

Daniel.

View solution in original post

Raghava_Joijode
Level 3
Level 3

Hi Daniel,

Thanks for the inputs, I'll try implementing this. 

vjetty
Level 4
Level 4

Hi, Raghava

Here is the example with CQ JS API - CQ.HTTP.externalize. You may try something like this.

 var currentPageContent = CQ.HTTP.get(CQ.HTTP.externalize(CQ.utils.WCM.getPagePath() + "/jcr:content.json"));

Thanks! 

daniel_henriqu1
Level 4
Level 4

Vamsi, AFAIK CQ.HTTP is exposed by a client lib and therefore is not available to be used in a JavaScript Use (a server side element interpreted by Rhino).

Regards,

Daniel.

Raghava_Joijode
Level 3
Level 3

Thanks Daniel it worked, 

Working code snip: 

            var externalizer = sling.getService(Packages.com.day.cq.commons.Externalizer);
            var externalURL = externalizer.externalLink(global.resource.getResourceResolver(),"publish", global.currentPage.getPath());

 

Thanks.