Skip to content

Data & Templates

Dynamic Pages

A dynamic page is one page that serves many URLs. A developer writes a filter that recognises a family of URLs and pulls values out of them; an author picks that filter on one page in the site. Every matching URL then renders through that page, with the values handed to its components. A site with ten thousand products needs one page, not ten thousand.

01 What a dynamic page is

Kestros routes a URL dynamically only when nothing else claims it. A dynamic page filter watches GET requests under /content/sites/ (authoring) and /public/sites/ (published), and steps aside the moment the request path resolves to a real resource, so an existing page is never intercepted. Dynamic routing is what happens instead of a 404, not instead of your content.

Two halves make it work, and they belong to different people:

  • The developer writes a filter in Java: which URLs it answers, what it extracts from them, and the name it is known by.
  • The author chooses one page in the site to render those URLs (the template page; not a Page Template), by picking the filter from a dropdown on that page.

The filter never names its page, and the page can be renamed or moved without breaking anything: the link between them is a single property on the page, dynamicPageType.

02 Writing a filter

A filter extends AbstractDynamicPageFilter, which does the resolving, the page lookup and the dispatch. You supply three methods and two annotations.

TeamPageFilter.java
@Component(service = {Filter.class, DynamicPageFilter.class})
@SlingServletFilter(scope = SlingServletFilterScope.REQUEST,
    pattern = "/(?:content|public)/sites/.*/teams/.*\\.html",
    methods = "GET")
public class TeamPageFilter extends AbstractDynamicPageFilter {

  @Override
  public String getDisplayName() {
    return "Team Detail";          // what authors see in the dropdown
  }

  @Override
  public String getFilterType() {
    return "my-site-team";         // the value stored on the page
  }

  @Override
  public Map<String, String> extractParameters(String requestPath,
      BaseSite site, SlingHttpServletRequest request) {
    String suffix = requestPath.substring(site.getResource().getPath().length());
    if (!suffix.startsWith("/teams/")) {
      return null;                     // not ours: the request carries on untouched
    }
    String slug = suffix.substring("/teams/".length());
    Map<String, String> params = new HashMap<>();
    params.put("team", slug);
    params.put("dynamicPageTitle", slug);
    return params;
  }
}
PieceRequiredWhat it does
@Component(service = {Filter.class, DynamicPageFilter.class}) Yes Registers the class twice: as a servlet filter, so it sees requests, and as a DynamicPageFilter, so authors can pick it. Leave out the second and the filter runs but never appears in the dropdown.
@SlingServletFilter(pattern, methods = "GET") Yes Which request paths reach the filter at all. Cover both /content/sites/ and /public/sites/, or the published site answers 404. Otherwise keep the pattern as narrow as the URLs you serve; everything it matches pays for a resource lookup.
getFilterType() Yes The filter’s stable identifier. A page claims the filter by storing exactly this string in dynamicPageType, so changing it disconnects every page that claimed the old value.
getDisplayName() Yes The label in the author’s dropdown. Free to change.
extractParameters(requestPath, site, request) Yes Decides whether the URL is yours and what it carries. Return null to decline, and the request continues as if the filter were not there. Return a map and each entry becomes a request attribute on the rendered page. requestPath is the request URI cut at the extension. Selectors are not removed (riverton-fc.print.html arrives as …/riverton-fc.print), so strip them yourself.
dynamicPageTitle, dynamicPageDescription No Two reserved keys in the returned map. They become the page’s meta title and description, in place of the template page’s own, only when the template page’s resource type is (or extends) kestros/commons/components/kestros-dynamic-page. A standard kestros-base-page ignores them.
dynamicPageType (on the page) Yes A String property on the page’s jcr:content, holding a filter type. It is what the dropdown writes. A page without it is a standard page.
Look up your data in extractParameters. It is the one place you can still decline cleanly. If the team does not exist, return null and the visitor gets an ordinary 404 instead of an empty template.

03 Choosing the page, in site admin

