概览
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Google AMP Cache 提供发布到网络的有效 AMP 内容的缓存副本。直接从 Google AMP Cache 中提供内容,以提供快速的用户体验。
若要利用 Google AMP Cache,必须使用 AMP Cache 网址格式直接从缓存中访问 AMP 网址。每次用户通过缓存访问 AMP 内容时,内容都会自动更新,而更新后的内容会在缓存内容后传送给下一位用户。
AMP 缓存网址格式由以下部分组成:
组件 |
说明 |
子域名 |
在字符限制和技术规范允许的情况下,Google AMP Cache 创建的子网域将易于用户理解,并且与发布商自己的网域非常相似。Google AMP Cache 会尽可能通过以下方式创建各个子网域:
- 将 AMP 文档网域从 IDN (Punycode) 转换为 UTF-8。
- 将每个“-”(短划线)替换为“--”(2 个短划线)。
- 将每个“(.)”替换为“-”(短划线)。
- 转换回 IDN (Punycode)。
例如,pub.com 将映射到 pub-com.cdn.ampproject.org 。
如果技术限制阻止人工可读的子网域运行,系统将改用单向哈希。 |
内容类型 |
内容类型可以是以下任一类型:对于 AMP HTML 文档,显示为 'c';对于图片,为 &i39;i';对于字体等资源,为 &r33。
Google AMP Cache 会针对与这些内容类型不匹配的资源返回 404 错误(请参阅核心错误)。 |
可选(可选) |
如果存在此标记,则要求 Google 使用 TLS(安全 HTTPS)从源站提取内容。如果 's' 不存在,则使用纯 HTTP 从源站提取内容。 |
URI |
要提取的内容的 URI。URI 不能包含架构;也就是说,不得包含“http://”或“https://”。 |
查找子域名
通常情况下,Google AMP Cache 网址的子网域将按照上述步骤列出原始网址的用户可读懂转换内容。不过,由于这种情况可能不成立,因此对于生产就绪应用,检索 Google AMP Cache 网址的最佳方法是使用 Google AMP Cache 网址 API。
对于非生产环境,如需确定 Google AMP Cache 生成的子网域名称,请试用 amp.dev 上的使用 Google AMP Cache 交互式工具。
使用 TLS 请求文档的示例
请求从 https://example.com/amp_document.html
中提取的 AMP HTML 文档(使用 /c
前缀指定):
https://example-com.cdn.ampproject.org/c/s/example.com/amp_document.html
由于 /s
后跟 /c
前缀,因此系统将使用 TLS 从 example.com 中提取文档。
使用纯 HTTP 请求图片的示例
请求从 http://example.com/logo.png
提取的图片(使用 /i
前缀指定):
https://example-com.cdn.ampproject.org/i/example.com/logo.png
由于 /i
前缀后面没有 /s
,因此 Google 会使用普通 HTTP 而不是 HTTP over TLS 从 example.com 中提取图片。
查询参数示例
AMP 缓存网址格式还可以在查询字符串中包含参数。
下面是一个包含参数的 HTTPS 示例:
https://example-com.cdn.ampproject.org/c/s/example.com/g?value=Hello%20World
Google AMP Cache 更新
当用户从 Google AMP Cache 请求 AMP 文档时,缓存会自动请求更新,以便在内容缓存后为下一位用户提供新内容。借助此模型,AMP 文档更新会自动快速传播;少数用户在更新后会看到未更新的版本。
缓存遵循“stale-when-revalidate”模型。它使用源缓存的标头(例如 Max-Age
)作为决定特定文档或资源是否过时的提示。
当用户请求过时的内容时,该请求会导致系统提取新的副本,以便下一个用户获得新鲜内容。
为限制其为发布商网站生成的负载量,Google AMP Cache 会将所有文档视为至少 15 秒,并将任何资源至少视为 1 分钟。
请注意,这些数字将来可能会发生变化,因为我们会调整缓存,以在发布商网站的新鲜度和负载之间达到最佳平衡。
缓存优化和修改
Google AMP Cache 会执行各种优化和修改,例如:
- 验证内容是否确实采用 AMP 格式,从而符合所有 AMP 效果目标。
- 除了 AMP 文档之外,还会缓存图片和字体。
- 限制图片大小上限,以防止浏览器出现内存问题和响应速度慢。
- 通过
amp-img
标记提高图片传递效率的各种转换,例如:
- 移除不可见或很难看到的数据,例如某些元数据。
- 将图片转换为更小、更适合移动设备的图片格式,例如在支持 WebP 的浏览器中将 GIF、PNG 和 JPEG 格式的图片转换为 WebP 格式。
- 如果请求包含 Save-Data 标头,则将图片转换为低画质。
- 生成备用大小的版本并添加
srcset
属性以支持分发自适应尺寸的图片。
- 通过安全的通道 (HTTPS) 提供,并使用最新的网络协议(SPDY、HTTP/2)。
- 清理 AMP 文档,防止因错误关闭的 HTML 标记、注释等因素而导致 XSS 攻击。
在进行上述转换时,Google AMP Cache 会忽略“Cache-Control: no-transform”标头。
HTML 清理
Google AMP Cache 会重写所有文档以对解析结果进行标准化。
例如:
- 去掉了所有 HTML 注释。
- 标记和属性名称采用小写形式。
- 属性值会始终采用英文引号进行转义。
- 除 HTML5 void 元素外,所有标记均已关闭。
- 标记内的空格会被剥离。
- 文本已转义。
- 使用 UTF-8 等效字符简化了编码文本字符。
- 只能位于
body
中的元素会移到 body
中。
- 出站链接已设为绝对链接,这样一来,当文档是通过 Google AMP Cache 来源(而非发布商来源)提供时,出站链接仍然有效。
Google AMP Cache 为浏览器添加了各种预提取提示标记,以帮助提前加载资源。例如,<link href='https://fonts.googleapis.com/css?family=Lato' rel='stylesheet’>
会更改为:
<link href="https://fonts.googleapis.com/css?family=Lato" rel="stylesheet">
<link href="https://fonts.gstatic.com" rel="dns-prefetch preconnect">
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2023-08-30。
[null,null,["最后更新时间 (UTC):2023-08-30。"],[[["\u003cp\u003eGoogle AMP Cache delivers cached copies of valid AMP content for a faster user experience.\u003c/p\u003e\n"],["\u003cp\u003eUtilize the AMP Cache URL format to access content directly from the cache, ensuring automatic updates with each user access.\u003c/p\u003e\n"],["\u003cp\u003eThe Google AMP Cache optimizes and modifies content, including image transformations, security enhancements, and HTML sanitization, to improve delivery and performance.\u003c/p\u003e\n"],["\u003cp\u003eThe cache employs a "stale-while-revalidate" model, using origin caching headers and automatic updates to balance content freshness with publisher site load.\u003c/p\u003e\n"]]],["The Google AMP Cache stores and serves cached AMP content for fast user experiences. Accessing content via the AMP Cache URL format triggers automatic updates. The format includes a subdomain, content type (`c`, `i`, or `r`), optional `s` for TLS, and the content's URI. Content is considered fresh for at least 15 seconds (documents) or 1 minute (resources) before being revalidated. The cache optimizes content, such as images and documents, by validating AMP format, applying various transformations, and sanitizing HTML, for efficient and secure delivery.\n"],null,["# Overview\n\nThe Google AMP Cache serves cached copies\nof valid AMP content published to the web.\nServe content directly from the Google AMP Cache\nto provide a fast user experience.\n\n\nTo take advantage of the Google AMP Cache,\nan AMP URL must be accessed directly from the cache\nusing the AMP Cache URL format.\nEach time a user accesses AMP content from the cache,\nthe content is automatically updated,\nand the updated version is served to the next user once the content has been cached.\n\nAMP Cache URL format\n--------------------\n\n\nThe AMP Cache URL format consists of the following components:\n\n| Component | Description |\n|----------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| Subdomain name | Subdomains created by the Google AMP Cache will be human-readable when character limits and technical specs allow, and will closely resemble the publisher's own domain. When possible, the Google AMP Cache will create each subdomain by: 1. Converting the AMP document domain from IDN (Punycode) to UTF-8. 2. Replacing every \"-\" (dash) with \"--\"(2 dashes). 3. Replacing every \".\" (dot) with a \"-\" (dash). 4. Converting back to IDN (Punycode). For example, `pub.com` will map to `pub-com.cdn.ampproject.org`. Where technical limitations prevent a human readable subdomain, a one-way hash will be used instead. |\n| Content type | The content type is one of the following: 'c' for an AMP HTML document, 'i' for an image, or 'r' for a resource such as a font. The Google AMP Cache returns a 404 error for resources that do not match these content types (see the [core error](/amp/cache/reference/errors/core_errors#NOT_FOUND)). |\n| Optional 's' | If present, requires that Google fetch the content from the origin using TLS (secure HTTPS). If the 's' is not present, then content gets fetched from the origin using plain HTTP. |\n| URI | URI of the content to be fetched. The URI cannot contain a scheme; that is, no \\`http://\\` or \\`https://\\`. |\n\n| **Note:** Use the [AMP URL API](/amp/cache/reference/acceleratedmobilepageurl/rest/v1/ampUrls/batchGet), to match URLs to corresponding AMP URLs and serve the AMP versions instead (see [Link to AMP Content](/amp/cache/use-amp-url)).\n\n### Finding the subdomain name\n\nTypically, the subdomain name for a Google AMP Cache URL will be a human-readable conversion of\nthe original URL using the steps outlined above. However, because there are circumstances where\nthis may not be true, the best way to retrieve Google AMP Cache URLs for production-ready\napplications is to use the [Google AMP Cache URL API](/amp/cache/use-amp-url).\n\nFor non-production situations, to determine what subdomain name the Google AMP Cache generates,\ntry out the [Using the Google AMP Cache](https://amp.dev/documentation/examples/guides/using_the_google_amp_cache/)\ninteractive tool on [amp.dev](https://amp.dev/documentation/examples/guides/using_the_google_amp_cache/).\n\n### Example requesting document using TLS\n\nRequest for an AMP HTML document (specified with the `/c` prefix)\nfetched from `https://example.com/amp_document.html`: \n\n```\nhttps://example-com.cdn.ampproject.org/c/s/example.com/amp_document.html\n```\n\nBecause `/s` follows the `/c` prefix,\nthe document will be fetched from example.com using TLS.\n\n### Example requesting image using plain HTTP\n\nRequest for an image (specified with the `/i` prefix)\nfetched from `http://example.com/logo.png`: \n\n```\nhttps://example-com.cdn.ampproject.org/i/example.com/logo.png\n```\n\nBecause there's no `/s` following the `/i` prefix,\nGoogle fetches the image from example.com using plain HTTP,\nnot HTTP over TLS.\n\n### Query parameter example\n\nThe AMP Cache URL format can also include parameters in the query string.\nThe following is an HTTPS example with parameters:\n`https://example-com.cdn.ampproject.org/c/s/example.com/g?value=Hello%20World`\n\nGoogle AMP Cache updates\n------------------------\n\nWhen a user requests an AMP document from the Google AMP Cache,\nthe cache automatically requests updates in order to be able\nto serve fresh content for the next user once the content has been cached.\nWith this model, updates to AMP documents propagate automatically and quickly;\nfew users will see the non-updated version after your update.\n\n\nThe cache follows a \"stale-while-revalidate\" model.\nIt uses the origin's caching headers, such as `Max-Age`,\nas hints in deciding whether a particular document or resource is stale.\nWhen a user makes a request for something that is stale,\nthat request causes a new copy to be fetched, so that the next user gets fresh content.\n\n\nTo limit the amount of load it generates for publisher sites,\nthe Google AMP Cache considers any document fresh for at least 15 seconds,\nand any resource fresh for at least 1 minute.\nNote that those numbers may change in the future,\nas we tune the cache for optimum balance between freshness and load on publisher sites.\n\nCache optimizations and modifications\n-------------------------------------\n\nThe Google AMP Cache performs optimizations and modifications,\nsuch as the following:\n\n- Validates content is truly in AMP format, meeting all AMP performance goals.\n- Caches images and fonts in addition to AMP documents.\n- Limits maximum image dimensions to prevent browser memory issues and poor responsiveness.\n- Various transformations to improve the delivery efficiency of images via the `amp-img` tag, such as:\n - Removal of data that is invisible or difficult to see, such as certain metadata.\n - Conversion of images to smaller and mobile-friendlier image formats, such as converting GIF, PNG, and JPEG format images to WebP in browsers that support WebP.\n - Transformation of the image to a lower quality if the request includes the Save-Data header.\n - Generation of alternatively sized versions and adding `srcset` attributes to support delivery of responsively sized images.\n- Serves over a secure channel (HTTPS) and uses the latest web protocols (SPDY, HTTP/2).\n- Sanitizes AMP documents to prevent XSS attacks based on incorrectly closed HTML tags, comments, and more.\n\n\nIn making the above transformations,\nthe Google AMP Cache disregards the \"Cache-Control: no-transform\" header.\n\nHTML sanitization\n-----------------\n\n\nThe Google AMP Cache rewrites all documents to normalize parsing.\nExamples include:\n\n- All HTML comments are stripped.\n- Tag and attribute names are lowercased.\n- Attribute values are consistently quoted and escaped.\n- All tags are closed, except for HTML5 void elements.\n- Whitespace inside tags is stripped.\n- Text is escaped.\n- Encoded text characters are simplified, using UTF-8 equivalent characters.\n- Elements that can only be in the `body` get moved into the `body`.\n- Outbound links are made absolute so that they continue to work when the document is served from the Google AMP Cache origin instead of the publisher origin.\n\nInsert prefetch tags\n--------------------\n\nThe Google AMP Cache adds various prefetch hint tags for browsers\nto assist in loading resources earlier.\nFor example,\n`\u003clink href='https://fonts.googleapis.com/css?family=Lato' rel='stylesheet'\u003e`\nchanges to: \n\n```text\n\u003clink href=\"https://fonts.googleapis.com/css?family=Lato\" rel=\"stylesheet\"\u003e\n\u003clink href=\"https://fonts.gstatic.com\" rel=\"dns-prefetch preconnect\"\u003e\n```"]]