Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Scan output build folder instead of src/routes #1

@NEO97online

Description

@NEO97online

Currently this package scans the src/routes folder to generate the sitemap. This works fine for normal pages, but it doesn't work for dynamically generated pages like [slug].svelte, etc.

Instead, we should scan the build folder. Here's a demo script that does this:

import fs from "fs";
import fg from "fast-glob";
import { create } from "xmlbuilder2";
import pkg from "./package.json";

const getUrl = (url) => {
	const trimmed = url.slice(6).replace("index.html", "");
	return `${pkg.url}/${trimmed}`;
};

async function createSitemap() {
	const sitemap = create({ version: "1.0" }).ele("urlset", {
		xmlns: "http://www.sitemaps.org/schemas/sitemap/0.9"
	});

	const pages = await fg(["build/**/*.html"]);

	pages.forEach((page) => {
		const url = sitemap.ele("url");
		url.ele("loc").txt(getUrl(page));
		url.ele("changefreq").txt("weekly");
	});

	const xml = sitemap.end({ prettyPrint: true });

	fs.writeFileSync("build/sitemap.xml", xml);
}

createSitemap();

We should use a method like this instead of routes, as it will capture the actual output from Svelte, which can differ greatly from the source files.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions