AMP components lack a template rendering solution
See original GitHub issueBackground
amp-story
creates some components with a lot of dynamic DOM that gets built manually or with an arbitrary single-render solution we call SimpleTemplate
s.
amp-lightbox-gallery
runs into similar issues of manipulating/creating a lot of DOM manually.
This is problematic:
-
Template rendering for amp-story is bloated: allocating memory for an arbitrary strings/object hierarchy and then recursively navigating it.
-
Manually writing DOM manipulation, templating logic and arbitrary formats is productivity-prohibiting.
Using
innerHTML
is fine for cases where the DOM content is static, but that can lead to changes to make the content dynamic. That’s a slippery slope into security holes. See #11633 for a nightmare PR. -
Other templating systems are used on AMP at the authorship level: mustache, JSX. These can benefit from a unified templating output for SSR, etc.
Goal
- Reduce the overhead of template rendering on
amp-story
and possibly other components. - Define a single templating system to be used internally.
- Make AMP component rendering first-class to make AMP more “app-like”.
Proposal
-
Implement a unified templating solution for AMP components to build/manipulate complex DOM, which gets compiled into a performant output.
- The input language should be constant. Consider lit-html (easiest), handlebars, mustache, JSX, etc. The language should be statically analyzable, so the simpler the better (i.e. JSX is difficult to statically analyse due to its dynamic nature and use of JS syntax).
-
The output performance can be improved over time:
-
We can easily output semi-dynamic templates into vanilla DOM transforms now if we do some refactoring of
amp-story
/amp-lightbox-gallery
. -
For complex components that manage a lot of state (i.e.
amp-story
) consider a more robust workflow like Svelte. -
Output format can be leveraged in SSR by other components in AMP: authorship-level templating, etc
-
-
Reduces code complexity: fewer instances of imperative manipulation, proprietarily described templates, etc.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:2
- Comments:5 (4 by maintainers)
@kristoferbaxter sketched out a design that looks pretty great. Some additions/changes mine:
Assignment of
MyTemplate
then gets compiled into something like:What’s cool about this:
Thanks both for commenting, I think @kristoferbaxter tightened the loose ends.
/cc @cathyxz for
amp-lightbox-gallery
, @cvializ foramp-date-picker