Skip to content

Commit 0a8b6f4

Browse files
committed
feat: generate sitemap at build time
1 parent 53a8045 commit 0a8b6f4

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,6 @@ apps/
5959

6060
instructions/
6161

62-
*.dummy*
62+
*.dummy*
63+
64+
sitemap.xml

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"private": true,
55
"scripts": {
66
"dev": "next dev",
7+
"prebuild": "node tools/scripts/generate-sitemap.js",
78
"build": "next build",
89
"start": "next start",
910
"lint": "eslint ./src",

tools/scripts/generate-sitemap.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* eslint-disable @typescript-eslint/no-var-requires */
2+
// scripts/generate-sitemap.js
3+
4+
const fs = require('fs');
5+
const path = require('path');
6+
7+
const SITEMAP_URL = 'https://middleware.jobstash.xyz/app/sitemap/ev';
8+
const PUBLIC_DIR = path.join(process.cwd(), 'public');
9+
10+
/**
11+
* Fetches sitemap from middleware API and writes to public/sitemap.xml
12+
* Creates public directory if it doesn't exist
13+
* Only override the file if the contents have changed
14+
*/
15+
async function generateSitemap() {
16+
console.log(`Fetching sitemap from ${SITEMAP_URL}...`);
17+
18+
const response = await fetch(SITEMAP_URL);
19+
if (!response.ok) {
20+
console.error(`Failed to fetch sitemap: ${response.statusText}`);
21+
process.exit(1);
22+
}
23+
24+
const newSitemap = await response.text();
25+
const sitemapPath = path.join(PUBLIC_DIR, 'sitemap.xml');
26+
27+
if (!fs.existsSync(PUBLIC_DIR)) {
28+
fs.mkdirSync(PUBLIC_DIR, { recursive: true });
29+
}
30+
31+
let currentSitemap = null;
32+
if (fs.existsSync(sitemapPath)) {
33+
currentSitemap = fs.readFileSync(sitemapPath, 'utf-8');
34+
}
35+
36+
if (currentSitemap !== newSitemap) {
37+
fs.writeFileSync(sitemapPath, newSitemap);
38+
console.log(`Sitemap updated at ${sitemapPath}`);
39+
} else {
40+
console.log('No changes in sitemap. File not updated.');
41+
}
42+
}
43+
44+
generateSitemap().catch((err) => {
45+
console.error('Error generating sitemap:', err);
46+
process.exit(1);
47+
});

0 commit comments

Comments
 (0)