126 lines
4.0 KiB
TypeScript
126 lines
4.0 KiB
TypeScript
import { locales, defaultLocale, Locale } from './i18n';
|
|
import { getTranslations } from './translations';
|
|
|
|
export interface SitemapEntry {
|
|
url: string;
|
|
lastModified: Date;
|
|
changeFrequency: 'always' | 'hourly' | 'daily' | 'weekly' | 'monthly' | 'yearly' | 'never';
|
|
priority: number;
|
|
alternates?: {
|
|
languages: Record<string, string>;
|
|
};
|
|
}
|
|
|
|
export function generateStaticPages(baseUrl: string): SitemapEntry[] {
|
|
const staticPages = [
|
|
{ path: '', priority: 1.0, changeFrequency: 'daily' as const },
|
|
{ path: '/blog', priority: 0.8, changeFrequency: 'weekly' as const },
|
|
{ path: '/contact', priority: 0.6, changeFrequency: 'monthly' as const },
|
|
{ path: '/products', priority: 0.7, changeFrequency: 'weekly' as const },
|
|
];
|
|
|
|
const entries: SitemapEntry[] = [];
|
|
|
|
locales.forEach((locale) => {
|
|
staticPages.forEach((page) => {
|
|
const url =
|
|
locale === defaultLocale
|
|
? `${baseUrl}${page.path}`
|
|
: `${baseUrl}/${locale}${page.path}`;
|
|
|
|
entries.push({
|
|
url,
|
|
lastModified: new Date(),
|
|
changeFrequency: page.changeFrequency,
|
|
priority: page.priority,
|
|
alternates: {
|
|
languages: Object.fromEntries(
|
|
locales.map((loc) => [
|
|
loc,
|
|
loc === defaultLocale
|
|
? `${baseUrl}${page.path}`
|
|
: `${baseUrl}/${loc}${page.path}`,
|
|
]),
|
|
),
|
|
},
|
|
});
|
|
});
|
|
});
|
|
|
|
return entries;
|
|
}
|
|
|
|
export function generateBlogPages(baseUrl: string): SitemapEntry[] {
|
|
// Get blog post slugs from your translations or database
|
|
const blogSlugs = ['featured', 'post1', 'post2', 'post3', 'post4', 'post5'];
|
|
|
|
const entries: SitemapEntry[] = [];
|
|
|
|
locales.forEach((locale) => {
|
|
blogSlugs.forEach((slug) => {
|
|
const url =
|
|
locale === defaultLocale
|
|
? `${baseUrl}/blog/${slug}`
|
|
: `${baseUrl}/${locale}/blog/${slug}`;
|
|
|
|
entries.push({
|
|
url,
|
|
lastModified: new Date(),
|
|
changeFrequency: 'monthly',
|
|
priority: 0.7,
|
|
alternates: {
|
|
languages: Object.fromEntries(
|
|
locales.map((loc) => [
|
|
loc,
|
|
loc === defaultLocale
|
|
? `${baseUrl}/blog/${slug}`
|
|
: `${baseUrl}/${loc}/blog/${slug}`,
|
|
]),
|
|
),
|
|
},
|
|
});
|
|
});
|
|
});
|
|
|
|
return entries;
|
|
}
|
|
|
|
export function generateSitemap(baseUrl: string): SitemapEntry[] {
|
|
const staticPages = generateStaticPages(baseUrl);
|
|
const blogPages = generateBlogPages(baseUrl);
|
|
|
|
return [...staticPages, ...blogPages];
|
|
}
|
|
|
|
// Helper function to get all available blog post slugs
|
|
export function getBlogPostSlugs(): string[] {
|
|
// In a real application, you might fetch this from a CMS or database
|
|
// For now, we'll use the static list from your existing blog structure
|
|
return ['featured', 'post1', 'post2', 'post3', 'post4', 'post5'];
|
|
}
|
|
|
|
// Helper function to validate if a blog post exists
|
|
export function validateBlogPost(slug: string, locale: Locale): boolean {
|
|
const translations = getTranslations(locale);
|
|
const posts = translations.blog.posts;
|
|
return slug in posts;
|
|
}
|
|
|
|
export function getSitemapStats(baseUrl: string): {
|
|
urlCount: number;
|
|
staticPages: number;
|
|
blogPages: number;
|
|
languages: number;
|
|
} {
|
|
const staticPages = generateStaticPages(baseUrl);
|
|
const blogPages = generateBlogPages(baseUrl);
|
|
const sitemap = [...staticPages, ...blogPages];
|
|
|
|
return {
|
|
urlCount: sitemap.length,
|
|
staticPages: staticPages.length,
|
|
blogPages: blogPages.length,
|
|
languages: locales.length,
|
|
};
|
|
}
|