惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

www.infosecurity-magazine.com
www.infosecurity-magazine.com
D
DataBreaches.Net
T
Tailwind CSS Blog
M
MIT News - Artificial intelligence
Stack Overflow Blog
Stack Overflow Blog
F
Full Disclosure
V2EX - 技术
V2EX - 技术
N
News and Events Feed by Topic
Help Net Security
Help Net Security
L
LangChain Blog
Y
Y Combinator Blog
宝玉的分享
宝玉的分享
Google Online Security Blog
Google Online Security Blog
P
Proofpoint News Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
The Blog of Author Tim Ferriss
Google DeepMind News
Google DeepMind News
The Register - Security
The Register - Security
B
Blog RSS Feed
N
Netflix TechBlog - Medium
N
News | PayPal Newsroom
TaoSecurity Blog
TaoSecurity Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
V
Vulnerabilities – Threatpost
B
Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
I
Intezer
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园_首页
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
AI
AI
aimingoo的专栏
aimingoo的专栏
大猫的无限游戏
大猫的无限游戏
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Cyberwarzone
Cyberwarzone
P
Proofpoint News Feed
Google DeepMind News
Google DeepMind News
G
GRAHAM CLULEY
Vercel News
Vercel News
罗磊的独立博客
MyScale Blog
MyScale Blog
Last Week in AI
Last Week in AI
博客园 - 司徒正美
C
CERT Recently Published Vulnerability Notes
GbyAI
GbyAI
Scott Helme
Scott Helme
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
T
Troy Hunt's Blog
A
About on SuperTechFans
P
Privacy International News Feed

James Guo’s Portfolio (@ZE3kr)

Hertz Rental Debt Collection and How I Sued the Debt Collector macOS on iPad? (with Jump Desktop) Vision Pro Initial Experience | ZE3kr The Downtime of Alibaba Cloud Hong Kong Region How Fast is mmWave 5G? 2000Mbps! See if Your iPhone Supports mmWave Magic Keyboard for iPad Pro Experience Self-built PowerDNS Advanced: GeoDNS, dnsdist, Lua Records SSD USB, Windows To Go and Mac WordPress 5.0 Update is Available, New Editor, New Theme Azure DNS, NS1, Constellix, Comparison of Three International GeoDNS Service Providers Recommendations for Some Exquisite and Useful Software on Mac Talk About Data Backup | ZE3kr Introduction to DNSSEC, How Signatures Work Cloudflare Argo And Railgun Comparison Test, The technology of CDN Acceleration Recommendations For Several Free Server Monitoring Services Comparison of Several Full-Site CDNs 2017, Another Look at SSL and HTTPS Some suggestions on website construction and service purchase Detailed Explanation of DNS Domain Name Resolution System - Basics Cloudflare's new feature experience - Load Balancing, Rate Limiting Back to WordPress' Built-In Comment System Implementing ECDSA/RSA Dual Certificate with Free Let's Encrypt How to configure for pure IPv6-Only network access Self-built PowerDNS GeoDNS Server | ZE3kr Install GitLab on Your Own Server Instead of GitHub! A Few WordPress Optimization Suggestions Comprehensive Comparison of DNS Services such as CloudXNS, Route 53, and Alibaba Cloud DNS Talk about the streaming of video on the Internet Canon Announces New APS-C Model EOS 80D, Powerful Video Recording, New Focus System and CMOS Several recommended plugins for WordPress Using Matomo with WordPress to Build a Powerful Statistics System TL-PA500 power cat, deploy wireless LAN cluster Why use a CDN service? The pros and cons of static web pages HTTPS Everywhere is Coming | ZE3kr Withings Activité Pop smartwatch recommendation, sleep tracking, exercise recording, smart alarm clock DELL P2415Q 4K Monitor Recommended Use srcset + sizes attributes and w identifier to solve all responsive image problems Eyefi Mobi makes your camera Wi-Fi ready now Mobile First - Speed | ZE3kr MacID Lets iPhone Unlock Apple's Mac with Touch ID ProCamera – Recommended iPhone camera software Mobile First - Origins | ZE3kr
Build Blazing Fast Mobile Pages With AMP
2016-10-16 · via James Guo’s Portfolio (@ZE3kr)

