Skip to main content

Crate larust_sitemap

Crate larust_sitemap 

Source
Expand description

Laravel’s spatie/laravel-sitemap - an XML <urlset> builder plus a convenience response wrapper, not an automatically-mounted route (the same “nothing auto-mounted, the app wires it explicitly” convention every other shim crate this session follows - larust_http::throttle/ csrf/responsecache included). A route handler builds a Vec<SitemapEntry> - mixing static pages discovered via from_static_routes with dynamic per-model URLs the app supplies itself, since this crate has no visibility into an app’s own models, the same reasoning every other shim crate here already documents for not knowing an app’s users/posts-shaped tables - and returns it via response:

ⓘ
async fn sitemap(router: &Router) -> impl IntoResponse {
    let mut entries = larust_sitemap::from_static_routes(
        &larust_support::url(""),
        &router.routes(),
    );
    for post in Post::all().await? {
        entries.push(
            SitemapEntry::new(larust_support::url(&format!("/posts/{}", post.id)))
                .last_modified(post.updated_at)
                .change_freq(ChangeFreq::Weekly),
        );
    }
    larust_sitemap::response(&entries)
}

§Deliberately out of scope for this version

  • No sitemap index / multi-file pagination. The sitemap protocol caps a single file at 50,000 URLs; spatie’s own package auto-splits into a SitemapIndex beyond that. This crate always emits one <urlset> - a real follow-up if an app’s URL count ever approaches that limit, not attempted speculatively here.
  • No built-in caching. Generating a sitemap can be expensive for a large site, and the app is best placed to decide the right TTL - wrap whichever route calls response in larust_http:: responsecache::for_minutes(...) rather than this crate reinventing caching.

Structs§

SitemapEntry
One <url> entry - build with SitemapEntry::new, then chain whichever optional fields apply (Laravel’s own Url::create($loc) ->setLastModificationDate(...)->setChangeFrequency(...) ->setPriority(...)).

Enums§

ChangeFreq
Laravel’s own changefreq vocabulary - advisory only, per the sitemap protocol spec (crawlers aren’t required to honor it).

Functions§

build_xml
Builds the full <?xml ...?><urlset>...</urlset> document - the sitemap protocol (https://www.sitemaps.org/protocol.html).
from_static_routes
Every GET route registered on a larust_http::Router (pass &router.routes()) with no {param} placeholder - the static half of a sitemap, turned into absolute-URL entries under base_url. Dynamic per-model URLs (/posts/{post}) aren’t included: this crate has no visibility into an app’s own models to enumerate them. Combine this function’s output with the app’s own dynamically-built entries.
response
build_xml, wrapped as a ready-to-return axum response with the right content type - Laravel’s own package’s Sitemap::render() equivalent. Not auto-mounted anywhere; a route handler calls this directly (see this crate’s own doc comment for a full example).