|
| 1 | +// app/sitemap.xml/route.ts |
| 2 | +import { type NextRequest } from 'next/server'; |
| 3 | + |
| 4 | +import { |
| 5 | + ENTITY_CONFIG, |
| 6 | + ENTITY_CONFIG_TYPE, |
| 7 | + getSiteMapConfig, |
| 8 | + isSitemapEnabled, |
| 9 | +} from '@/lib/utils'; |
| 10 | + |
| 11 | +const getAllEntityCounts = async (): Promise<Record<string, number>> => { |
| 12 | + const counts: Record<string, number> = {}; |
| 13 | + |
| 14 | + const countPromises: Promise<{ entityName: string; count: number }>[] = []; |
| 15 | + |
| 16 | + Object.entries(ENTITY_CONFIG).forEach(([entityName, config]) => { |
| 17 | + if (config.source === 'graphql' && config.graphqlQuery) { |
| 18 | + countPromises.push(getGraphqlEntityCount(entityName, config)); |
| 19 | + } |
| 20 | + if (config.source === 'search' && config.endpoint) { |
| 21 | + countPromises.push(getSearchEntityCount(entityName, 5, 1)); |
| 22 | + } |
| 23 | + }); |
| 24 | + |
| 25 | + const results = await Promise.all(countPromises); |
| 26 | + |
| 27 | + results.forEach(({ entityName, count }) => { |
| 28 | + counts[entityName] = count; |
| 29 | + }); |
| 30 | + |
| 31 | + return counts; |
| 32 | +}; |
| 33 | + |
| 34 | +export async function getGraphqlEntityCount( |
| 35 | + entity: string, |
| 36 | + config: ENTITY_CONFIG_TYPE[string] |
| 37 | +): Promise<{ entityName: string; count: number; list: any }> { |
| 38 | + try { |
| 39 | + const response = await fetch( |
| 40 | + `${process.env.FEATURE_SITEMAP_BACKEND_BASE_URL}/graphql`, |
| 41 | + { |
| 42 | + method: 'POST', |
| 43 | + headers: { |
| 44 | + 'Content-Type': 'application/json', |
| 45 | + }, |
| 46 | + body: JSON.stringify({ |
| 47 | + query: config.graphqlQuery, |
| 48 | + variables: {}, |
| 49 | + }), |
| 50 | + } |
| 51 | + ); |
| 52 | + const data = await response.json(); |
| 53 | + |
| 54 | + return { |
| 55 | + entityName: entity, |
| 56 | + count: data?.data?.[config.queryResKey as string]?.length || 0, |
| 57 | + list: data?.data?.[config.queryResKey as string] || [], |
| 58 | + }; |
| 59 | + } catch (error) { |
| 60 | + console.error(`Error fetching count for ${entity}:`, error); |
| 61 | + return { entityName: entity, count: 0, list: [] }; |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +export async function getSearchEntityCount( |
| 66 | + entity: string, |
| 67 | + size: number, |
| 68 | + page: number |
| 69 | +): Promise<{ entityName: string; count: number; list: any }> { |
| 70 | + try { |
| 71 | + const config = ENTITY_CONFIG[entity]; |
| 72 | + const response = await fetch( |
| 73 | + `${process.env.FEATURE_SITEMAP_BACKEND_BASE_URL}${config.endpoint}?sort=recent&size=${size}&page=${page}`, |
| 74 | + { |
| 75 | + method: 'GET', |
| 76 | + headers: { |
| 77 | + 'Content-Type': 'application/json', |
| 78 | + }, |
| 79 | + next: { revalidate: 3600 }, |
| 80 | + } |
| 81 | + ); |
| 82 | + const data = await response.json(); |
| 83 | + return { entityName: entity, count: data.total, list: data.results }; |
| 84 | + } catch (error) { |
| 85 | + console.error(`Error fetching count for ${entity}:`, error); |
| 86 | + return { entityName: entity, count: 0, list: [] }; |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +function generateStaticUrls(): string { |
| 91 | + const baseUrl = process.env.NEXTAUTH_URL; |
| 92 | + |
| 93 | + const staticPages = [ |
| 94 | + { path: '', priority: '1.0', changefreq: 'daily' }, |
| 95 | + { path: '/datasets', priority: '0.9', changefreq: 'daily' }, |
| 96 | + { path: '/usecases', priority: '0.8', changefreq: 'weekly' }, |
| 97 | + { path: '/publishers', priority: '0.7', changefreq: 'weekly' }, |
| 98 | + { path: '/sectors', priority: '0.7', changefreq: 'weekly' }, |
| 99 | + ]; |
| 100 | + |
| 101 | + return staticPages |
| 102 | + .map( |
| 103 | + (page) => ` |
| 104 | + <url> |
| 105 | + <loc>${baseUrl}${page.path}</loc> |
| 106 | + <changefreq>${page.changefreq}</changefreq> |
| 107 | + <priority>${page.priority}</priority> |
| 108 | + </url>` |
| 109 | + ) |
| 110 | + .join(''); |
| 111 | +} |
| 112 | + |
| 113 | +function generateSitemapIndex( |
| 114 | + sitemapUrls: string[], |
| 115 | + staticUrls: string |
| 116 | +): string { |
| 117 | + const sitemapEntries = sitemapUrls |
| 118 | + .map( |
| 119 | + (url) => |
| 120 | + ` |
| 121 | + <url> |
| 122 | + <loc>${url}</loc> |
| 123 | + <lastmod>${new Date().toISOString()}</lastmod> |
| 124 | + </url>` |
| 125 | + ) |
| 126 | + .join(''); |
| 127 | + |
| 128 | + return `<?xml version="1.0" encoding="UTF-8"?>\n |
| 129 | + <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">${staticUrls}\n${sitemapEntries}\n</urlset>`; |
| 130 | +} |
| 131 | + |
| 132 | +export async function GET(request: NextRequest) { |
| 133 | + // Check if sitemaps are enabled via feature flag |
| 134 | + if (!isSitemapEnabled()) { |
| 135 | + return new Response('Sitemaps are not enabled', { status: 404 }); |
| 136 | + } |
| 137 | + |
| 138 | + try { |
| 139 | + const flags = getSiteMapConfig(); |
| 140 | + const ITEMS_PER_SITEMAP = flags.itemsPerPage; |
| 141 | + |
| 142 | + // Fetch counts for all entities |
| 143 | + // const [sectorsCount] = await Promise.all([ |
| 144 | + // getGraphqlEntityCount({ sectors: ENTITY_CONFIG.sectors }), |
| 145 | + // ]); |
| 146 | + |
| 147 | + const baseUrl = process.env.NEXTAUTH_URL; |
| 148 | + |
| 149 | + // Generate sitemap URLs for each entity |
| 150 | + const sitemapUrls: string[] = []; |
| 151 | + |
| 152 | + const entityCounts = await getAllEntityCounts(); |
| 153 | + |
| 154 | + // Datasets sitemaps |
| 155 | + if (entityCounts.datasets > 0) { |
| 156 | + const datasetPages = Math.ceil(entityCounts.datasets / ITEMS_PER_SITEMAP); |
| 157 | + for (let i = 1; i <= datasetPages; i++) { |
| 158 | + sitemapUrls.push(`${baseUrl}/sitemap/datasets-${i}.xml`); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + // Usecases sitemaps |
| 163 | + const usecasePages = Math.ceil(entityCounts.usecases / ITEMS_PER_SITEMAP); |
| 164 | + for (let i = 1; i <= usecasePages; i++) { |
| 165 | + sitemapUrls.push(`${baseUrl}/sitemap/usecases-${i}.xml`); |
| 166 | + } |
| 167 | + |
| 168 | + // Contributors sitemaps |
| 169 | + const contributorPages = Math.ceil( |
| 170 | + entityCounts.contributors / ITEMS_PER_SITEMAP |
| 171 | + ); |
| 172 | + for (let i = 1; i <= contributorPages; i++) { |
| 173 | + sitemapUrls.push(`${baseUrl}/sitemap/contributors-${i}.xml`); |
| 174 | + } |
| 175 | + |
| 176 | + // Sectors sitemaps |
| 177 | + if (entityCounts.sectors > 0) { |
| 178 | + const sectorPages = Math.ceil(entityCounts.sectors / ITEMS_PER_SITEMAP); |
| 179 | + for (let i = 1; i <= sectorPages; i++) { |
| 180 | + sitemapUrls.push(`${baseUrl}/sitemap/sectors-${i}.xml`); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + const sitemapIndex = generateSitemapIndex( |
| 185 | + sitemapUrls, |
| 186 | + generateStaticUrls() |
| 187 | + ); |
| 188 | + |
| 189 | + return new Response(sitemapIndex, { |
| 190 | + status: 200, |
| 191 | + headers: { |
| 192 | + 'Content-Type': 'application/xml', |
| 193 | + 'Cache-Control': `public, max-age=${flags.cacheDuration}`, |
| 194 | + }, |
| 195 | + }); |
| 196 | + |
| 197 | + // return new Response(JSON.stringify(entityCounts), { status: 200 }); |
| 198 | + } catch (error) { |
| 199 | + console.error('Error generating sitemap index:', error); |
| 200 | + |
| 201 | + const errorSitemap = `<?xml version="1.0" encoding="UTF-8"?> |
| 202 | +<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> |
| 203 | +</urlset>`; |
| 204 | + |
| 205 | + return new Response(errorSitemap, { |
| 206 | + status: 500, |
| 207 | + headers: { |
| 208 | + 'Content-Type': 'application/xml', |
| 209 | + }, |
| 210 | + }); |
| 211 | + } |
| 212 | +} |
| 213 | + |
| 214 | +export const dynamic = 'force-dynamic'; |
0 commit comments