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
builder
gem - create
helper.rb
file underlib
folder if you haven’t yet - use
Nanoc::Helpers::XMLSitemap
in your lib helper file - add
sitemap/sitemap.erb
to yourcontent
folder - update
sitemap/sitemap.erb
with<%= xml_sitemap %>
- update
Rule
file with sitemap compile and route rules - update site configuration file
nanoc.yml
with 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'