Generate XML sitemaps and robots.txt files, hooked into Parklife’s build process. After all HTML pages are built, the sitemap generator discovers them, assigns priorities, and provides accurate last modification dates using Git timestamps.
...
Parklife.application.after_build do |application|
sitemap = Sitemap.new(base_url: application.config.base, build_dir: application.config.build_dir, generate_robots: true)
return Rails.logger.error("Error generating sitemap: #{sitemap.errors.full_messages.join(', ')}") unless sitemap.valid?
sitemap.generate!
end
...
Delete the default robots.txt
file from the public/
folder.
At the time of this guide, the build hooks are not included in the latest Parklife release.
...
# TODO: 30jul25 - switch back to gem release once PR #124 is included (re-introduces build callbacks)
# https://github.com/benpickles/parklife/pull/124
gem "parklife", github: "benpickles/parklife"
...
bundle
The system generates three files in your build directory:
The sitemap automatically discovers all HTML files in the build directory, excluding error pages (404.html, 500.html, etc.).
Pages are assigned priorities based on their URL structure:
/
) - Priority 1.0/page
) - Priority 0.8/section/page
) - Priority 0.5To provide accurate last modification dates, the system attempts to find the original source file for each HTML page. Source files are discovered using CONTENT_PATTERNS
:
content/*/%s.md
- Markdown files in content directoriesapp/views/*/%s.html.erb
- ERB view templatesThe timestamp strategy depends on whether a source file is found:
All pages are marked with a “monthly” change frequency by default.
The implementation follows Rails conventions with a main Sitemap
model and supporting classes:
Sitemap
- Main model handling generation and file writingSitemap::Entry
- Represents individual sitemap entriesSitemap::RobotsGeneratable
- Mixin for robots.txt generationThe generated robots.txt file includes:
User-agent: *
Allow: /
Sitemap: https://example.com/sitemap.xml.gz
Sitemap: https://example.com/sitemap.xml
Both compressed and uncompressed sitemap URLs are included for maximum compatibility.
The sitemap generator requires:
base_url
- The site’s base URLbuild_dir
- Directory containing built HTML filesgenerate_robots
- Boolean flag to enable robots.txt generationError pages defined in ERROR_PAGES
constant are automatically excluded from the sitemap.
Commit: Sitemap and Robots