Generating a Sitemap
Create an XML sitemap for search engines.
Overview
A sitemap tells search engines which pages exist on your site. Saga includes a built-in sitemap(baseURL:filter:) renderer that automatically includes all pages generated by your writers and createPage(_:using:) calls.
Basic usage
try await Saga(input: "content", output: "deploy")
.register(
folder: "articles",
metadata: ArticleMetadata.self,
readers: [.parsleyMarkdownReader],
writers: [
.itemWriter(swim(renderArticle)),
.listWriter(swim(renderArticles)),
]
)
.createPage("index.html", using: swim(renderHome))
.createPage("sitemap.xml", using: Saga.sitemap(baseURL: URL(string: "https://example.com")!))
.run()
The sitemap includes every page: individual articles, list pages, and the homepage.
Important Order matters!
sitemap(baseURL:filter:)should be the last step in the pipeline, so that it sees all the previously generated pages.
Filtering pages
Use the filter parameter to exclude pages that don’t belong in a sitemap:
try await Saga(input: "content", output: "deploy")
.createPage("404.html", using: swim(render404))
.createPage("search/index.html", using: swim(renderSearch))
.createPage("sitemap.xml", using: Saga.sitemap(
baseURL: URL(string: "https://example.com")!,
filter: { path in
path != "404.html" && path != "search/index.html"
}
))
.run()
Tip Or, just place these
createPagesteps after thesitemapone. Since order matters, they won’t be part of the sitemap if they come after that step.