Expand my Community achievements bar.

SOLVED

Mapping adaptive image to different domain.

Avatar

Former Community Member

I would like to map images to subdomain, so path to resource

/content/sitename/en/homepage/_jcr_content/par/adaptiveimage.img.full.high.jpg/1389935334441.jpg

would be rewritten by ResourceResolver to smth like this

static.domain/homepage/_jcr_content/par/adaptiveimage.img.full.high.jpg/1389935334441.jpg

When I try the simplest (not even trying to use regex to catch all requests to images) scenario by configuring ResourceResolver like this:

<?xml version="1.0" encoding="UTF-8"?>
<jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0"
    jcr:primaryType="sling:Mapping"
    sling:internalRedirect="[/content/site/en,/]"
    sling:match="http://static.domain"/>

I can see that all of the links used on the page are rewritten correctly (), but references in adaptive image component markup remain the same, e.g.

---

<div data-picture data-alt=''>
    <div data-src='/content/site/en/homepage/jcr:content/par/adaptiveimage.img.320.low.jpg/1389935334441.jpg'       data-media="(min-width: 1px)"></div>                                        
    <div data-src='/content/site/en/homepage/jcr:content/par/adaptiveimage.img.320.medium.jpg/1389935334441.jpg'    data-media="(min-width: 320px)"></div>  
    <div data-src='/content/site/en/homepage/jcr:content/par/adaptiveimage.img.480.medium.jpg/1389935334441.jpg'    data-media="(min-width: 321px)"></div>  
    <div data-src='/content/site/en/homepage/jcr:content/par/adaptiveimage.img.476.high.jpg/1389935334441.jpg'      data-media="(min-width: 481px)"></div>   
    <div data-src='/content/site/en/homepage/jcr:content/par/adaptiveimage.img.620.high.jpg/1389935334441.jpg'      data-media="(min-width: 769px)"></div>  
    <div data-src='/content/site/en/homepage/jcr:content/par/adaptiveimage.img.full.high.jpg/1389935334441.jpg'     data-media="(min-width: 1025px)"></div>

    <noscript>
        <img src='/content/site/en/homepage/_jcr_content/par/adaptiveimage.img.320.low.jpg/1389935334441.jpg' alt=''>
    </noscript>
</div>

---

Even if I try to use image path in a link field, say - /content/site/en/homepage/jcr:content/par/adaptiveimage.img.full.high.jpg/1389935334441.jpg. The link appears but still not rewritten according to the mapping.

 

Any ideas?

1 Accepted Solution

Avatar

Correct answer by
Employee

 Hi,

The default CQ link rewriter only rewrites a subset of HTML tags and only rewrites links ending in .html.

There's a custom rewriter pipeline component in ACS AEM Commons which handles the static domain part, but not the path mapping: https://github.com/Adobe-Consulting-Services/acs-aem-commons/blob/master/bundle/src/main/java/com/ad.... In all the cases I've seen, rewriting image links is unnecessary as these URLs are not seen by most users.

Regards,

Justin

View solution in original post

3 Replies

Avatar

Level 6

This is, as I see it, two problems. The first one is to make the the domain point to a specific root in the path. That is if we have multiple domains served by the same CQ instance. Then we need to map the content root to the domain, both forward and reverse mappings. That is done something like this for the domain/site about.mysite.org that is stored in CQ under /content/mysite/en (Not actually tested).

Forward <?xml version="1.0" encoding="UTF-8"?> <jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0" jcr:mixinTypes="[cq:ReplicationStatus]" jcr:primaryType="sling:Mapping" sling:internalRedirect="[/content/mysite/$2.html$3]" sling:match="(.*)about\.mysite\.org\.[0-9]*((?!(/)).*)\.html([\?|/|#].+)?$"/> Reverse <?xml version="1.0" encoding="UTF-8"?> <jcr:root xmlns:sling="http://sling.apache.org/jcr/sling/1.0" xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0" jcr:mixinTypes="[cq:ReplicationStatus]" jcr:primaryType="sling:Mapping" sling:internalRedirect="[^/content/mysite/]" sling:match="/"/>  

Some explanation on the regular expression in the forward. We use the (.*) infront of the about part of the domain name, since then we can use it on all environments, like qa-about and stage-about. That is the first capture group, $1, that we don't use.

The next one is the .[0-9]*. That is when we use different ports. Then comes the ((?!(/)).*)   . This is the magic second capture group, $2, that catches the path. This one we append in the internal redirect. The last capture group is ([\?|/|#].+) . This one grabs and forwards the query parameters or sling parameters. In this example, I only allow this to be done on the ,html extension. Everything else just uses the normal path.

The reverse is much easier. I map a specific path, the /content/mysite, to the /. Maybe you can make the reverse mapping append your domain, but I don't think so...

This is the first part of your question.

The second one is a bit trickier... All the output of image URLs are done relative. The jsp parser does not know that we want all images to start with static.mysite.org. We have two options now. Either we make our own, domain aware, adaptive image, and have that append our static domain to all images. Or we make our own adaptive image, and use the externalize api. I would use the later, since then I can have non-static paths on the author environment and static on the publish.

Hope that this gives you some more info.

/Ove

Avatar

Correct answer by
Employee

 Hi,

The default CQ link rewriter only rewrites a subset of HTML tags and only rewrites links ending in .html.

There's a custom rewriter pipeline component in ACS AEM Commons which handles the static domain part, but not the path mapping: https://github.com/Adobe-Consulting-Services/acs-aem-commons/blob/master/bundle/src/main/java/com/ad.... In all the cases I've seen, rewriting image links is unnecessary as these URLs are not seen by most users.

Regards,

Justin

Avatar

Level 2

Hi, when it comes to staticreferencerewriter, we have been using this in our project. Now, we are expanding to Multi site and hence I am not sure is it will work for different sites. I created different configs with different host patterns. But, it doesn't seem to be working and always ends up taking the last entry in the config. Please let me know how this can be extended for MSM. Thanks.