|
| 1 | +import { MetadataRoute } from 'next'; |
| 2 | +import { getAllContent } from './services/contentService'; |
| 3 | +import { getBaseUrl } from './services/siteService'; |
| 4 | +import { execSync } from 'child_process'; |
| 5 | +import path from 'path'; |
| 6 | + |
| 7 | +// Required to evaluate at build time |
| 8 | +export const dynamic = 'force-static'; |
| 9 | + |
| 10 | +// Get the last git commit date for a specific file |
| 11 | +const getGitLastModified = (filePath: string): Date => { |
| 12 | + try { |
| 13 | + // Convert absolute path to relative path from repo root |
| 14 | + const relativePath = path.relative(process.cwd(), filePath).replace(/\\/g, '/'); |
| 15 | + const output = execSync( |
| 16 | + `git log -1 --format=%cI -- "${relativePath}"`, |
| 17 | + { encoding: 'utf-8', cwd: process.cwd() } |
| 18 | + ).trim(); |
| 19 | + |
| 20 | + return output ? new Date(output) : new Date(); |
| 21 | + } catch (error) { |
| 22 | + console.warn(`Could not get git date for ${filePath}`); |
| 23 | + return new Date(); |
| 24 | + } |
| 25 | +}; |
| 26 | + |
| 27 | +export default function sitemap(): MetadataRoute.Sitemap { |
| 28 | + const baseUrl = getBaseUrl(); |
| 29 | + const entities = getAllContent(); |
| 30 | + |
| 31 | + return entities |
| 32 | + .filter(entity => !entity.isExternal) // Exclude external blog posts |
| 33 | + .map(entity => { |
| 34 | + // Determine priority and change frequency based on page type and depth |
| 35 | + let priority = 0.00; |
| 36 | + let changeFrequency: 'yearly' | 'monthly' | 'weekly' = 'yearly'; |
| 37 | + |
| 38 | + if (entity.type === 'home') { |
| 39 | + priority = 1.0; |
| 40 | + changeFrequency = 'yearly'; |
| 41 | + } else if (entity.type === 'docs') { |
| 42 | + priority = 0.85; |
| 43 | + changeFrequency = 'weekly'; |
| 44 | + } else if (entity.type === 'packages') { |
| 45 | + priority = 0.70; |
| 46 | + changeFrequency = 'weekly'; |
| 47 | + } else if (entity.type === 'reference') { |
| 48 | + priority = 0.55; |
| 49 | + changeFrequency = 'monthly'; |
| 50 | + } else if (entity.type === 'blog') { |
| 51 | + priority = 0.40; |
| 52 | + changeFrequency = 'monthly'; |
| 53 | + } else if (entity.type === 'landing') { |
| 54 | + priority = 0.25; |
| 55 | + changeFrequency = 'monthly'; |
| 56 | + } |
| 57 | + |
| 58 | + // Get last modified date from git |
| 59 | + const lastModified = entity.filePath |
| 60 | + ? getGitLastModified(entity.filePath) |
| 61 | + : new Date(); |
| 62 | + |
| 63 | + return { |
| 64 | + url: `${baseUrl}${entity.url}`, |
| 65 | + lastModified, |
| 66 | + changeFrequency, |
| 67 | + priority, |
| 68 | + }; |
| 69 | + }); |
| 70 | +} |
0 commit comments