Amp-Caching and Updation in Rails

Ideakart
ruby-rails-tips
Published in
2 min readAug 27, 2019

Amp Cache can really load your pages faster when a user clicks on google results, one reason being the limit on 50,000 bytes and no java-script support.

Now you can add support for amp by just adding an amp extension in your rails app and you can use the following components.

That being said, one problem is that once you update your page, how do you update the cached page on google servers.

Now you can definitely read about it here:

For e.g. I own this domain ideakart.com and update the cache frequently using the code described below.

Here is the script for updating the cache:

Amp Cache Updation
  1. You can generate the public and private keys by using this:
openssl genrsa 2048 > private-key.pem
openssl rsa -in private-key.pem -pubout >public-key.pem

Also add your public file to this location:

https://example.com/.well-known/amphtml/apikey.pub

2. `echo -n >url.txt “/update-cache/c/s/www.ideakart.com#{url}?amp_action=flush&amp_ts=#{ts}" && cat url.txt | openssl dgst -sha256 -sign private-key.pem >signature.bin`

This line basically copies your code to url.txt and then sign it with the help of the private key generated.

3. Then encode signature bin using base64 using:

amp_signature = Base64.urlsafe_encode64(File.read(“signature.bin”))

4. Then just update the cache by hitting wget or curl:

`wget -q — spider “https://www-ideakart-com.cdn.ampproject.org/update-cache/c/s/www.ideakart.com#{url}?amp_action=flush&amp_ts=#{ts}&amp_url_signature=#{amp_signature}"`

That’s it, it will update the amp cache on google.

--

--