feat: update product categories data structure and implement sitemap generation
- Refactored product categories to use a new data structure. - Updated API routes to fetch product categories from the new data source. - Implemented sitemap generation script using dotenv for environment variables. - Added sitemap generation endpoint to the API. - Removed old sitemap file and replaced it with a dynamically generated one.
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
'use server';
|
||||
|
||||
// app/(api)/sitemap/route.ts
|
||||
import { NextResponse } from 'next/server';
|
||||
import { productCategoriesData } from '../product_categories/data';
|
||||
|
||||
const HOST = 'http://localhost:3000';
|
||||
|
||||
const pages = [
|
||||
'/',
|
||||
'/about-us',
|
||||
'/contact-us',
|
||||
'/blog',
|
||||
'/product-categories',
|
||||
'/projects',
|
||||
];
|
||||
|
||||
const locales = ['en', 'fa'];
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
pages.push(
|
||||
...productCategoriesData.map(
|
||||
(productCategory) => `/product-categories/${productCategory.id}`,
|
||||
),
|
||||
);
|
||||
|
||||
const urls = pages.flatMap((page) =>
|
||||
locales.map((locale) => {
|
||||
const loc = `/${locale}`;
|
||||
return `
|
||||
<url>
|
||||
<loc>${HOST}${loc}${page}</loc>
|
||||
<changefreq>weekly</changefreq>
|
||||
<priority>0.8</priority>
|
||||
</url>
|
||||
`;
|
||||
}),
|
||||
);
|
||||
|
||||
const xml = `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
||||
${urls.join('\n')}
|
||||
</urlset>`;
|
||||
|
||||
return new NextResponse(xml, {
|
||||
headers: {
|
||||
'Content-Type': 'application/xml',
|
||||
},
|
||||
});
|
||||
} catch (err) {
|
||||
console.error('Sitemap generation failed:', err);
|
||||
return new NextResponse('Internal Server Errorsssssss', { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user