Skip to content

Commit d84c952

Browse files
Merge pull request #1 from prerender/add-sitemap
Add sitemap
2 parents fa726fb + a67dd81 commit d84c952

File tree

4 files changed

+114
-1
lines changed

4 files changed

+114
-1
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ bestiary/
113113
- **SEO Ready** - Structured data and semantic HTML
114114
- **Git-based CMS** - All content changes are version controlled
115115

116+
## 🗺 Sitemap
117+
118+
The sitemap is available at:
119+
[https://weirdanimals.life/sitemap.xml](https://weirdanimals.life/sitemap.xml)
120+
116121
## 🔧 Development Tips
117122

118123
### Adding New Animal Categories

package-lock.json

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/plugins/sitemapPlugin.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
};

vite.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { defineConfig } from "vite";
22
import react from "@vitejs/plugin-react";
33
import path from "path";
4+
import { sitemapPlugin } from "./src/plugins/sitemapPlugin";
45

56
// https://vite.dev/config/
67
export default defineConfig({
7-
plugins: [react()],
8+
plugins: [react(), sitemapPlugin()],
89
resolve: {
910
alias: {
1011
"~features": path.resolve(__dirname, "./src/features"),

0 commit comments

Comments
 (0)