feat: Add new script for generating sitemap

This commit is contained in:
Gauthier Daniels 2025-04-18 17:43:25 +02:00
parent a77cd5d5c9
commit 3a84a78c43
3 changed files with 43 additions and 1 deletions

View File

@ -1,6 +1,7 @@
{ {
"scripts": { "scripts": {
"dev": "tsx ./fastify-entry.ts", "dev": "tsx ./fastify-entry.ts",
"dev:sitemap": "tsx --watch ./sitemap.ts",
"build": "vike build", "build": "vike build",
"preview": "cross-env NODE_ENV=production tsx ./fastify-entry.ts", "preview": "cross-env NODE_ENV=production tsx ./fastify-entry.ts",
"lint": "eslint .", "lint": "eslint .",

View File

@ -19,6 +19,8 @@ type SitemapElement = {
}; };
class Sitemap { class Sitemap {
private readonly pagesPath = path.join(__dirname, "pages");
private readonly dataPath = path.join(__dirname, "data");
private readonly sitemapPath = path.join(__dirname, "public", "sitemap.xml"); private readonly sitemapPath = path.join(__dirname, "public", "sitemap.xml");
private readonly lastModified = new Date().toISOString(); private readonly lastModified = new Date().toISOString();
private readonly baseUrl = getBaseUrl(); private readonly baseUrl = getBaseUrl();
@ -51,8 +53,11 @@ class Sitemap {
this.sitemap += `</urlset>`; this.sitemap += `</urlset>`;
} }
private addSitemapElement(url: SitemapElement): void {}
private buildSitemap(): void { private buildSitemap(): void {
this.prependSitemap(); this.prependSitemap();
this.urls.forEach(this.addSitemapElement.bind(this));
this.appendSitemap(); this.appendSitemap();
} }
@ -60,7 +65,40 @@ class Sitemap {
fs.writeFileSync(this.sitemapPath, this.sitemap, "utf8"); fs.writeFileSync(this.sitemapPath, this.sitemap, "utf8");
} }
private loadUrls(): void {} private loadPriority(href: string): string {
const isRootUrl = ["/", ""].includes(href);
const isMainUrl = ["/docs", "/certifications", "/politique-de-confidentialite", "/mentions-legales"].includes(href);
if (isRootUrl) return "1.0";
if (isMainUrl) return "0.9";
return "0.8";
}
private loadLastModified(href: string): string {}
private loadFile(href: string) {}
private loadUrls(): void {
this.urls = navigation.flatMap((item) => {
return item.links
.map((link) => {
const file = this.loadFile(link.href);
if (!file) {
console.warn(`File not found for URL: ${link.href}`);
return null;
}
return {
location: `${this.baseUrl}${link.href}`,
lastmod: this.loadLastModified(link.href),
priority: this.loadPriority(link.href),
};
})
.filter((url) => url !== null);
});
console.log("Loaded URLs:", this.urls);
}
public generateSitemap(): void { public generateSitemap(): void {
console.log("Generating sitemap..."); console.log("Generating sitemap...");

3
app/sitemap.ts Normal file
View File

@ -0,0 +1,3 @@
import { sitemap } from "./services/Sitemap";
sitemap.generateSitemap();