Scaffold SSR site with CMS/blog/gallery/SEO admin and deploy configs for montech.ir. Co-authored-by: Cursor <cursoragent@cursor.com>
41 lines
1.2 KiB
TypeScript
41 lines
1.2 KiB
TypeScript
export default defineEventHandler(async (event) => {
|
|
const config = useRuntimeConfig();
|
|
const site = config.public.siteUrl || "https://montech.ir";
|
|
const apiBase = config.apiBase || "http://127.0.0.1:3050";
|
|
|
|
let posts: Array<{ slug: string; updatedAt?: string; publishedAt?: string }> = [];
|
|
try {
|
|
posts = await $fetch(`${apiBase}/api/public/blog`);
|
|
} catch {
|
|
posts = [];
|
|
}
|
|
|
|
const staticUrls = ["", "/blog", "/gallery"];
|
|
const urls = [
|
|
...staticUrls.map((path) => ({ loc: `${site}${path || "/"}`, changefreq: "weekly", priority: path === "" ? "1.0" : "0.8" })),
|
|
...posts.map((p) => ({
|
|
loc: `${site}/blog/${p.slug}`,
|
|
changefreq: "monthly",
|
|
priority: "0.6",
|
|
lastmod: p.updatedAt || p.publishedAt,
|
|
})),
|
|
];
|
|
|
|
const body = `<?xml version="1.0" encoding="UTF-8"?>
|
|
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
|
${urls
|
|
.map(
|
|
(u) => ` <url>
|
|
<loc>${u.loc}</loc>
|
|
${"lastmod" in u && u.lastmod ? `<lastmod>${new Date(u.lastmod as string).toISOString()}</lastmod>` : ""}
|
|
<changefreq>${u.changefreq}</changefreq>
|
|
<priority>${u.priority}</priority>
|
|
</url>`,
|
|
)
|
|
.join("\n")}
|
|
</urlset>`;
|
|
|
|
setHeader(event, "Content-Type", "application/xml");
|
|
return body;
|
|
});
|