-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-sitemap.js
76 lines (62 loc) · 2.06 KB
/
generate-sitemap.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Description: Generates a sitemap.xml file for the website.
const fs = require("fs");
const { routes } = require("./src/lib/Routes.js");
const BASE_URL = "https://www.elhassu.com";
function formatW3C(date) {
if (!(date instanceof Date)) {
try {
date = new Date(date);
if (isNaN(date.getTime())) {
throw new Error();
}
} catch (e) {
throw new Error("'date' is not a valid Date object or string.");
}
}
let year = date.getFullYear();
let month = date.getMonth() + 1;
if (month < 10) month = "0" + month;
let day = date.getDate();
if (day < 10) day = "0" + day;
let hours = date.getHours();
if (hours < 10) hours = "0" + hours;
let minutes = date.getMinutes();
if (minutes < 10) minutes = "0" + minutes;
let seconds = date.getSeconds();
if (seconds < 10) seconds = "0" + seconds;
let offset = -date.getTimezoneOffset();
let offsetHours = Math.abs(Math.floor(offset / 60));
let offsetMinutes = Math.abs(offset) - offsetHours * 60;
if (offsetHours < 10) offsetHours = "0" + offsetHours;
if (offsetMinutes < 10) offsetMinutes = "0" + offsetMinutes;
let offsetSign = "+";
if (offset < 0) offsetSign = "-";
return `${year}-${month}-${day}T${hours}:${minutes}:${seconds}${offsetSign}${offsetHours}:${offsetMinutes}`;
}
(async () => {
console.time("Sitemap generated");
const remappedRoutes = [];
routes.forEach(({ path, updatedAt, subPaths }) => {
remappedRoutes.push({ path, updatedAt });
if (subPaths?.length) {
subPaths.forEach(({ path: subPath, updatedAt }) => {
remappedRoutes.push({ path: `${path}${subPath}`, updatedAt });
});
}
});
const sitemap = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${remappedRoutes
.map(({ path, updatedAt }) => {
return `<url>
<loc>${BASE_URL}${path}</loc>
<lastmod>${formatW3C(updatedAt ? new Date(updatedAt) : new Date())}</lastmod>
<changefreq>monthly</changefreq>
</url>`;
})
.join("\n ")}
</urlset>
`;
fs.writeFileSync("./public/sitemap.xml", sitemap);
console.timeEnd("Sitemap generated");
})();