The author connects a page to a filter with the Dynamic Page Filter dropdown. It is on the Create Page dialog, and for an existing page in the Edit Metadata dialog, opened from the page’s details screen. It lists every registered filter by its display name, and its empty value reads Standard page. Choosing Standard page clears the property and the page stops being dynamic.

  • One page per filter, per site. A filter that another page in the same site already claims is shown disabled, labelled with that page’s path. Save is refused on the server too, so the rule cannot be skipped.
  • Sites are independent. Two sites can each have their own page on the same filter.
  • The page name is yours. The filter finds its page by the property, not by name, so renaming or moving the page keeps it working.

04 How a request resolves

  • The request must match the filter’s @SlingServletFilter pattern, carry an extension, and start with /content/sites/ or /public/sites/. Anything else is passed straight on.
  • If the path resolves to a real resource, the filter does nothing at all.
  • The path is walked to find the containing site. No site, no routing.
  • extractParameters runs. null ends the filter’s part and the request carries on.
  • The page in that site whose dynamicPageType equals the filter type is found. If none claims it, the request carries on to the normal 404.
  • Each extracted value is set as a request attribute, and the page is included and written back as text/html.

The page lookup in step 5 is cached per site and filter type, and the cached page is re-checked on every request. Move the claim to another page and the next request follows it, with no restart.

05 The template page is hidden

The page an author picks is still a real page at a real URL. Because it now carries dynamicPageType, a direct .html request for it returns 404, so visitors never see it rendered with no values. Adding editMode=true to the URL shows it again, to anyone, published site included. It is not access control, so keep nothing private on the template page.

The hiding applies to the html extension only. A .json request for the same page is not hidden.

06 Reading a value on the page

Each entry from extractParameters is a request attribute on the rendered page, under its own key. A component reads it through its model or datasource. Here a squad-table datasource reads the team value:

SquadTableDataSource.java
if (getRequest() != null && getRequest().getAttribute("team") != null) {
  final String team = String.valueOf(getRequest().getAttribute("team"));
  // look the team up and build the rows
}

Write the component so it behaves sensibly when the attribute is absent, as it is when an author opens the template page with editMode=true.

07 A worked example

One team page serving every team in the site, using the filter above.

  • Deploy TeamPageFilter in your site’s bundle. Team Detail appears in the dropdown.
  • Create a page called team and choose Team Detail as its Dynamic Page Filter. Its jcr:content now carries dynamicPageType = my-site-team.
  • Put a component on it that reads the team attribute.
what each URL does
/content/sites/my-site/teams/riverton-fc.html   // 200: the team page renders, with team = riverton-fc
/content/sites/my-site/team.html                // 404, the template page is hidden
/content/sites/my-site/team.html?editMode=true  // 200 for anyone: the template, unfilled
Keep real pages off the dynamic URLs. If /my-site/teams/riverton-fc ever exists as a page, that URL renders the page and the filter steps aside. A page at /my-site/teams itself is fine: the filter checks the full path, not its prefix.

08 Limitations to design around

  • Only requests with an extension are routed. /my-site/teams/riverton-fc with no extension never reaches extractParameters.
  • A filter no page claims does nothing. Its URLs get the normal 404. The Dynamic Page Type Claims Check on site admin’s System Health page (/libs/kestros/site-admin2/health.html) lists every unclaimed filter.
  • Two pages claiming one filter is reported, not blocked at request time. The dropdown and the server both refuse it on save, but a content package, a migration, or any write that bypasses the Sling POST servlet can create it. One page is served; page validation and the health check both warn.
  • Hiding covers .html only. The template page’s .json stays reachable.
  • Don’t mix with route nodes. A site with a jcr:content/dynamic-routes node (older projects, and the archetype’s sample site, have one) answers 404 for any URL its routes don’t match, and that can happen before your filter sees the request.
  • Every value is a String. extractParameters returns Map<String, String>; anything richer is looked up again by the component that needs it.