使用 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 缓存使用 落后一个的缓存模型,该模型可以通过 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 编写