Skip to main content

logicaffeine_web/
sitemap.rs

1//! Sitemap + prerender route enumeration for SEO.
2//!
3//! [`prerender_routes`] is the canonical list of crawlable pages: the SSG build
4//! prerenders exactly these routes (served via `/api/static_routes`), the sitemap
5//! is generated from them, and the tests below hold `public/sitemap.xml` byte-equal
6//! to [`generate_sitemap`] so the shipped file can never go stale.
7//!
8//! # Usage
9//!
10//! ```
11//! use logicaffeine_web::sitemap::generate_sitemap;
12//! let xml = generate_sitemap();
13//! assert!(!xml.is_empty());
14//! ```
15
16use std::fmt::Write;
17
18const BASE_URL: &str = "https://logicaffeine.com";
19const CURRENT_DATE: &str = "2026-11-02";
20
21/// Sitemap entry with URL and metadata
22pub struct SitemapEntry {
23    pub path: String,
24    pub changefreq: &'static str,
25    pub priority: f32,
26}
27
28/// The curated static pages (path, changefreq, priority).
29pub fn get_static_routes() -> Vec<SitemapEntry> {
30    [
31        ("/", "weekly", 1.0),
32        ("/guide", "monthly", 0.9),
33        ("/learn", "monthly", 0.9),
34        ("/crates", "monthly", 0.8),
35        ("/studio", "weekly", 0.8),
36        ("/benchmarks", "weekly", 0.8),
37        ("/contact", "monthly", 0.8),
38        ("/roadmap", "monthly", 0.7),
39        ("/news", "weekly", 0.7),
40        ("/registry", "weekly", 0.6),
41        ("/privacy", "yearly", 0.3),
42        ("/terms", "yearly", 0.3),
43    ]
44    .into_iter()
45    .map(|(path, changefreq, priority)| SitemapEntry {
46        path: path.to_string(),
47        changefreq,
48        priority,
49    })
50    .collect()
51}
52
53/// One entry per published news article, derived from the article registry so a
54/// new post can never be forgotten here.
55pub fn get_news_routes() -> Vec<SitemapEntry> {
56    crate::ui::pages::news::get_articles()
57        .into_iter()
58        .map(|article| SitemapEntry {
59            path: format!("/news/{}", article.slug),
60            changefreq: "monthly",
61            priority: 0.6,
62        })
63        .collect()
64}
65
66/// Every route the SSG build prerenders — the sitemap in path form. `/workspace/:subject` and
67/// `/registry/package/:name` (not enumerable at build time) stay client-side.
68pub fn prerender_routes() -> Vec<String> {
69    get_static_routes()
70        .into_iter()
71        .chain(get_news_routes())
72        .map(|entry| entry.path)
73        .collect()
74}
75
76/// Generate the complete sitemap XML
77pub fn generate_sitemap() -> String {
78    let mut xml = String::new();
79
80    writeln!(xml, r#"<?xml version="1.0" encoding="UTF-8"?>"#).unwrap();
81    writeln!(
82        xml,
83        r#"<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">"#
84    )
85    .unwrap();
86
87    writeln!(xml, "  <!-- Main Pages -->").unwrap();
88    for entry in get_static_routes() {
89        write_url_entry(&mut xml, entry);
90    }
91
92    writeln!(xml, "\n  <!-- News Articles -->").unwrap();
93    for entry in get_news_routes() {
94        write_url_entry(&mut xml, entry);
95    }
96
97    writeln!(xml, "</urlset>").unwrap();
98
99    xml
100}
101
102fn write_url_entry(xml: &mut String, entry: SitemapEntry) {
103    writeln!(xml, "  <url>").unwrap();
104    writeln!(xml, "    <loc>{}{}</loc>", BASE_URL, entry.path).unwrap();
105    writeln!(xml, "    <lastmod>{}</lastmod>", CURRENT_DATE).unwrap();
106    writeln!(xml, "    <changefreq>{}</changefreq>", entry.changefreq).unwrap();
107    writeln!(xml, "    <priority>{:.1}</priority>", entry.priority).unwrap();
108    writeln!(xml, "  </url>").unwrap();
109}
110
111#[cfg(test)]
112#[path = "../../../crates/logicaffeine_tests/unit/logicaffeine_web/sitemap/tests.rs"]
113mod tests;