Better SEO with gem sitemap_generator

Tram Ho

Preface

In today’s web world, is SEO (search engine optimization) indispensable after creating a website? With that in mind, a newbie Ruby on Rails developer like me will have to solve that requirement in the near future. Through the protocol, sitemaps are encoded as UTF-8 and saved as XML files. It provides the clearest map for bots, a crawler to provide search services like Google , duckduckgo . This is extremely simple but becomes complicated if the object to create sitemaps is a large and complex website. Complicated, should let the computer do it, but create sitemaps with rice how: v Let’s get started

Solution (gem sitemap_generator)

There are many solutions to create sitemaps for Rails, but I prefer the gem sitemap_generator . Let’s see if it has any interesting functions.

  • It works independently. So we can use it without Rails
  • Extremely flexible
  • Gem has its own rules that are not affected by other app’s configurations
  • It allows us to upload a sitemaps file from a third party (the file was created by another tool).
  • It will automatically pings when the sitemap is created
  • Support multi-sitemap with many media formats like video, images, news, etc

In this article I will use Rails to create web apps with a few simple features to make sitemaps.

Create project

Open terminal and type command

The command must be very familiar every time you start a new project, I add -T to remove the test files. This gem is compatible with all versions of Rails, so you can use any version. Rails 5, or Rails 6 (newest) also oke. After the command has finished running, go to the project folder cd Sitemapper then run rails s and open the browser to the path localhost:3000 if it appears on the screen Yay! You’re on Rails! is okay then.

Now we need some routes, models, controllers, migrations for the project. The project will be a blog with categories , each category has many posts with a simple attribute of title and content .

We add the link to link the model if not already

Now the indispensable part is Routes

And don’t forget to add the default link.

Create Controller corresponding to the model

Next to create some sample data we use gem fake

Then remember to run bundle install Change the seed file again to be oke.

Number “5” you can customize if you want more data offline. Now run the seed data command

gem “sitemap_generator”

Add gem to Gemfile and run bundle install command

Using rake to install sitemaps, run:

The above command will create a configuration file configs/sitemap.rb . Open the file and you can see the first line (excluding comments).

That is the domain config line of the website. We can change it according to our domain.

Or we can configure a multi sitemap if needed such as:

I will correct it as:

To the main part that is SitemapGenerator::Sitemap.create we add the default page

add can take a bunch of arguments of arguments we add code to generate for the Post , Category and have the following complete file:

changefreq is the time interval each time that data changes. The default will be Time.zone.now

Done, now run the following command to scan and create a sitemap file automatically.

This is my result

If you go to public folder, you will find sitemap.xml.gz , extract the file and we will sitemap.xml. Open it with a browser to make it easier to see

You can refer to the option of add in here to customize as required by the project offline.

source code: demo source

Thank you for reading.

Share the news now

Source : Viblo