Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ bestiary/
- **SEO Ready** - Structured data and semantic HTML
- **Git-based CMS** - All content changes are version controlled

## 🗺 Sitemap

The sitemap is available at:
[https://weirdanimals.life/sitemap.xml](https://weirdanimals.life/sitemap.xml)

## 🔧 Development Tips

### Adding New Animal Categories
Expand Down
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

97 changes: 97 additions & 0 deletions src/plugins/sitemapPlugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import path from "path";
import fs from "fs";

const BASE_URL = 'https://weirdanimals.life/';

const getJsonFiles = (dir) => {
try {
return fs.readdirSync(dir)
.filter(file => file.endsWith('.json'))
.map(file => file.replace('.json', ''));
} catch (error) {
console.error(`Error reading directory ${dir}:`, error);
return [];
}
};

const getSitemapContent = () => {
const categories = getJsonFiles(path.resolve('src/data/categories'));
const animals = getJsonFiles(path.resolve('src/data/animals'));
const currentDate = new Date().toISOString().split('T')[0];

let xml = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>${BASE_URL}/</loc>
<lastmod>${currentDate}</lastmod>
<changefreq>daily</changefreq>
<priority>1.0</priority>
</url>
<url>
<loc>${BASE_URL}/categories</loc>
<lastmod>${currentDate}</lastmod>
<changefreq>weekly</changefreq>
<priority>0.8</priority>
</url>`;

categories.forEach(category => {
xml += `
<url>
<loc>${BASE_URL}/category/${category}</loc>
<lastmod>${currentDate}</lastmod>
<changefreq>weekly</changefreq>
<priority>0.7</priority>
</url>`;
});

animals.forEach(animal => {
xml += `
<url>
<loc>${BASE_URL}/animal/${animal}</loc>
<lastmod>${currentDate}</lastmod>
<changefreq>monthly</changefreq>
<priority>0.6</priority>
</url>`;
});

xml += `
</urlset>`;

return xml;
};

const generateSitemap = (outDir) => {
const xml = getSitemapContent();

// Ensure outDir exists before writing
if (!fs.existsSync(outDir)) {
fs.mkdirSync(outDir, { recursive: true });
}

const sitemapPath = path.resolve(outDir, 'sitemap.xml');
fs.writeFileSync(sitemapPath, xml);
console.log(`✓ Sitemap generated at ${sitemapPath}`);
};

export const sitemapPlugin = () => {
return {
name: 'generate-sitemap',
// Hook for production build
closeBundle() {
generateSitemap('dist');
},
// Hook for development server
configureServer(server) {
server.middlewares.use((req, res, next) => {
if (req.url === '/sitemap.xml') {
const xml = getSitemapContent();
res.statusCode = 200;
res.setHeader('Content-Type', 'application/xml');
res.end(xml);
} else {
next();
}
});
}
};
};
3 changes: 2 additions & 1 deletion vite.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import path from "path";
import { sitemapPlugin } from "./src/plugins/sitemapPlugin";

// https://vite.dev/config/
export default defineConfig({
plugins: [react()],
plugins: [react(), sitemapPlugin()],
resolve: {
alias: {
"~features": path.resolve(__dirname, "./src/features"),
Expand Down