使用 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