bartholomej / svelte-sitemap

Sitemap generator for SvelteKit. Small helper which scans your SvelteKit routes and generates static sitemap.xml

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Scan output build folder instead of src/routes

NEO97online opened this issue · comments

commented

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.

commented

Thank you @auderer
It was just proof-of-concept and it worked well for my case. But you're right this should run as postbuild
Here is PR #2

You can install next version like this:
yarn add svelte-sitemap@next

commented

I am using the vercel adapter with its own build output folder. Would be great if we can customize the build dir.

commented

@tonprince Good point! But I consider this issue closed, so I made a new one. Let's discuss there #3
cc @100lvlmaster

This approach is nice but the vercel adapter does not generate html files

commented

@100lvlmaster You are right, the Vercel adapter does not generate static files and my library is intended just for the Static Adapter.

I personally do not use the Vercel plugin but maybe it could be used only in a specific combination with a static adapter... (I know it won't be a very common case)

But of course if there are no static files, then my library will not work either.
If you have an idea, I'd love to hear it ;)