使用 Google AMP 缓存
简介
Google AMP 缓存存储有效的 AMP 并提供对 AMP 的持续快速访问。任何人都可以使用它。
AMP 缓存 URL 格式
在可能的情况下,Google AMP 缓存将为每个 AMP 文档的域创建一个子域,方法是首先将其从 IDN (punycode) 转换为 UTF-8。缓存将每个 -
(破折号)替换为 --
(2 个破折号),并将每个 .
(点)替换为 -
(破折号)。例如,pub.com
将映射到 pub-com.cdn.ampproject.org
。
以下是 https://amp.org.cn/about/websites/
的 AMP 缓存 URL
转换后的 AMP 缓存 URL 由以下部分组成
amp-dev
:使用上述算法转换为子域的发布者域。cdn.ampproject.org
:AMP 缓存域。c
:表示它是 AMP 文档(还有i
表示图像,r
表示字体等资源)。s
:表示 AMP 缓存使用 TLS(安全 HTTPS)从源获取内容。amp.dev/examples/components/amp-img
:原始 URL,不包括方案。
URL 中包含查询字符串中的参数是允许的,只需将这些参数也包含在 AMP 缓存 URL 中即可。
示例:https://amp-dev.cdn.ampproject.org/c/s/amp.dev/documentation/examples/api/query?value=Hello%20World。
缓存更新
AMP 缓存使用 one-behind 缓存模型,该模型可以通过 max-age
缓存指令进行控制。您可以使用此示例页面测试其行为,该页面将在服务时打印当前日期
Max-age 15 秒:https://amp-dev.cdn.ampproject.org/c/s/amp.dev/documentation/examples/api/query?maxage=15。
重定向和错误处理
以下是一些关于 AMP 缓存如何处理重定向和错误的示例
重定向
AMP 缓存在解析 AMP URL 时会遵循重定向。例如,如果一个 URL 重定向到另一个 AMP URL
$ curl -I https://ampbyexample.com/components/amp-img/ HTTP/1.1 301 Moved Permanently Content-Type: text/html; charset=utf-8 Location: https://amp.org.cn/documentation/examples/components/amp-img/ ...
那么 AMP 缓存将返回原始 URL 解析后的重定向内容。
示例:https://amp-dev.cdn.ampproject.org/c/s/ampbyexample.com/components/amp-img/。
未找到
当在 AMP 缓存中找不到页面时,它将显示一个错误页面并返回 404 状态。
示例:https://amp-dev.cdn.ampproject.org/amp.dev/not-found
无效的 AMP
当页面是无效的 AMP 时,AMP 缓存将返回 404 状态。
示例:https://amp-dev.cdn.ampproject.org/amp.dev/static/samples/files/invalid_amp.html
服务器错误
如果 URL 返回 5XX 服务器错误,AMP 缓存将返回 404 状态。
示例:https://amp-dev.cdn.ampproject.org/amp.dev/documentation/examples/api/error
如果此页面上的解释没有涵盖您所有的问题,请随时联系其他 AMP 用户来讨论您的具体用例。
转到 Stack Overflow 未解释的功能?AMP 项目强烈鼓励您的参与和贡献!我们希望您能成为我们开源社区的持续参与者,但也欢迎您对您特别感兴趣的问题做出一次性贡献。
在 GitHub 上编辑示例-
作者: @kul3r4