Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scan output build folder instead of src/routes #1

Closed
NEO97online opened this issue Jun 17, 2021 · 5 comments · Fixed by #2
Closed

Scan output build folder instead of src/routes #1

NEO97online opened this issue Jun 17, 2021 · 5 comments · Fixed by #2
Assignees
Labels
enhancement New feature or request

Comments

@NEO97online
Copy link

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.

@bartholomej
Copy link
Owner

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

@bartholomej bartholomej pinned this issue Jun 21, 2021
@bartholomej bartholomej added the enhancement New feature or request label Jun 21, 2021
@bartholomej bartholomej self-assigned this Jun 21, 2021
@tonprince
Copy link

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

@bartholomej bartholomej unpinned this issue Jul 14, 2021
@bartholomej
Copy link
Owner

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

@100lvlmaster
Copy link

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

@bartholomej
Copy link
Owner

@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 ;)

bartholomej pushed a commit that referenced this issue Aug 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants