77 lines
2.0 KiB
TypeScript
77 lines
2.0 KiB
TypeScript
import { MetadataRoute } from 'next';
|
|
import { getAllPages, getAllPosts, getAllProducts, getAllCategories } from '../lib/data';
|
|
|
|
export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
|
|
const baseUrl = process.env.SITE_URL || 'https://klz-cables.com';
|
|
const urls: MetadataRoute.Sitemap = [];
|
|
|
|
// Add homepage
|
|
urls.push({
|
|
url: baseUrl,
|
|
lastModified: new Date(),
|
|
changeFrequency: 'daily',
|
|
priority: 1,
|
|
});
|
|
|
|
// Add all pages
|
|
try {
|
|
const pages = await getAllPages();
|
|
pages.forEach(page => {
|
|
urls.push({
|
|
url: `${baseUrl}/${page.slug}`,
|
|
lastModified: new Date(page.updatedAt || Date.now()),
|
|
changeFrequency: 'weekly',
|
|
priority: 0.8,
|
|
});
|
|
});
|
|
} catch (error) {
|
|
console.warn('Could not fetch pages for sitemap:', error);
|
|
}
|
|
|
|
// Add blog posts
|
|
try {
|
|
const posts = await getAllPosts();
|
|
posts.forEach(post => {
|
|
urls.push({
|
|
url: `${baseUrl}/blog/${post.slug}`,
|
|
lastModified: new Date(post.updatedAt || Date.now()),
|
|
changeFrequency: 'weekly',
|
|
priority: 0.6,
|
|
});
|
|
});
|
|
} catch (error) {
|
|
console.warn('Could not fetch posts for sitemap:', error);
|
|
}
|
|
|
|
// Add products
|
|
try {
|
|
const products = await getAllProducts();
|
|
products.forEach(product => {
|
|
urls.push({
|
|
url: `${baseUrl}/product/${product.slug}`,
|
|
lastModified: new Date(product.updatedAt || Date.now()),
|
|
changeFrequency: 'monthly',
|
|
priority: 0.5,
|
|
});
|
|
});
|
|
} catch (error) {
|
|
console.warn('Could not fetch products for sitemap:', error);
|
|
}
|
|
|
|
// Add product categories
|
|
try {
|
|
const categories = await getAllCategories();
|
|
categories.forEach(category => {
|
|
urls.push({
|
|
url: `${baseUrl}/product-category/${category.slug}`,
|
|
lastModified: new Date(Date.now()),
|
|
changeFrequency: 'monthly',
|
|
priority: 0.4,
|
|
});
|
|
});
|
|
} catch (error) {
|
|
console.warn('Could not fetch categories for sitemap:', error);
|
|
}
|
|
|
|
return urls;
|
|
} |