Sitemap with nanoc
I use nanoc a static site generator for my personal blog.
you can also take a look at the source here github
I have a plan to write a blog about using nanoc to create a personal site from scratch.
Soon I will be able to write my experience in words.
But today itβs something basic about how can we generate a simple sitemap for our site via nanoc tool.
If you are not aware of sitemap and what is this? looke here a quickly
sitemap helps web crawlers to do a better job of crawling your site
how to generate a sitemap for your site
you should already have a blog structure with nanoc create-site command
# directories structure of nanoc sites
βββ Rules
βββ content
βΒ Β βββ index.html
βΒ Β βββ stylesheet.css
βββ layouts
βΒ Β βββ default.html
βββ lib
βββ nanoc.yaml
βββ output
4 directories, 5 files
I am using tree utility to print the directories structure. Not heard about tree command utility, I have a simple introduction here tree command.
in most cases, you add your site related changes under content folder.
so letβs create a new folder called sitemap under content
mkdir content/sitemap
# show direcories under content folder
tree -d content/
#=>
content/
βββ sitemap
1 directory
once we have sitemap directory under content we will add sitemap.erb file under sitemap directory.
# crete sitemap.erb file
touch content/sitemap/sitemap.erb
# show all files under content/sitemap folder
tree content/sitemap
#=>
content/sitemap
βββ sitemap.erb
0 directories, 1 file
now itβs the time to add sitemap generator helper in sitemap.erb file.
nanoc tool has a helper to generate sitemap links called Nanoc::Helpers::XMLSitemap.
Use this helper in you lib helper file and update the content of content/sitemap/sitemap.erb with <%= xml_sitemap %>.
and if you get an error saying LoadError: cannot load such file -- builder
you need to install builder gem.
gem install builder
Add the code change for sitemap generator
# add Nanoc::Helpers::XMLSitemap to you lib helper file
# if you don't have one create it
# lib/helper.rb
use_helper Nanoc::Helpers::XMLSitemap
# content/sitemap/sitemap.erb
<%= xml_sitemap %>
If you have run the nanoc command to compile the site you will be wondering why no changes for sitemap.erb it still the same π
Let me tell you that we need to define a rule for this to generate the desired behavior.
for that, we need to changes our Rules file and most important add base url in the site configuration file.
sitemap generation requires the site configuration to specify the base URL for the site
add the following changes to your Rules and nanoc.yml file
# Rule file changes
# compile the sitemap.erb and put the compiled content under sitemap.xml in root
compile '/sitemap/sitemap.erb' do
filter :erb
write item.identifier.without_ext.to_s.sub('/sitemap', '') + '.xml'
end
# nanoc.yml file changes
base_url: 'http://127.0.0.1:3000'
At last run the nanoc command in the root of your blog, you will see a sitemap.xml
the file generated under output folder
you will get the directories structure similar to this
βββ Rules
βββ content
βΒ Β βββ index.html
βΒ Β βββ sitemap
βΒ Β βΒ Β βββ sitemap.erb
βΒ Β βββ stylesheet.css
βββ crash.log
βββ layouts
βΒ Β βββ default.html
βββ lib
βΒ Β βββ helper.rb
βββ nanoc.yaml
βββ output
βΒ Β βββ index.html
βΒ Β βββ sitemap.xml
βΒ Β βββ stylesheet.css
βββ tmp
βββ nanoc
βββ 1c6d93c4bdafb
βββ checksums
βββ compiled_content
βββ dependencies
βββ outdatedness
βββ rule_memory
8 directories, 16 files
summary
- install
buildergem - create
helper.rbfile underlibfolder if you havenβt yet - use
Nanoc::Helpers::XMLSitemapin your lib helper file - add
sitemap/sitemap.erbto yourcontentfolder - update
sitemap/sitemap.erbwith<%= xml_sitemap %> - update
Rulefile with sitemap compile and route rules - update site configuration file
nanoc.ymlwith base_url value (mandatory)
# install builder gem
gem install builder
# create lib/helper.rb
touch lib/helper.rb
# lib/helper.rb
# add following
use_helper Nanoc::Helpers::XMLSitemap
# create sitemap folder and sitemap.erb file under content folder
mkdir content/sitemap
touch content/sitemap/sitemap.erb
#content/sitemap/sitemap.erb
# add following in sitemap.erb
<%= xml_sitemap %>
# Rule
# make change in Rule for sitemap
# add following
compile '/sitemap/sitemap.erb' do
filter :erb
write item.identifier.without_ext.to_s.sub('/sitemap', '') + '.xml'
end
# nanoc.yml
# add base_url value to nanoc.yml (mandatory)
base_url: 'https://tenderprog.com'