Sitemap for your Rails App with Google Cloud Storage

There are a lot of articles out there showing how to configure your Rails app to create sitemaps with the site_generator gem and AWS.

Here are my two cents but this time I will configure the above to work with Google Cloud Storage.

Google Cloud

First things first, you need an active Google Cloud account (duh)!

Google Cloud Storage

Great we have our bucket but still need a bit of configuration!

Note: make sure you only use this bucket for the sitemap or objects you do not mind sharing with the outside world

Google Service Account

We now need a method to securely create our sitemap in Google Cloud Storage.

We will use a service account with a generated key file that we can use in our Rails app.

We have now finished all the Google Cloud configuration, let’s now head to our Rails App.

Rails

Rails version is 5.2.4.3

Remember the key file we generated? It is time to put it in use.

I recommend to copy the content of this key in credentials.yml - that’s what I have done but you may choose a different path. Whatever option you choose make sure is secure and encrypted!

Add these to gems to your Gemfile and bundle install

gem 'google-cloud-storage', '~> 1.26', '>= 1.26.1'
gem 'sitemap_generator', '~> 6.1'

Create config/sitemap.rb as follow

require 'google/cloud/storage'
SitemapGenerator::Sitemap.default_host = "https://www.YOURDOMAIN.com"
SitemapGenerator::Sitemap.create_index = false

SitemapGenerator::Sitemap.sitemaps_host = "https://storage.googleapis.com/[BUCKET_NAME]"
SitemapGenerator::Sitemap.adapter = SitemapGenerator::GoogleStorageAdapter.new(
  # Credentials or path/to/file
  credentials: Rails.application.credentials.gcs_hooleapp_prd,
  project_id: '[GOOGLE_CLOUD_PROJECT_ID]',
  bucket: '[BUCKET_NAME]'
)
# this is just an example of how to crawl your routes and generate the sitemap
SitemapGenerator::Sitemap.create do
  add '/path1', changefreq: 'monthly'
  add '/path2', changefreq: 'daily'
  Product.find_each do |product|
    add product_path(product), lastmod: product.updated_at
  end
end

For further details check sitemap_generator here

We need to update routes.rb with our sitemap location:

  get '/sitemap.xml.gz', to: redirect("https://storage.googleapis.com/[BUCKET_NAME]/sitemap.xml.gz", status: 301)

Make sure you add status:301 otherwise Google may ignore it and your site will not be indexed!

With all these steps in place you can bin/rails sitemap:create and your sitemap will magically show in your bucket!

Bonus

If you are using Heroku to host your app, you may want to take advantage of free scheduler addon . This allows you to run a rake task (like sitemap:refresh) on a daily/monthly/whatever basis.

That’s it! Hope you find it useful!!!

Comments

comments powered by Disqus