使用 Google AMP Cache
簡介
Google AMP Cache 儲存有效的 AMP,並提供對 AMP 持續快速的存取。任何人皆可使用。
AMP Cache URL 格式
在可能的情況下,Google AMP Cache 會為每個 AMP 文件的網域建立子網域,方法是先將網域從 IDN (punycode) 轉換為 UTF-8。快取會將每個 -
(破折號) 替換為 --
(2 個破折號),並將每個 .
(點) 替換為 -
(破折號)。例如,pub.com
會對應到 pub-com.cdn.ampproject.org
。
以下是 https://amp.dev.org.tw/about/websites/
的 AMP Cache URL
轉換後的 AMP Cache URL 包含以下部分
amp-dev
:發布者網域使用上述演算法轉換為子網域。cdn.ampproject.org
:AMP Cache 網域。c
:表示這是 AMP 文件 (也有i
表示圖片,r
表示資源,例如字型)。s
:表示 AMP Cache 使用 TLS (安全 HTTPS) 從原始來源擷取內容。amp.dev/examples/components/amp-img
:原始 URL,不包含 scheme。
URL 可以包含查詢字串中的參數,只需將這些參數也包含在 AMP Cache URL 中即可。
範例:https://amp-dev.cdn.ampproject.org/c/s/amp.dev/documentation/examples/api/query?value=Hello%20World。
快取更新
AMP Cache 使用 落後一個版本的快取模型,此模型可由 max-age
快取指令 控制。您可以使用此範例頁面測試行為,此頁面將在服務時印出目前日期
Max-age 15 秒:https://amp-dev.cdn.ampproject.org/c/s/amp.dev/documentation/examples/api/query?maxage=15。
重新導向與錯誤處理
以下是一些關於 AMP Cache 如何處理重新導向和錯誤的範例
重新導向
AMP Cache 在解析 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.dev.org.tw/documentation/examples/components/amp-img/ ...
則 AMP Cache 將針對原始 URL 傳回已解析的重新導向內容。
範例:https://amp-dev.cdn.ampproject.org/c/s/ampbyexample.com/components/amp-img/。
找不到
當在 AMP Cache 中找不到頁面時,它會顯示錯誤頁面並傳回 404 狀態。
範例:https://amp-dev.cdn.ampproject.org/amp.dev/not-found
無效的 AMP
當頁面是無效的 AMP 時,AMP Cache 將傳回 404 狀態。
範例:https://amp-dev.cdn.ampproject.org/amp.dev/static/samples/files/invalid_amp.html
伺服器錯誤
如果 URL 傳回 5XX 伺服器錯誤,AMP Cache 將傳回 404 狀態。
範例:https://amp-dev.cdn.ampproject.org/amp.dev/documentation/examples/api/error
如果此頁面上的說明未能涵蓋您的所有問題,請隨時與其他 AMP 使用者聯繫,討論您的確切使用案例。
前往 Stack Overflow 未說明的特性?AMP 專案大力鼓勵您的參與和貢獻!我們希望您能成為我們開放原始碼社群的長期參與者,但我們也歡迎針對您特別關注的問題做出一次性貢獻。
在 GitHub 上編輯範例-
作者 @kul3r4