|
| 1 | +import path from "path"; |
| 2 | +import fs from "fs"; |
| 3 | + |
| 4 | +const BASE_URL = 'https://weirdanimals.life/'; |
| 5 | + |
| 6 | +const getJsonFiles = (dir) => { |
| 7 | + try { |
| 8 | + return fs.readdirSync(dir) |
| 9 | + .filter(file => file.endsWith('.json')) |
| 10 | + .map(file => file.replace('.json', '')); |
| 11 | + } catch (error) { |
| 12 | + console.error(`Error reading directory ${dir}:`, error); |
| 13 | + return []; |
| 14 | + } |
| 15 | +}; |
| 16 | + |
| 17 | +const getSitemapContent = () => { |
| 18 | + const categories = getJsonFiles(path.resolve('src/data/categories')); |
| 19 | + const animals = getJsonFiles(path.resolve('src/data/animals')); |
| 20 | + const currentDate = new Date().toISOString().split('T')[0]; |
| 21 | + |
| 22 | + let xml = `<?xml version="1.0" encoding="UTF-8"?> |
| 23 | +<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> |
| 24 | + <url> |
| 25 | + <loc>${BASE_URL}/</loc> |
| 26 | + <lastmod>${currentDate}</lastmod> |
| 27 | + <changefreq>daily</changefreq> |
| 28 | + <priority>1.0</priority> |
| 29 | + </url> |
| 30 | + <url> |
| 31 | + <loc>${BASE_URL}/categories</loc> |
| 32 | + <lastmod>${currentDate}</lastmod> |
| 33 | + <changefreq>weekly</changefreq> |
| 34 | + <priority>0.8</priority> |
| 35 | + </url>`; |
| 36 | + |
| 37 | + categories.forEach(category => { |
| 38 | + xml += ` |
| 39 | + <url> |
| 40 | + <loc>${BASE_URL}/category/${category}</loc> |
| 41 | + <lastmod>${currentDate}</lastmod> |
| 42 | + <changefreq>weekly</changefreq> |
| 43 | + <priority>0.7</priority> |
| 44 | + </url>`; |
| 45 | + }); |
| 46 | + |
| 47 | + animals.forEach(animal => { |
| 48 | + xml += ` |
| 49 | + <url> |
| 50 | + <loc>${BASE_URL}/animal/${animal}</loc> |
| 51 | + <lastmod>${currentDate}</lastmod> |
| 52 | + <changefreq>monthly</changefreq> |
| 53 | + <priority>0.6</priority> |
| 54 | + </url>`; |
| 55 | + }); |
| 56 | + |
| 57 | + xml += ` |
| 58 | +</urlset>`; |
| 59 | + |
| 60 | + return xml; |
| 61 | +}; |
| 62 | + |
| 63 | +const generateSitemap = (outDir) => { |
| 64 | + const xml = getSitemapContent(); |
| 65 | + |
| 66 | + // Ensure outDir exists before writing |
| 67 | + if (!fs.existsSync(outDir)) { |
| 68 | + fs.mkdirSync(outDir, { recursive: true }); |
| 69 | + } |
| 70 | + |
| 71 | + const sitemapPath = path.resolve(outDir, 'sitemap.xml'); |
| 72 | + fs.writeFileSync(sitemapPath, xml); |
| 73 | + console.log(`✓ Sitemap generated at ${sitemapPath}`); |
| 74 | +}; |
| 75 | + |
| 76 | +export const sitemapPlugin = () => { |
| 77 | + return { |
| 78 | + name: 'generate-sitemap', |
| 79 | + // Hook for production build |
| 80 | + closeBundle() { |
| 81 | + generateSitemap('dist'); |
| 82 | + }, |
| 83 | + // Hook for development server |
| 84 | + configureServer(server) { |
| 85 | + server.middlewares.use((req, res, next) => { |
| 86 | + if (req.url === '/sitemap.xml') { |
| 87 | + const xml = getSitemapContent(); |
| 88 | + res.statusCode = 200; |
| 89 | + res.setHeader('Content-Type', 'application/xml'); |
| 90 | + res.end(xml); |
| 91 | + } else { |
| 92 | + next(); |
| 93 | + } |
| 94 | + }); |
| 95 | + } |
| 96 | + }; |
| 97 | +}; |
0 commit comments