March 22, 2020
Estimated Post Reading Time ~

AMP with AEM - Demonstrating the ability of Content Fragments in AEM

AMP integration with AEM - CaaS Model(Content as a service) demo
I was receiving requests for writing sample code to showcase a Third-party AMP page that retrieves data from AEM (Content as a service).

What is AMP?
The AMP Project is an open-source Google-run website publishing technology initiative aiming to make the web better for all. The project enables the creation of websites and ads that are consistently fast, beautiful, and high-performing across devices and distribution platforms. More details on https://www.ampproject.org/

How can I develop AMP websites?
You need to refer to the above URL for implementing AMP Projects, which has detailed information about the syntax of AMP.

AMP integration with AEM
Step 1: To work with AMP and AEM, create an AMP file as shown below and deploy it on any server.

A sample AMP Structure is given below.
Save the below file as 'Sample_AMP.html'

<!doctype html>
<html ⚡>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
<title>AMP demo with AEM</title>
<link rel="canonical" href="index.html" />
<script async src="https://cdn.ampproject.org/v0.js"></script>
<script async custom-element="amp-list" src="https://cdn.ampproject.org/v0/amp-list-0.1.js"></script>
<script async custom-template="amp-mustache" src="https://cdn.ampproject.org/v0/amp-mustache-0.2.js"></script>
<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style>
<noscript>
<style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style>
</noscript>
</head>
<body>
<amp-list width="auto"
height="100"
layout="fixed-height" src="test.json">
<template type="amp-mustache">
<div class="url-entry">
<a href="{{url}}">{{title}}</a>
</div>
</template>
</amp-list>
</body>
</html>

It takes a JSON input file(src="test.json") and publish data on page.

Step 2: Author a content fragment in AEM which has JSON format as below.
{
"items": [
{
"title": "AMP YouTube",
"url": "https://www.youtube.com/"
},
{
"title": "AMPproject.org",
"url": "https://www.ampproject.org/"
},
{
"title": "AEM Tutorial Blog",
"url": "http://aem-cq-tutorials.blogspot.com/"
},
{
"title": "Solr with AEM Totorial",
"url": "http://aemsolr.blogspot.in/"
}
]
Step 3: Now, update the 'Sample_AMP.html' for src="test.json" with publishing URL of content fragment authored page
Step 4: Ensure CORS is enabled in AEM.
Step 5: Now invoke the AMP file, from the server, and you can see that data is appearing as shown below.


Notes:
AMP - uses specific tags and syntax which need to be validated after each change in code. You can use an online AMP validator(https://validator.ampproject.org/) for this purpose.


By aem4beginner

No comments:

Post a Comment

If you have any doubts or questions, please let us know.