2026-10-22 17:18:17 +03:30
|
|
|
import type { PublicProductRef } from "@imen/shared";
|
|
|
|
|
|
2026-10-07 00:45:51 +03:30
|
|
|
interface SitemapEntry {
|
|
|
|
|
loc: string;
|
|
|
|
|
priority: string;
|
2026-10-22 17:18:17 +03:30
|
|
|
lastmod?: string;
|
2026-10-07 00:45:51 +03:30
|
|
|
}
|
|
|
|
|
|
2026-10-21 20:31:05 +03:30
|
|
|
export default defineEventHandler(async (event) => {
|
2026-10-22 17:18:17 +03:30
|
|
|
const [categories, products] = await Promise.all([
|
|
|
|
|
fetchCategories(),
|
|
|
|
|
serverApi<PublicProductRef[]>("/public/products"),
|
|
|
|
|
]);
|
2026-10-21 20:31:05 +03:30
|
|
|
const pages: SitemapEntry[] = [
|
|
|
|
|
{ loc: "/", priority: "1" },
|
|
|
|
|
...categories.map((c) => ({ loc: `/categories/${c.slug}`, priority: "0.8" })),
|
2026-10-22 17:18:17 +03:30
|
|
|
...products.map((p) => ({ loc: `/products/${p.slug}`, priority: "0.6", lastmod: p.updatedAt })),
|
2026-10-21 20:31:05 +03:30
|
|
|
];
|
2026-10-06 16:28:05 +03:30
|
|
|
|
|
|
|
|
setHeader(event, "Content-Type", "application/xml; charset=utf-8");
|
|
|
|
|
const lastmod = new Date().toISOString();
|
|
|
|
|
const urls = pages
|
|
|
|
|
.map(
|
|
|
|
|
(p) =>
|
2026-10-22 17:18:17 +03:30
|
|
|
`<url>\n<loc>${absUrl(p.loc)}</loc>\n<lastmod>${p.lastmod ?? lastmod}</lastmod>\n<changefreq>weekly</changefreq>\n<priority>${p.priority}</priority>\n</url>`,
|
2026-10-06 16:28:05 +03:30
|
|
|
)
|
|
|
|
|
.join("\n");
|
|
|
|
|
return `<?xml version="1.0" encoding="UTF-8"?>\n<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n${urls}\n</urlset>\n`;
|
|
|
|
|
});
|