收藏444
分享
阅读14027
更新时间2022-04-11
MIP (Mobile Instant Pages - 移动网页加速器)主要用于移动端页面加速。
这篇文档将带你快速创建一个 MIP 页面。
首先创建一个标准的 HTML 文件, 注意:
<html>标签中增加mip标识在 HTML 代码中,添加MIP依赖的mip.js和mip.css。
<!DOCTYPE html> <html mip> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1"> <link rel="stylesheet" type="text/css" href="https://mipcache.bdstatic.com/static/v1/mip.css"> <title>Hello World</title> </head> <body> <h1>Hello World!</h1> <script src="https://mipcache.bdstatic.com/static/v1/mip.js"></script> </body> </html>
<link rel="miphtml">和<link rel="canonical">主要用于告知搜索引擎页面间的关系。添加关联标签后,MIP页的会继承原页面(移动端)的点击权重,同时MIP页将作为搜索引擎的首选导流页面。
使用规则:
<link rel="canonical">在MIP 页中使用,<link rel="miphtml">在原页面使用。<link rel="canonical">标签指向PC页,则MIP页<link rel="canonical">的 href 也指向PC页。MIP页没有对应的原页面,则指向MIP页本身url。<!DOCTYPE html> <html mip> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1"> <link rel="stylesheet" type="text/css" href="https://mipcache.bdstatic.com/static/v1/mip.css"> <link rel="canonical" href="//www.mipengine.org/"> <title>Hello World</title> </head> <body> <h1>Hello World!</h1> <script src="https://mipcache.bdstatic.com/static/v1/mip.js"></script> </body> </html>
出于速度考虑,建议內联使用 css 样式。所有样式写在<style mip-custom></style>中,注意:style 标签仅允许出现一次。
<!DOCTYPE html>
<html mip>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
<link rel="stylesheet" type="text/css" href="https://mipcache.bdstatic.com/static/v1/mip.css">
<link rel="canonical" href="//www.mipengine.org/">
<title>Hello World</title>
<style mip-custom>
h1 { color: red;}
</style>
</head>
<body>
<h1>Hello World!</h1>
<script src="https://mipcache.bdstatic.com/static/v1/mip.js"></script>
</body>
</html>
MIP十分关注页面速度,也因此禁用了一些引起拖慢速度的html标签(<img>标签会引起浏览器的repaint和reflow,为了避免这些,MIP提供了替代标签<mip-img>。详见<mip-img>使用文档
<!DOCTYPE html>
<html mip>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
<link rel="stylesheet" type="text/css" href="https://mipcache.bdstatic.com/static/v1/mip.css">
<link rel="canonical" href="//www.mipengine.org/">
<title>Hello World</title>
<style mip-custom>
h1 { color: red;}
</style>
</head>
<body>
<h1>Hello World!</h1>
<mip-img src="https://www.mipengine.org/static/img/mip_logo_3b722d7.png"></mip-img>
<script src="https://mipcache.bdstatic.com/static/v1/mip.js"></script>
</body>
</html>
出于对代码质量和性能的考虑,MIP页中不允许自定义javascript代码,所有的交互通过引入MIP组件实现。MIP组件可以理解为封装了js的自定义html标签。上一步中的<mip-img>也是一个MIP组件。
我们以分享组件为例,根据<mip-share>,需要依赖//mipcache.bdstatic.com/static/v1/mip-share/mip-share.js脚本,用在页面里就是这样:
<!DOCTYPE html>
<html mip>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
<link rel="stylesheet" type="text/css" href="https://mipcache.bdstatic.com/static/v1/mip.css">
<link rel="canonical" href="//www.mipengine.org/">
<title>Hello World</title>
<style mip-custom>
h1 { color: red;}
</style>
</head>
<body>
<h1>Hello World!</h1>
<mip-img src="https://www.mipengine.org/static/img/mip_logo_3b722d7.png"></mip-img>
<mip-share title="分享:我的第一个MIP页面"></mip-share>
<script src="https://mipcache.bdstatic.com/static/v1/mip.js"></script>
<script src="https://mipcache.bdstatic.com/static/v1/mip-share/mip-share.js"></script>
</body>
</html>
在使用组件时,请注意阅读组件文档,查看组件是否依赖额外脚本。如果依赖,请在mip.js之后引入脚本。
开发完成后,可以使用
MIP页文件可以直接运行,你可以选择如下方式,像预览普通HTML站点一样预览 MIP HTML 页面:
到目前为止,你已经创建好了一个MIP页面。这个页面有图,有文,能分享,可以在浏览器中运行。
进阶的内容,请参考
相关
视频
RELATED VIDEOS
科技资讯
1
2
3
4
5
6
7
8
9
精选课程
共5课时
17.3万人学习
共49课时
77.4万人学习
共29课时
62万人学习
共25课时
39.5万人学习
共43课时
71.3万人学习
共25课时
61.9万人学习
共22课时
23.1万人学习
共28课时
34.1万人学习
共89课时
125.8万人学习