Templating... templating is a challenging one that I don't think has been solved sufficiently. Handlebars is a natural fit, but it misses the mark in certain contexts. If I'm doing a new AEM project today, I'm using HBS, but I'm yearning for something better.
The dust has settled a bit since that post:
- Sling Model Exporters have been shipping for a year.
- Content Fragments are now naturally exposed as JSON.
- Core Components 2.0 are now backed by Sling Model Exporters.
- React has gone MIT.
We're still in a bit of a holding pattern, though. We likely still need to rely on HTL (server-side rendering) in 2018. Especially if we want 100% global SEO coverage. So what do we want from an SPA framework?
Ideal SPA Requirements
- 100% global SEO coverage
- Little-to-no Flashes Of Unstyled Content (FUOC)
- Front-end developer friendly... community momentum, documentation, hire-ability, etc.
- Mix and match with existing HTL components
Pick two? Maybe?
Enter React DOM Components
How does this look in AEM land?
At a high level, like this:
- The Sling Model (with Exporter) powers both server-side and client-side rendering.
- The HTL component is built the same way as always (but with a custom element name for efficiency)
- A DOM Model is built to define the attributes, nodes, and content to be used from DOM.
- A DOM Component is built to find the custom element, build the data model, and pass that data to a React Component.
- The React Component is just a vanilla React Component.
- A DAO class is built to fetch data from the Sling Model Exporter. Click a button? The React Component tells the DAO to fetch the data and update the state.
The big down-side to this process is that we're essentially building two models and two views. It's not ideal, but it lets each team (FE & BE) continue to build in ways they are familiar with. A back-end developer can still work in Java. A front-end developer who has never used AEM can work in almost 100% pure React the day they start the job.
React DOM Components are also AEM-friendly. They know when editor.html has mutated a page for authoring and pushes the state to the React Component automatically.
Step 1 - Sling Model
Step 2 - TabControl HTL Component
Step 3 & 4 - TabControlDOM & TabControlModel
Step 5 & 6 - TabControl React Component with DAO
The React Component and Data Access Object (DAO) have been combined for demonstration simplicity.
What is outlined above is not the only way to integrate AEM with React. There is a separate project using Typescript and Nashorn to achieve something similar. For my purposes, this is a little more opinionated and heavy handed than I like, hence the process above.
A Full Project
All the source with a sample implementation can be found on GitHub. This project is what I'm calling "Pug Ranch 2018" which is a reference implementation of AEM Archetype 12 plus a few front-end niceties to create a "tab-control" component using HTL and React.