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