AMP技术说明
AMP是一个在Github上的开源项目,全称Accelerated Mobile Pages。 顾名思义是为了加速移动网络的网页加载从而提升体验。 AMP是一套解决方案主要有3大核心组成:
1. AMP HTML
AMP HTML 本质上是使用自定义 AMP 标签&属性扩展的 HTML。通过这些自定义 AMP 标签&属性,可以确保可靠性能。
2. AMP js
AMP JS 库主要做了以下工作:
- 管理资源加载
- 实现 AMP HTML 自定义标签
- 最佳性能优化做法,比如 iframe 沙盒化、预先计算所有元素的布局、禁用性能缓慢的 CSS 选择器等等
另外,AMP 页面不允许使用第三方脚本。
3. AMP Cache
AMP Cache 是用来缓存所有 AMP 页面相关资源的基于代理的 CDN。
可能说的有点抽象,实际上就是在 CDN 网络加了一层代理,专门用来处理 AMP 页面的缓存。
实际上,AMP Cache 主要做的是:
- 实时处理:接受 AMP 页面的资源加载请求,分析相关路径,获取对应的缓存资源,如果没有缓存,则到真实的服务器获取资源
- 离线处理:处理爬虫爬到的 AMP 页面,分析页面上的资源,进行相应的路径替换以及缓存
AMP应用说明
因为AMP不允许使用第三方脚本,页面的交互,动画是通过AMP Component来实现的,并且css只能内嵌,对开发十分不友好,所以AMP更多的适用于简单的静态页面(如公司官网、博客、广告页、邮件等),不适合交互复杂的页面。 但是AMP可以与原型网页互相引用,可以对部分展示页面进行AMP页面的开发。
AMP改造简单页面实例
一般html:
<!doctype html>
<html lang="en">
<head>
<link rel="shortcut icon" href="amp_favicon.png">
<title>News Article</title>
<link href="base.css" rel="stylesheet" />
<script src="base.js"></script>
</head>
<body>
<header>
News Site
</header>
<article>
<h1>Article Name</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam egestas tortor sapien, non tristique ligula accumsan eu.</p>
<img src="mountains.jpg">
</article>
</body>
</html>
AMP改造后:
<!doctype html>
<!-- html标签设置⚡或者amp,必要 -->
<html ⚡ lang="en">
<head>
<!-- 视口的设置,必要 -->
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- 编码方式设置,必要 -->
<meta charset="utf-8">
<!-- 申明与原型页面的引用关系 -->
<link rel="canonical" href="./article.html">
<script async src="https://cdn.ampproject.org/v0.js"></script>
<link rel="shortcut icon" href="amp_favicon.png">
<title>News Article</title>
<!-- Contain the AMP boilerplate code in their <head> tag. -->
<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>
<!-- 无法引用css文件,只能使用下面的方法进行内嵌css的编写 -->
<style amp-custom>
body {
width: auto;
margin: 0;
padding: 0;
}
header {
background: Tomato;
color: white;
font-size: 2em;
text-align: center;
}
h1 {
margin: 0;
padding: 0.5em;
background: white;
box-shadow: 0px 3px 5px grey;
}
p {
padding: 0.5em;
margin: 0.5em;
}
</style>
<!-- 搜索元数据的设置,提供给搜索引擎 -->
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "NewsArticle",
"mainEntityOfPage":{
"@type":"WebPage",
"@id":"https://example.com/my-article.html"
},
"headline": "My First AMP Article",
"image": {
"@type": "ImageObject",
"url": "https://example.com/article_thumbnail1.jpg",
"height": 800,
"width": 800
},
"datePublished": "2015-02-05T08:00:00+08:00",
"dateModified": "2015-02-05T09:20:00+08:00",
"author": {
"@type": "Person",
"name": "John Doe"
},
"publisher": {
"@type": "Organization",
"name": "⚡ AMP Times",
"logo": {
"@type": "ImageObject",
"url": "https://example.com/amptimes_logo.jpg",
"width": 600,
"height": 60
}
},
"description": "My first experience in an AMPlified world"
}
</script>
</head>
<body>
<header>
News Site
</header>
<article>
<h1>Article Name</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam egestas tortor sapien, non tristique ligula
accumsan eu.</p>
<!-- 页面标签的改变及布局方式的改变 -->
<amp-img layout="responsive" width="266" height="150" src="mountains.jpg"></amp-img>
<!-- 无法使用js,只能通过amp组件实现交互 -->
</article>
</body>
</html>
参考: Accelerated Mobile Pages Foundations Beautiful, interactive, canonical AMP pages AMP Dev