Swiftpack.co - vapor-community/vapor-sitemap as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by vapor-community.
vapor-community/vapor-sitemap 1.1.0
A dynamic sitemap generator for Vapor.
⭐️ 5
🕓 2 weeks ago
macOS
.package(url: "https://github.com/vapor-community/vapor-sitemap.git", from: "1.1.0")

Vapor Sitemap

A dynamic sitemap generator for Vapor.

Setup

Add the package to your Package.swift file:

let package = Package(
    // ...
    dependencies: [
        // 💧 A server-side Swift web framework.
        .package(url: "https://github.com/vapor/vapor.git", from: "4.0.0"),
        .package(url: "https://github.com/vapor-community/vapor-sitemap.git", from: "1.0.0"), // Add this line
    ],
    targets: [
        .target(
            name: "App",
            dependencies: [
                .product(name: "Vapor", package: "vapor"),
                .product(name: "VaporSitemap", package: "vapor-sitemap"), // Add this line
            ]
        ),
    ],
    // ...
)

Usage

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

isSitemap(_ req: Request) -> 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(_ req: Request) -> Bool {
    return req.url.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(_ req: Request) -> [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(_ req: Request) -> [SitemapURL] {
    let prefix = "https://www.example.com/"
    let paths = ["home", "page1", "folder/page2"]
        
    return paths.map { path in
        prefix + path
    }
    .map(SitemapURL.init)
}

Final step

In your configure.swift, add the corresponding middleware:

app.middleware.use(SitemapMiddleware(
    isSitemap: isSitemap,
    generateURLs: generateURLs
))

And you're ready to go!

About

This package is developed and maintained by Nathan Fallet.

GitHub

link
Stars: 5
Last commit: 2 weeks ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Dependencies

Release Notes

VaporSitemap v1.1.0
2 weeks ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics