diff --git a/app/fastify-entry.ts b/app/fastify-entry.ts index 2127835..dcbac11 100644 --- a/app/fastify-entry.ts +++ b/app/fastify-entry.ts @@ -3,6 +3,7 @@ import type { Theme } from "@/contexts/ThemeContext"; import { createHandler } from "@universal-middleware/fastify"; import { telefuncHandler } from "./server/telefunc-handler"; import { vikeHandler } from "./server/vike-handler"; +import { sitemap } from "./services/Sitemap"; import fastifyCookie from "@fastify/cookie"; import { fileURLToPath } from "node:url"; import { dirname } from "node:path"; @@ -34,6 +35,8 @@ declare global { async function startServer() { const app = Fastify(); + sitemap.generateSitemap(); + app.register(fastifyCookie, { secret: "todo", hook: "onRequest", diff --git a/app/public/sitemap.xml b/app/public/sitemap.xml new file mode 100644 index 0000000..edf58cf --- /dev/null +++ b/app/public/sitemap.xml @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/app/services/Sitemap.ts b/app/services/Sitemap.ts new file mode 100644 index 0000000..ad2e69e --- /dev/null +++ b/app/services/Sitemap.ts @@ -0,0 +1,77 @@ +import { navigation } from "@/lib/navigation"; +import path from "path"; +import fs from "fs"; + +const __dirname = path.resolve(); + +const getBaseUrl = () => { + if (process.env.NODE_ENV === "production") { + return "https://memento-dev.fr"; + } + + return `http://localhost:${process.env.PORT || 3000}`; +}; + +type SitemapElement = { + location: string; + lastmod: string; + priority: string; +}; + +class Sitemap { + private readonly sitemapPath = path.join(__dirname, "public", "sitemap.xml"); + private readonly lastModified = new Date().toISOString(); + private readonly baseUrl = getBaseUrl(); + + private urls: SitemapElement[] = []; + private sitemap: string = ""; + + private static instance: Sitemap; + + private constructor() {} + + public static getInstance(): Sitemap { + if (!Sitemap.instance) { + Sitemap.instance = new Sitemap(); + } + return Sitemap.instance; + } + + private resetMemory(): void { + this.sitemap = ""; + this.urls = []; + } + + private prependSitemap(): void { + this.sitemap = ``; + this.sitemap += ``; + } + + private appendSitemap(): void { + this.sitemap += ``; + } + + private buildSitemap(): void { + this.prependSitemap(); + this.appendSitemap(); + } + + private saveSitemap(): void { + fs.writeFileSync(this.sitemapPath, this.sitemap, "utf8"); + } + + private loadUrls(): void {} + + public generateSitemap(): void { + console.log("Generating sitemap..."); + + this.resetMemory(); + this.loadUrls(); + this.buildSitemap(); + this.saveSitemap(); + + console.log("Sitemap generated successfully."); + } +} + +export const sitemap = Sitemap.getInstance();