Skip to content

Instantly share code, notes, and snippets.

@yafkari
Created January 19, 2020 11:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yafkari/05816ba6805de0384c22e070fc321baf to your computer and use it in GitHub Desktop.
Save yafkari/05816ba6805de0384c22e070fc321baf to your computer and use it in GitHub Desktop.
Simple sitemap.xml file for Svelte/Sapper blog project
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);
}
@wyozi
Copy link

wyozi commented Apr 26, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment