移动网页加速器百度MIP无需等待加载

MIP (Mobile Instant Pages - 移动网页加速器)主要用于移动端页面加速。
这篇文档将带你快速创建一个 MIP 页面。

移动网页加速器百度MIP无需等待加载-上流阁

1. 创建 HTML 文件
首先创建一个标准的 HTML 文件, 注意:

在<html>标签中增加mip标识
编码为 utf-8
添加meta-viewport,用于移动端展现

<!DOCTYPE html>
<html mip>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
<title>Hello World</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>

2. 添加MIP运行环境
在 HTML 代码中,添加MIP依赖的mipmain.js和mipmain.css。 使用最新版本的文件能够保证组件的正常使用,可以在这里查看mipmain.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/mipmain-v1.1.1.css">
<title>Hello World</title>
</head>
<body>
<h1>Hello World!</h1>
<script src="https://mipcache.bdstatic.com/static/mipmain-v1.1.2.js"></script>
</body>
</html>

3. 添加 MIP 关联标签
<link rel="miphtml">和<link rel="canonical">主要用于告知搜索引擎页面间的关系。

在MIP页中使用<link rel="canonical">, href 指向原页面, 若只有 MIP 页,则指向自己。
如果MIP页和原页面同时存在,则在原页面中添加<link rel="miphtml">指向MIP页。MIP页的点击会与原页面的点击权重加和计算,同时原页面将作为搜索引擎的首选页。

<!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/mipmain-v1.1.1.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/mipmain-v1.1.2.js"></script>
</body>
</html>

4. 添加样式
出于速度考虑,建议內联使用 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/mipmain-v1.1.1.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/mipmain-v1.1.2.js"></script>
</body>
</html>

5. 替换禁用 HTML 标签
MIP十分关注页面速度,也因此禁用了一些引起拖慢速度的html标签(禁用列表)。例如,<img>标签会引起浏览器的repaint和reflow,为了避免这些,MIP提供了替代标签<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/mipmain-v1.1.1.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/mipmain-v1.1.2.js"></script>
</body>
</html>

6. 使用MIP组件
出于对代码质量和性能的考虑,MIP页中不允许自定义javascript代码,所有的交互通过引入MIP组件实现。MIP组件可以理解为封装了js的自定义html标签。上一步中的<mip-img>也是一个MIP组件。

我们以分享组件为例,根据分享组件文档,组件对应的html标签为<mip-share>,需要依赖//mipcache.bdstatic.com/static/v1.1/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/mipmain-v1.1.1.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/mipmain-v1.1.2.js"></script>
<script src="https://mipcache.bdstatic.com/static/v1.1/mip-share.js"></script>
</body>
</html>

在使用组件时,请注意阅读组件文档,查看组件是否依赖额外脚本。如果依赖,请在mipmain.js之后引入脚本。

7. 预览
开发完成后,可以使用MIP校验工具保证代码规范。

MIP页文件可以直接运行,你可以选择如下方式,像预览普通HTML站点一样预览 MIP HTML 页面:

直接在浏览器中打开(由于XML Http Requests失败可能会导致某些元素预览失败)
在本地部署一个服务,如apache,nginx等
8. 起飞
到目前为止,你已经创建好了一个MIP页面。这个页面有图,有文,能分享,可以在浏览器中运行。

*文章为作者独立观点,不代表上流阁立场
本文由 江风成 授权 上流阁 发表,并经上流阁编辑。转载此文章须经作者同意,并请附上出处(上流阁)及本页链接。原文链接https://www.o6c.com/web/2016/11/07/662.html
发表评论

坐等沙发
相关文章
WEB前端Nodejs升级高版本启动错误ERESOLVE unable to resolve dependency tree
WEB前端Nodejs升级高版本启动错误ERESOL…
GoAccess轻量nginx日志分析工具与中文可视化goaccess-1.6.2.tar
GoAccess轻量nginx日志分析工具与中文可…
HTML中Data URI scheme BASE64 文件的拼接头
HTML中Data URI scheme BASE64 文件的拼…
WEB前端 解决监听浏览器窗口关闭之onbeforeunload不起作用?
WEB前端 解决监听浏览器窗口关闭之onbef…
VUE el-input__suffix样式大小修改
VUE el-input__suffix样式大小修改
VUE el ui el-date-picker时间禁用范围禁用干货
VUE el ui el-date-picker时间禁用范围…
javaweb开发程序员php开发,微信开发。接受定制开发

最新评论