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

推荐订阅源

宝玉的分享
宝玉的分享
NISL@THU
NISL@THU
E
Exploit-DB.com RSS Feed
L
LINUX DO - 热门话题
L
Lohrmann on Cybersecurity
K
Kaspersky official blog
Project Zero
Project Zero
Cisco Talos Blog
Cisco Talos Blog
T
The Exploit Database - CXSecurity.com
P
Palo Alto Networks Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
S
Schneier on Security
G
GRAHAM CLULEY
The Hacker News
The Hacker News
T
Threat Research - Cisco Blogs
Scott Helme
Scott Helme
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy & Cybersecurity Law Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Cyberwarzone
Cyberwarzone
C
CERT Recently Published Vulnerability Notes
T
Tor Project blog
AWS News Blog
AWS News Blog
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
爱范儿
爱范儿
P
Privacy International News Feed
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
S
Securelist
G
Google Developers Blog
The Last Watchdog
The Last Watchdog
Google Online Security Blog
Google Online Security Blog
美团技术团队
F
Fortinet All Blogs
小众软件
小众软件
Recorded Future
Recorded Future
V
Visual Studio Blog
B
Blog RSS Feed
H
Help Net Security
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Google DeepMind News
Google DeepMind News
Blog — PlanetScale
Blog — PlanetScale
博客园 - 聂微东
Stack Overflow Blog
Stack Overflow Blog
Martin Fowler
Martin Fowler
Latest news
Latest news
Spread Privacy
Spread Privacy
H
Heimdal Security Blog

博客园 - QDuck

Handlebars.js入门教程 GHOST CMS - Handlebars Themes - Further Reading GHOST CMS - Properties GHOST CMS - Redirects GHOST CMS - Channels GHOST CMS - Content Collections GHOST CMS - Content Taxonomies GHOST CMS - Custom Routes GHOST CMS - URLs & Dynamic Routing GHOST CMS - Responsive Images 图片显示 GHOST CMS - Editor编辑器 GHOST CMS - Utility Helpers公用事业帮手 GHOST CMS - Data Helpers GHOST CMS - Functional Helpers功能助手 GHOST CMS - 标签Tag GHOST CMS - Author作者 GHOST CMS - Page 页面 GHOST CMS - Post 文章 GHOST CMS - 索引Index
GHOST CMS - Google AMP谷歌地图
QDuck · 2019-12-22 · via 博客园 - QDuck

Google AMP

Ghost supports AMP Accelerated Mobile Pages for the posts in your publication.

Overview

The AMP project is a Google led initiative that enables lightweight versions of any piece of content on a site which offers speed and performance your readers in the browser.

In Ghost, a single handlebars template is used to automatically render AMP versions of the posts on your site. The default amp.hbs template offers all of the basic functionality of AMP, but it's possible to build on top of this to include other AMP supported features if required!

The AMP feature is enabled by default, or you can disable it in the settings of Ghost admin if you'd prefer not to use it.

Route

To access any post rendered using the amp.hbs template on your site, add /amp/ to the end of any post URL on your publication. The parent post also has a canonical link to it's AMP equivalent.

Template

The amp context always uses the amp.hbs template. Ghost will look for this template within your theme files and use this by default.

The template structure is as follows:

.
├── /assets
|   └── /css
|       ├── screen.css
|   ├── /fonts
|   ├── /images
|   ├── /js
├── default.hbs
├── index.hbs [required]
└── post.hbs [required]
└── amp.hbs [optional]
└── package.json [required]

Check out the default template in full.

Data

The amp context provides access to the post object which matches the route. As with all contexts in Ghost, all of the @blog global data is also available.

When outputting the post, you can use a block expression ({{#post}}{{/post}}) to drop into the post scope and access all of the attributes. See a full list of attributes.

AMP features

AMP consists of three different parts:

  • AMP HTML
  • AMP JS
  • Google AMP Cache

AMP has many restrictions for optimal performance. For example, JavaScript can only be used in certain circumstances, CSS must be in the correct tags inside of the <head>section, and AMP HTML must be used instead of common HTML.

If you are making adjustments to your amp.hbs file, follow the documentation provided on the official AMP docs site.

Edited amp.hbs templates can be updated in your theme by uploading a .zip of your updated theme in Ghost admin.

Debugging AMP

Since AMP has strict restrictions, it's important to ensure that your code passes AMP validation. The quickest way to do this is to add #development=1 to the AMP URL, and check for validation errors in your browser console.

Helpers

Because the amp context is using the post data, you can use almost every post helperinside of the {{#post}}{{/post}} block expression. In addition to this, there are three helpers especially for amp which are decribed below.

{{amp_ghost_head}}

This helper belongs just before the </head> tag in amp.hbs and outputs structured data in JSON/LD, Facebook Open Graph and Twitter cards, as well as an RSS URL path. It is a simplified version of {{ghost_head}} for AMP.

{{amp_components}}

This helper can exist just before the </head> tag in amp.hbs and can output the necessary javascript if your content contains a .gif, an <iframe> or an <audio> tag.

{{amp_content}}

This helper transforms post content into valid AMP HTML.

  • <img> transforms to <amp-img>
  • .gif transforms to <amp-anim>
  • <iframe> transforms to <amp-iframe>
  • <audio> transforms to <amp-audio>

{{img_url}} helper

There are special requirements for using the {{img_url}} helper. It must be wrapped in an <amp-img> tag and must provide a width and height property. This only works for post images.

Summary

You've now got an understanding of how Ghost implements AMP pages on your publication, have an understanding of how the amp.hbs template works should you decide to develop it further for your site. Always refer to the official AMP documentation when making adjustments to your AMP template!