import type { MetadataRoute } from 'next'; import { i18n } from './dictionaries'; import fs from 'fs'; import path from 'path'; export default function sitemap(): MetadataRoute.Sitemap { const baseUrl = 'https://moygroup.com.tr'; // Dynamically scan the app/[lang] folder for pages const langDir = path.join(process.cwd(), 'app/[lang]'); let subfolders: string[] = []; try { if (fs.existsSync(langDir)) { subfolders = fs.readdirSync(langDir).filter((file) => { const filePath = path.join(langDir, file); if (fs.statSync(filePath).isDirectory()) { // Only include folders that contain a page.tsx file return fs.existsSync(path.join(filePath, 'page.tsx')); } return false; }); } } catch (error) { console.error('Error reading directories for sitemap:', error); } // Base page paths: home page ('') + dynamic folders const pages = ['', ...subfolders.map((folder) => `/${folder}`)]; const sitemapEntries: MetadataRoute.Sitemap = []; for (const page of pages) { // Generate language alternates for this page const alternatesLanguages: Record = {}; for (const locale of i18n.locales) { alternatesLanguages[locale] = `${baseUrl}/${locale}${page}`; } // Add entries for all locales, providing localized alternates for each for (const locale of i18n.locales) { sitemapEntries.push({ url: `${baseUrl}/${locale}${page}`, lastModified: new Date(), changeFrequency: page === '' ? 'weekly' : 'monthly', priority: page === '' ? 1.0 : 0.8, alternates: { languages: alternatesLanguages, }, }); } } return sitemapEntries; }