AMP (Accelerated Mobile Pages) is an open source project of Google. By using AMP HTML, can significantly improve the loading speed of web pages. When I was using Google on mobile devices recently, I noticed that a lot of sites started using AMP. Getting to an AMP page from Google is incredibly fast, unbelievably fast—with almost zero latency. This site is also configured with AMP. This article analyzes some features of AMP, and introduces how to support AMP on WordPress and develop plugins to customize it.

AMP HTML

AMP HTML is also a kind of HTML, a standard compliant AMP page can only use limited tags, and many tags are not HTML standard, such as <amp-img>, <amp-video>, etc. An example of Hello, AMPs:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<!doctype html>
<html amp lang="en">
<head>
<meta charset="utf-8">
<script async src="https://cdn.ampproject.org/v0.js"></script>
<title>Hello, AMPs</title>
<link rel="canonical" href="http://example.ampproject.org/article-metadata.html" />
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "NewsArticle",
"headline": "Open-source framework for publishing content",
"datePublished": "2015-10-07T12:02:41Z",
"image": [
"logo.jpg"
]
}
</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>
<h1>Welcome to the mobile web</h1>
</body>
</html>

This code can be run directly in major browsers. Compared with normal HTML, it has the following differences:

  • AMP HTML adds the amp attribute to <html> to mark this as an AMP page
  • A JS must be referenced in <head>: <script async src="https://cdn.ampproject.org/v0.js"></script>
  • Requires style with amp-boilerplate attribute, one of which is in <noscript>.
  • <head> needs to have a <script type="application/ld+json"> to mark the metadata of the document.

Example of inserting an image in AMP HTML:

1
<amp-img src="450px.jpg" srcset="450px.jpg 450w, 1200px.jpg 1200w"></amp-img>

You will find that this is actually no different from the syntax of the ordinary img tag, except that the name is changed. In fact, by introducing the image in this way, AMP Runtime (that is, the v0.js referenced in the header) will automatically parse this AMP Components, and will select an image to load according to the resolution of the device (the principle is just to create an img tag through js to let the browser recognize it), and it does not even need the browser to support [srcset](https:/ /guozeyu.com/2015/08/using-srcset/) attribute; and can automatically lazy load images. Inserting video and audio also needs to be done using AMP-specific methods. If JS is disabled in the browser, all images and videos on the page that use non-standard HTML tags will not be displayed.

Introducing More Interactions in AMP

Any own JS is forbidden in AMP, this is just to keep the JS loading fast - since mobile devices are not very performant, minimizing the use of JS or avoiding the use of low quality JS can improve the user experience. So, if you want to use more interaction, you have to do it through features of AMP HTML Extensions, so you can only use Limited interactive functionality. Or you can implement various complex functions through pure CSS.

Verify AMP

Validation of AMP pages is available online at this site

Why So Fast?

You will actually find that the only content in the page that may block loading is an external JS (that is, AMP Runtime), and this JS is also marked as asynchronous loading. All CSS is Inline.

Implement AMP on Your Own Website

To support AMP, each article needs to have two pages: a normal version of the page, and an AMP-compliant AMP page. Of course, it is also possible to make the page conform to the AMP specification by directly modifying the page itself, but this method will make more changes. The easiest way to implement AMP is through a plugin, especially if you’re using WordPress:

Implementation on WordPress

