indexmap is a small Ruby gem for generating XML sitemaps from explicit Ruby data.
It is designed for Rails apps that want:
- deterministic sitemap output
- plain Ruby configuration
- first-party rake tasks instead of a large DSL
- easy extraction of sitemap logic into app-owned manifests
By default, indexmap writes a sitemap index plus one or more child sitemap files. For simpler sites, it also supports :single_file mode, which writes a single urlset directly to sitemap.xml.
Add this line to your application's Gemfile:
gem "indexmap"And then execute:
bundle installOr install it directly:
gem install indexmapUpgrading an existing app? Read UPGRADE.md before deploying,
especially if the app uses custom storage or stores sitemap files under a
directory prefix such as sitemaps/.
require "indexmap"
sections = [
Indexmap::Section.new(
filename: "sitemap-marketing.xml",
entries: [
Indexmap::Entry.new(loc: "https://example.com/"),
Indexmap::Entry.new(loc: "https://example.com/pricing", lastmod: Date.new(2026, 4, 21))
]
)
]
Indexmap::Writer.new(
sections: sections,
base_url: "https://example.com"
).writeIn an initializer:
Indexmap.configure do |config|
config.base_url = -> { "https://example.com" }
config.storage = -> do
Indexmap::Storage::Filesystem.new(
path: Rails.public_path,
public_url: config.base_url
)
end
config.sections = -> do
[
Indexmap::Section.new(
filename: "sitemap-marketing.xml",
entries: [
Indexmap::Entry.new(loc: "https://example.com/")
]
)
]
end
endThen run:
bin/rails indexmap:sitemap:create
bin/rails indexmap:sitemap:format
bin/rails indexmap:sitemap:validateindexmap:sitemap:create is the main task. It builds sitemap files in memory,
formats them, validates the result, then writes the final XML files to the
configured storage. Existing sitemap files are left untouched if generation or
validation fails.
This is the default behavior. indexmap writes:
sitemap.xmlas a sitemap index- one or more child sitemap files from
config.sections
For sites that only want one sitemap.xml file:
Indexmap.configure do |config|
config.base_url = -> { "https://example.com" }
config.storage = -> { Indexmap::Storage::Filesystem.new(path: Rails.public_path, public_url: config.base_url) }
config.format = :single_file
config.entries = -> do
[
Indexmap::Entry.new(loc: "https://example.com/"),
Indexmap::Entry.new(loc: "https://example.com/about", lastmod: Date.new(2026, 4, 21))
]
end
endIn :single_file mode, indexmap writes a urlset directly to sitemap.xml and reads entries from config.entries instead of config.sections.
Most apps only need the default output. Use named outputs when one part of the sitemap must be generated separately, for example when static pages can be generated during deploy but database-heavy pages should refresh later. Named outputs write through the same configured storage as the default output.
Indexmap.configure do |config|
config.base_url = -> { "https://example.com" }
config.storage = -> { Indexmap::Storage::Filesystem.new(path: Rails.root.join("storage/sitemaps"), public_url: config.base_url) }
config.sections = -> { Sitemap.sections }
config.output :insights_data do |output|
output.format = :single_file
output.index_filename = "sitemap-insights-data.xml"
output.entries = -> { Sitemap.insights_data_entries }
end
endGenerate the default output:
Indexmap.createGenerate only the named output:
Indexmap.create(:insights_data)Named outputs inherit base_url and format from the main configuration unless
you override them. Storage is configured once and shared by every output.
Indexmap.create uses the same safe publish flow as the rake task: build,
format, validate, and then write the final XML file or files to storage.
Every indexmap operation reads and writes through config.storage. The storage
object is the source of truth for generation, validation, parsing, Google
submission, IndexNow submission, and IndexNow verification files.
The filesystem adapter stores files in a directory and exposes public URLs from the same filenames:
Indexmap.configure do |config|
config.base_url = "https://example.com"
config.storage = Indexmap::Storage::Filesystem.new(
path: Rails.public_path,
public_url: "https://example.com"
)
endRails apps that store sitemap files in Active Storage can use the optional
adapter. indexmap does not depend on activestorage; this adapter only uses
the model and attachment object you pass in.
Indexmap.configure do |config|
config.base_url = "https://example.com"
config.storage = Indexmap::Storage::ActiveStorage.new(
model: SitemapArtifact,
filename_column: :filename,
attachment: :file,
public_url: "https://example.com"
)
endCustom storage backends can implement the same small interface:
storage.write(filename, body, content_type:)
storage.read(filename)
storage.exist?(filename)
storage.list(prefix:, suffix:)
storage.delete(filename)
storage.public_url(filename)Use after_create when indexmap:sitemap:create should publish the default
sitemap first, then schedule slower dynamic sections for the background. The
callback runs only after the generated files have been formatted, validated, and
replaced successfully.
Indexmap.configure do |config|
config.base_url = -> { "https://example.com" }
config.storage = -> { Indexmap::Storage::Filesystem.new(path: Rails.root.join("storage/sitemaps"), public_url: config.base_url) }
config.sections = -> { Sitemap.sections }
config.output :insights_data do |output|
output.format = :single_file
output.index_filename = "sitemap-insights-data.xml"
output.entries = -> { Sitemap.insights_data_entries }
end
config.after_create do
Insights::SitemapRefreshJob.perform_later
end
endThen the job can stay small:
class Insights::SitemapRefreshJob < ApplicationJob
def perform
Indexmap.create(:insights_data)
end
endThis keeps deploys fast: the deploy only waits for indexmap:sitemap:create,
while database-dependent output is refreshed by the job backend.
indexmap also includes small utilities for working with generated sitemap files:
parser = Indexmap::Parser.new(source: "sitemap.xml")
parser.paths
# => ["/", "/about", "/articles/example"]
Indexmap::Validator.new.validate!The built-in validator checks for:
- missing sitemap files
- malformed sitemap XML
- empty sitemap files
- missing or duplicate child sitemap references
- duplicate sitemap URLs
- parameterized URLs in sitemap entries
- fragment URLs in sitemap entries
- non-HTTP or relative URLs
- URLs outside the configured
base_url - invalid
lastmodvalues
indexmap can ping Google Search Console and IndexNow after sitemap generation.
Available rake tasks:
bin/rails indexmap:sitemap:validate
bin/rails indexmap:google:ping
bin/rails indexmap:index_now:ping
bin/rails indexmap:index_now:write_key
bin/rails indexmap:pingGoogle pinging requires service account credentials:
Indexmap.configure do |config|
config.google.credentials = -> { ENV["GOOGLE_SITEMAP"] }
endIf config.google.credentials is blank, indexmap:google:ping skips Google submission.
You can optionally override the Search Console property identifier:
Indexmap.configure do |config|
config.google.credentials = -> { ENV["GOOGLE_SITEMAP"] }
config.google.property = -> { "sc-domain:example.com" }
endIf config.google.property is not set, indexmap defaults to sc-domain:<host>.
IndexNow submission requires a key. indexmap supports two ways to provide it:
- set
config.index_now.key - or keep a valid verification file in the configured storage as
<key>.txt
Configured-key example:
Indexmap.configure do |config|
config.index_now.key = -> { ENV["INDEXNOW_KEY"] }
endIf config.index_now.key is set, indexmap:sitemap:create also ensures the matching <key>.txt verification file exists in storage. It leaves an existing valid key file unchanged.
If you need a non-standard verification filename, configure it explicitly:
Indexmap.configure do |config|
config.index_now.key = -> { ENV["INDEXNOW_KEY"] }
config.index_now.key_filename = -> { "#{ENV.fetch("INDEXNOW_KEY")}.txt" }
endYou can also disable automatic key-file writes entirely:
Indexmap.configure do |config|
config.index_now.key = -> { ENV["INDEXNOW_KEY"] }
config.index_now.write_key_file = false
endIf you prefer the file-based flow, run:
bin/rails indexmap:index_now:write_keyThat task:
- reuses an existing valid key file when present
- otherwise generates a new key in
<key>.txt - makes that key available to
indexmap:index_now:pingwithout addingconfig.index_now.key
If neither a configured key nor a valid key file is present, indexmap:index_now:ping skips IndexNow submission.
Run tests:
bundle exec rake testRun lint:
bundle exec rake standardRun the full default task:
bundle exec rakeTests generate a coverage report automatically.
Note: Gemfile.lock is intentionally not tracked for this gem, following normal Ruby library conventions.
We use lefthook with the Ruby commitlint gem to enforce Conventional Commits on every commit. We also use Standard Ruby to keep code style consistent. CI validates commit messages, Standard Ruby, tests, and git-cliff changelog generation on pull requests and pushes to main/master.
Run the hook installer once per clone:
bundle exec lefthook installReleases are tag-driven and published by GitHub Actions to RubyGems. Local release commands never publish directly.
Install git-cliff locally before preparing a release. The release task regenerates CHANGELOG.md from Conventional Commits.
Before preparing a release, make sure you are on main or master with a clean worktree.
Then run one of:
bundle exec rake 'release:prepare[patch]'
bundle exec rake 'release:prepare[minor]'
bundle exec rake 'release:prepare[major]'
bundle exec rake 'release:prepare[0.1.0]'The task will:
- Regenerate
CHANGELOG.mdwithgit-cliff. - Update
lib/indexmap/version.rb. - Commit the release changes.
- Create and push the
vX.Y.Ztag.
The Release workflow then runs tests, publishes the gem to RubyGems, and creates the GitHub release from the changelog entry.
MIT License, see LICENSE.txt
Made by the team at Ethos Link — practical software for growing businesses. We build tools for hospitality operators who need clear workflows, fast onboarding, and real human support.
We also build Reviato, “Capture. Interpret. Act.”. Turn guest feedback into clear next steps for your team. Collect private appraisals, spot patterns across reviews, and act before small issues turn into public ones.