AEM in 40 Days
CalendarPhase 5 · Advanced Development

Day 34 of 40

Advanced Development Techniques

Model parameters, image delivery, SDI, and deprecated APIs

~50 min read2 videos6 source pages

By the end of today you should be able to

  1. Parameterise a Sling Model from HTL to make components reusable
  2. Use web-optimised image delivery for responsive images
  3. Cache page variants safely without breaking personalisation
  4. Find and remove deprecated APIs before they break a deployment

Parameterising a Sling Model from HTL

Sometimes one model should behave slightly differently depending on where it is used — a list that shows five items here and ten there, without an author-facing dialog field. HTL can pass parameters into a model:

<sly data-sly-use.list="${'com.wknd.core.models.AdventureList' @ limit=5, tag='hiking'}"/>

The model receives them with @RequestAttribute:

@Model(adaptables = SlingHttpServletRequest.class,
       defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class AdventureList {

    @RequestAttribute
    @Default(intValues = 10)
    private int limit;

    @RequestAttribute
    private String tag;
}
Parameters, not content

Use this for developer-set variation between usages. If an author should decide the value, it belongs in the dialog or the policy — a request attribute is invisible to them and cannot be changed without a deployment.

Web-optimised image delivery

AEM as a Cloud Service can transform images on delivery — resizing, cropping and converting to modern formats such as WebP — without you storing every rendition. Core Components use it automatically when it is enabled, generating srcset markup for responsive images.

Java APIs let you build these URLs yourself when you need to:

@Reference
private AssetDelivery assetDelivery;

Map<String, Object> params = new HashMap<>();
params.put(AssetDelivery.WIDTH_PARAMETER, 1200);
params.put(AssetDelivery.FORMAT_PARAMETER, "webp");
params.put(AssetDelivery.QUALITY_PARAMETER, 80);

String url = assetDelivery.getDeliveryURL(assetResource, params);

The benefit is real: smaller images, fewer stored renditions, and delivery formats chosen per browser. The constraint is that it is a Cloud Service capability — code that depends on it needs a fallback if you also target 6.5.

Caching page variants

A page that renders differently per audience is at odds with a cache that stores one file per URL. There are three honest options:

  • Selector-based variants — encode the variant in the URL (page.member.html). Each variant caches separately. Simple and effective when the number of variants is small and known.
  • Client-side personalisation — cache one page and let JavaScript adapt it. Keeps the cache hit rate high; costs a flash of default content.
  • Sling Dynamic Include (SDI) — cache the page but mark specific components for dynamic inclusion, assembled by the Dispatcher via SSI or ESI. The page caches; the personalised fragment does not.
The failure mode is serving the wrong person's page

Caching a page that varies by user without accounting for it means one visitor's personalised content is served to everyone. Anything that varies per user — as opposed to per segment — should not be in a cached page at all.

Deprecated APIs

Cloud Service moves continuously, and APIs marked deprecated are eventually removed. A build that passes today can fail later, so finding them is maintenance you schedule rather than discover.

  • The aemanalyser Maven plugin (the analyse module) reports use of deprecated and removed APIs at build time.
  • Cloud Manager's code quality gate flags them too.
  • Common casualties: getAdministrativeResourceResolver, older WCMUsePojo patterns, /etc-based structures, and Java 8 assumptions.

Treat analyser warnings as a queue to work through rather than noise to suppress. Each one is a future deployment failure with a known cause.

Related odds and ends worth knowing

  • Running a job on the leader instance only — with multiple author instances, a scheduled job would otherwise run several times. The Sling discovery API identifies the leader so a job runs once.
  • Sling Dynamic Include also helps with expensive components regardless of personalisation.
Today's takeaway

Request attributes for developer-set variation; web-optimised delivery instead of stored renditions; selectors, client-side or SDI for variants; and work the deprecation queue before it works you.

Watch

Adobe's own videos for this topic. They load only when you press play.

Web optimized image delivery
Sling Dynamic Include - Implementation Guide

Read on Experience League

The primary sources these notes are drawn from.

Your notes

Saved automatically to this browser.

Check yourself

10 questions on today's material. 80% to pass.

Take the quiz