Automattic (actually WordPress official) has made an AMP plugin, after installing and activating this plugin, no configuration is required - this is one of the reasons I like this plugin One, you can activate the AMP function. This plugin will automatically generate the corresponding page for each article and add AMP metadata to the original page accordingly. After installation, there is no change on the surface, but when you add /amp/ or ?amp=1 after an article page, you can see the AMP page corresponding to the article. The reason why this plugin is free of any configuration is that what it actually provides is just a framework. Users (actually developers) can develop a plugin to customize it. The official has given a [specific introduction] (https://github.com/Automattic/amp-wp/blob/master/readme.md), I list some things I customized:

Add Custom Statistics

AMP already supports Google Analytics, but if you need to add your own statistics, you can’t use JS, but the easiest way is to add an empty Gif:

1
2
3
4
5
6
7
8
add_action( 'amp_post_template_footer', 'tlo_amp_add_pixel' );
function tlo_amp_add_pixel( $amp_template ) {
//$post_id = $amp_template->get( 'post_id' );
$piwik_id = $GLOBALS\['wp-piwik'\]->getOption( 'site_id' );
?>
<amp-pixel src="https://piwik.example.com/piwik.php?idsite=<?php echo $piwik_id; ?>&rec=1&action_name=TITLE&urlref=DOCUMENT_REFERRER&url=CANONICAL_URL&rand=RANDOM"></amp-pixel >
<?php
}

This is an example of using the Piwik tool for statistics, and with the WP-Piwik plugin, it can automatically obtain the site ID (requires manual replacement of the domain name).

Field Test (Video)

After activating AMP for a period of time, using mobile devices to search for website content on Google can benefit significantly from AMP, the right side of the video contains a list of resources, using the Safari browser that comes with iOS 10 (do not Blink!):

It can be seen that while still staying in the search results, AMP content such as AMP Runtime has already started to download, and at the same time, the logo on my website and the first image in the webpage have also started to download. When the first search result is clicked, the page does not show any signs of reloading, and is directly presented in a similar way to ajax. At this time, the entire page has been preloaded, so the loading time is almost zero (due to the use of the iOS simulator, The configuration is lower, and the actual real machine test white screen time is shorter, it can be said that there is no perception). The URL domain name can be seen at the top of the page. When scrolling the page, you can see that the next few pictures are lazy loaded, and the delay loading time is just right, it is almost difficult to feel that it is lazy loading. It is worth noting that almost all the content of this page is provided by Google’s servers, including HTML and images (after testing, the video resources are still back to the source), only my statistical code is not proxied by Google. If the URL is shared at this time, the shared domain name is still Google’s, and when it is opened (or after the page is refreshed) the upper left corner will no longer be closed, and the rest is exactly the same. If this URL is opened on a non-mobile device, it will redirect to the original article (not AMP) page. After clicking the close button in the upper left corner, you will return to the search results page, and the whole process is very smooth. If the search results have multiple AMP pages, then entering those other pages will also take seconds. As you can imagine, a large part of the reason why AMP has so many restrictions is to prevent website owners from “doing evil” when rendering AMP pages on google.com, even if the browser is not controlled by Google (such as Safari).

Summarize

Google’s AMP feature is very similar to Facebook’s Instant Article, except that the latter doesn’t even require its own server hosting. A large amount of CSS in AMP can still be controlled by itself, which is much better than the [transcoding page] (https://www.guozeyu.com/2015/08/block-haosou/) of most domestic search engines. Pages can still serve their own ad content, which is why AMP has been accepted so quickly. It also retains a lot of open stuff in various non-open restrictions. Enabling AMP can not only benefit from loading in Google search result pages, AMP pages themselves can actually be used as pages specially adapted for mobile devices - if the website itself does not have a mobile version page, then the mobile version page can be directly made with AMP, Then jump automatically. Of course, as I said before, web pages can be made to conform to the AMP specification by directly modifying the page itself, or it can be made in accordance with the AMP specification at the beginning of production. AMP official website is a Classic example. However, this approach is too radical, and the compatibility is not very good (such as its strong dependence on JS), so it should be fully considered before use.

Supplement about MIP

Baidu borrowed many technologies from AMP and made MIP, which is very similar to AMP. After a simple experiment, I found that AMP can be easily changed to MIP, just need to replace or delete the following content on the AMP page:

  • <html amp replaced with <html mip
  • <script src="https://cdn.ampproject.org/v0.js" async></script> replace with <link rel="stylesheet" type="text/css" href ="https://mipcache.bdstatic.com/static/v1/mip.css">
  • amp-pixel replaced with mip-pix
  • amp- ** replaced by ** mip-
  • </body> replaced with <script src="https://mipcache.bdstatic.com/static/v1/mip.js"></script></body>
  • delete <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>

The modified AMP page can directly pass the MIP test, which is equivalent to being compatible with both AMP and MIP without secondary development. How convenient! MIP-enabled websites are also preloaded on Baidu search results, so they can be opened in seconds.

Some pits of MIP

Differences in CSS

The CSS of MIP comes with actually declares this style:

1
2
3
4
* {
margin: 0;
padding: 0
}

Because of its existence, it will cause typography problems in many places, and will override the browser’s default styles (such as h1, h2 tags), so you may also add some styles for Baidu separately.

video tag bug

After testing, the <mip-video> tag cannot support the <source> tag, and videos using this tag cannot be loaded.