- AMP:Building Accelerated Mobile Pages
- Ruadhan O'Donoghue
- 210字
- 2021-07-08 10:03:57
What's with the <style amp-boilerplate> code?
You might be wondering why you need to include the <style amp-boilerplate> code in every page. Even AMP's creator and tech lead, Malte Ubl, described this code as an atrocity! Unfortunately, it's a necessary evil; a hack developed by the AMP project team to avoid the infamous flash of unstyled content while the AMP page is loading.
It works like this. First, the AMP page content is hidden while it loads. Then, when the AMP-JS library has loaded, it will unhide the page after it has finished rendering it. This presents a problem, however. If the AMP-JS library was ever unavailable when a user requested the page, then the page would stay blank forever. This would be an unacceptable user experience, even if it happened only rarely.
The trick, then, is to use a CSS keyframe animation as a timeout function: if the AMP-JS library fails to make the content visible, then the CSS animation will make it visible automatically after a few seconds, and the user will still get to see some content. A simplified version of this trick, without the vendor prefixes, is shown as follows:
body {
animation: amp-timeout 0s 8s 1 normal forwards;
}
@key-frames amp-timeout {
0% {opacity: 0;}
100% {opacity: 1;}
}
- Oracle從入門到精通(第3版)
- C++案例趣學(xué)
- 流量的秘密:Google Analytics網(wǎng)站分析與優(yōu)化技巧(第2版)
- Arduino開發(fā)實(shí)戰(zhàn)指南:LabVIEW卷
- Instant 960 Grid System
- Python數(shù)據(jù)分析(第2版)
- TypeScript實(shí)戰(zhàn)指南
- iOS應(yīng)用逆向工程(第2版)
- Android開發(fā)案例教程與項(xiàng)目實(shí)戰(zhàn)(在線實(shí)驗(yàn)+在線自測)
- C語言從入門到精通
- Learning YARN
- 監(jiān)控的藝術(shù):云原生時代的監(jiān)控框架
- Hands-On Robotics Programming with C++
- Kotlin進(jìn)階實(shí)戰(zhàn)
- Game Development Patterns and Best Practices