VaporSitemap

1.1.0

A dynamic sitemap generator for Vapor.
vapor-community/vapor-sitemap

What's New

VaporSitemap v1.1.0

2022-04-26T09:46:58Z

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.

Description

  • Swift Tools 5.6.0
View More Packages from this Author

Dependencies

Last updated: Fri Oct 18 2024 10:33:52 GMT-0900 (Hawaii-Aleutian Daylight Time)