Compare commits

..

No commits in common. "9aeaec0821bfcdf7ace73cbf4020f6955412ee2d" and "7b88376057da80444164f6f33d7a1ac8698dd145" have entirely different histories.

5 changed files with 0 additions and 162 deletions

1
.gitignore vendored
View File

@ -2,5 +2,4 @@
app/.pnpm-store
app/node_modules
app/dist
app/public/sitemap.xml
**/*~lock*

View File

@ -3,7 +3,6 @@ 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";
@ -35,8 +34,6 @@ declare global {
async function startServer() {
const app = Fastify();
sitemap.generateSitemap();
app.register(fastifyCookie, {
secret: "todo",
hook: "onRequest",

View File

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

View File

@ -1,154 +0,0 @@
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 pagesPath = path.join(__dirname, "pages");
private readonly dataPath = path.join(__dirname, "data");
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 = `<?xml version="1.0" encoding="UTF-8"?>`;
this.sitemap += `<urlset xmlns="http://www.sitemaps.org/schemas/sitemap-image/1.1">`;
}
private appendSitemap(): void {
this.sitemap += `</urlset>`;
}
private addSitemapElement(url: SitemapElement): void {
this.sitemap += `<url>`;
this.sitemap += `<loc>${url.location}</loc>`;
this.sitemap += `<lastmod>${url.lastmod || this.lastModified}</lastmod>`;
this.sitemap += `<priority>${url.priority}</priority>`;
this.sitemap += `</url>`;
}
private buildSitemap(): void {
this.prependSitemap();
this.urls.forEach(this.addSitemapElement.bind(this));
this.appendSitemap();
}
private saveSitemap(): void {
fs.writeFileSync(this.sitemapPath, this.sitemap, "utf8");
}
private loadPriority(href: string): string {
const isRootUrl = ["/", ""].includes(href);
if (isRootUrl) return "1.0";
const countOfSlashes = (href.match(/\//g) || []).length;
return (1 - countOfSlashes * 0.1).toFixed(1);
}
private loadLastModified(href: string): string {
return this.lastModified;
}
private getFileServerLocation(href: string) {
const jsxHref = ["/politique-de-confidentialite", "/mentions-legales"];
const isJsxFile = jsxHref.includes(href);
if (isJsxFile) {
return path.join(this.pagesPath, href.replace("/", ""), "+Page.tsx");
}
return path.join(this.pagesPath, href.replace("/", ""), "page.md");
}
private loadSubitems(subitems: (typeof navigation)[number]["links"][number]["subitems"]): void {
subitems.forEach((subitem) => {
const fileLocation = this.getFileServerLocation(subitem.href);
console.log("File location:", fileLocation);
const priority = this.loadPriority(subitem.href);
const lastmod = this.loadLastModified(subitem.href);
const location = `${this.baseUrl}${subitem.href}`;
this.urls.push({
location,
lastmod,
priority,
});
});
}
private loadSection(section: (typeof navigation)[number]): void {
section.links.forEach((link) => {
if (link.subitems.length > 0) {
return this.loadSubitems(link.subitems);
}
const priority = this.loadPriority(link.href);
const lastmod = this.loadLastModified(link.href);
const location = `${this.baseUrl}${link.href}`;
this.urls.push({
location,
lastmod,
priority,
});
});
}
private loadUrls(): void {
navigation.forEach(this.loadSection.bind(this));
this.urls = Array.from(new Set(this.urls)).sort((a, b) => {
return a.location.localeCompare(b.location);
});
console.log("Loaded URLs:", this.urls);
}
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();

View File

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