Vapor Sitemap

A dynamic sitemap generator for Vapor.


Add the package to your Package.swift file:

let package = Package(
    // ...
    dependencies: [
        // ? A server-side Swift web framework.
        .package(url: "", from: "4.0.0"),
        .package(url: "", from: "1.0.0"), // Add this line
    targets: [
            name: "App",
            dependencies: [
                .product(name: "Vapor", package: "vapor"),
                .product(name: "VaporSitemap", package: "vapor-sitemap"), // Add this line
    // ...


First, we need to create some functions to tell to the middleware what to do:

isSitemap(_ path: String) -> Bool

The goal of this function is to tell the middleware if it should handle a path or not. A basic implementation is this one:

func isSitemap(_ path: String) -> Bool {
    return path == "/sitemap.xml"

With this implementation, you tell to the middleware to handle only /sitemap.xml.

In some cases, you may want to generate multiple sitemaps. To do so, just handle all the path you want in this function.

generateURLs(_ path: String) -> [SitemapURL]

The goal of this function is to give all the URLs to put in the specified sitemap. An example implementation is this one:

func generateURLs(_ path: String) -> [SitemapURL] {
    let prefix = ""
    let paths = ["home", "page1", "folder/page2"]
    return { path in
        prefix + path

Final step

In your configure.swift, add the corresponding middleware:

    isSitemap: isSitemap,
    generateURLs: generateURLs

And you’re ready to go!


This package is developed and maintained by Nathan Fallet.


View Github