Created
January 19, 2020 11:16
-
-
Save yafkari/05816ba6805de0384c22e070fc321baf to your computer and use it in GitHub Desktop.
Simple sitemap.xml file for Svelte/Sapper blog project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import posts from "./blog/_posts"; | |
const fs = require('fs'); | |
const BASE_URL = "https://www.zechtyounes.tech"; // TO CHANGE | |
const pages = [""]; | |
fs.readdirSync("./src/routes").forEach(file => { | |
file = file.split('.')[0]; | |
if (file.charAt(0) !== '_' && file !== "sitemap" && file !== "index") { | |
pages.push(file); | |
} | |
}); | |
const render = (pages, posts) => `<?xml version="1.0" encoding="UTF-8" ?> | |
<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"> | |
${pages | |
.map( | |
page => ` | |
<url><loc>${BASE_URL}/${page}</loc><priority>0.85</priority></url> | |
` | |
) | |
.join("\n")} | |
${posts | |
.map( | |
post => ` | |
<url> | |
<loc>${BASE_URL}/blog/${post.slug}</loc> | |
<priority>0.69</priority> | |
</url> | |
` | |
) | |
.join("\n")} | |
</urlset> | |
`; | |
export function get(req, res, next) { | |
res.setHeader("Cache-Control", `max-age=0, s-max-age=${600}`); // 10 minutes | |
res.setHeader("Content-Type", "application/rss+xml"); | |
const sitemap = render(pages, posts); | |
res.end(sitemap); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If using Sapper export, you probably need to fetch sitemap.xml so that it gets exported. Since the
--entry
option is only for directories, something like sveltejs/sapper#461 (comment) might be needed