adobe/asset-share-commons

Custom Fallback Renditions for Search Results and Details Image

DigiLiviu opened this issue · 4 comments

Hi,

Our clients wants to be able to set their own renditions for some of the assets for the Search Results cards and the Image component on the Details page. As such, we created customized InternalRedirectRenditionDispatcherImpl and StaticRenditionDispatcherImpl configs.

We can now upload files named rc-details-thumbnail.png and rc-results-thumbnail.png as new renditions and our configs will pick them up for the Details (Image Component) and Results (Cards) pages respectively.

The only problem is that now assets that they did add these custom renditions to are showing up as broken images. Is there a way to set a fallback on those configs? It just seems like it maps a rendition name to a filename without any options on what to do if that file isn't found...

I know they take regexes so we could create a regex that would satisfy two files, the first of which being the custom one and the second being the standard one, but I'm tentative to trust that the order in which they get resolved in will be consistent. Is that not a realistic concern?

Our current solution is that we have created a custom Processing Profile to create those "custom" renditions automatically as the standard thumbnails that AEM also generates so they always exist and the client can override them as needed. This seems like a risky solution though because if these assets ever get processed again (if someone accidentally triggers a reprocess), it seems as any existing (custom) renditions would get recreated with the default ones.

Any guidance is much appreciated.

Thanks,

Liviu

Following up on this, I have noticed (strangely), that I was indeed able to create a StaticRenditionDispatcherImpl config with an OR in the regex like

"rendition.mappings": [
    "rc-details-thumb=^(rc-details-thumbnail(\\..+)?)|(cq5dam\\.web\\.\\d+\\.\\d+\\..+)$",
]

Where rc-details-thumbnail.png is the rendition that I want it to look for first and the cq5dam one, ie - the part after the OR is what I want it to use as a fallback and it does indeed seem to work, ie - it returns the rc-details-thumbnail.png if it exists and the cq5dam one otherwise. I'm not that well versed in regexes so hopefully this is a consistent and reliable result.

That being said, when also using a InternalRedirectRenditionDispatcherImpl config that maps a rendition name to a mapping like

"rendition.mappings": [
    "card=${asset.path}.renditions/results-thumb/asset.rendition",
]

Where card acts as a rendition name that maps to a results-thumb static rendition mapping. It's used in the AssetRenditionImpl computed property from places like the card.html results component template

<img src="${asset.properties['rendition?name=card'] || properties['missingImage'] @ context = 'attribute'}">

In this case, card should map to a static rendition mapping using a similar StaticRenditionDispatcherImpl as shown above. In this case however, the OR does NOT work. So creating the config like

"rendition.mappings": [
    "results-thumb=^(results-thumbnail(\\..+)?)|(cq5dam\\.web\\.\\d+\\.\\d+\\..+)$",
]

Always returns the cq5dam rendition instead of the results-thumbnail.png thumbnail when the processing starts with a InternalRedirectRenditionDispatcherImpl config...

Is there a reason for that? Is this pure coincidence?

the way the static rendition dispatcher works is

  1. For the asset in question
  2. It gets a list of all the rendition nodes for that asset under .../some-asset.png/jcr:content/renditions
  3. and iterates over each of the children of that renditions node in their natural JCR order (the order youd see in CRXDE)
  4. For each rendition node name, it sees if the regex matches it
  5. It returns the first rendition name that matches the provided regex

So your regex works because your fallback appears to be always checking the rc-... rendition and finding it before continuing on and finding the cq5dam.web... rendition. The order the rendtiions are checked is what its determining which is found first.

I see thanks for that explanation @davidjgonzalez! Unfortunately, I'm not sure controlling the order is something that's going to be easy to do (I don't think it's controllable from the Assets module, we'd have to go through crx/de) and therefore this method will remain unpredictable.

Are there better ways to create fallback renditions if the one we are specifying isn't found?

@DigiLiviu if its for dispaly on the asset share, you can use the img tags onerror event to set the src to something else if the original SRC doesnt return a 200 .. otherwise there's not a great way to do this :(