So you are using AMP for your website, that’s great. AMP is one of the best way to increase the traffic to your site.
Often confused how AMP is cached in Google and when the content is updated in cache?
AMP Caching
AMP is using ‘stale-while-revalidate’ model.
Indicates that the client is willing to accept a stale response while asynchronously checking in the background for a fresh one. The seconds value indicates for how long the client is willing to accept a stale response.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
Google will try to check the origin site for the new content when a user views the page. If content is changed in the origin server, then Google will update the content in cache and serves it to the next user.
Update AMP Cache
Want to update the AMP cache immediately when the content is updated in the origin? Yes, that’s possible with the Google’s update-cache
URL. Google provides a special URL to clear any AMP content on the cache. When you Google’d about this you should have come across this page.
Get AMP Cached URL
Finding hard to get the Cached AMP URL? Don’t worry ampbyexample.com has a tool to make your job easy.
Paste your post URL on the ampbyexample.com URL conversion box and hit convert. It will give the Cache version of your post.
RSA Keys
All AMP cache clear request to Google must to signed with the proper RSA keys to prove the authenticity of your content on Google cache. So generate the private and public keys with the help of OpenSSL‘s command line tools.
Guidelines
You must follow the update-cache
guidelines:
- The AMP Cache hostname (cdn.ampproject.org) is excluded from the signature to allow submitting the same signed request to multiple AMP Cache operators.
- For signature verification, you must serve the public RSA key at a fixed location on the AMP document’s domain (to generate the key, see Generate the RSA key). For example:
https://example.com/.well-known/amphtml/apikey.pub
- The public key must not be roboted.
- The URL must be HTTPS.
- The domain must be the exact domain that you want to update, not a sub or super domain.
- You must publish the key in PEM format and serve the key with the content-type “text/plain”.
- The AMP Cache always fetches the public key from the same domain of the request, regardless of the domain specified by the document via any rel=canonical tag. If the origin domain serves an HTTP redirect at the location to be flushed, only the requested path is flushed from cache, and not the target of the redirect.
AMP Cache Clear
Construct the cache clear URL in the specified format.
https://example-com.<cache.updateCacheApiDomainSuffix>/update-cache/c/s/example.com/article?amp_action=flush&_ts=<ts_val>&_url_signature=<sig_val>
Hit this URL every time you want to clear the post cache from Google CDN like on every post save event.
That’s it. Cache will be cleared from Google CDN and fresh content will be shown to the next user who request for the content from Google.