-
Notifications
You must be signed in to change notification settings - Fork 34
/
sitemap.ts
39 lines (33 loc) · 1.16 KB
/
sitemap.ts
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
const path = require("path");
const fs = require("fs");
const { SitemapStream, streamToPromise } = require("sitemap");
const { paths } = require("./src/config/routes-config");
// https://github.com/ekalinin/sitemap.js/blob/master/api.md#sitemap-item-options
interface SitemapRecord {
url: string;
changefreq?: string;
priority?: number;
}
const mainPages: SitemapRecord[] = [];
const pathArray: string[] = Object.values(paths);
Object.values(pathArray).reduce((accumulator, currentPath) => {
if (!currentPath.includes("*") && !currentPath.includes(":slug")) {
accumulator.push({
url: currentPath,
changefreq: "monthly",
priority: 0.6,
});
}
return accumulator;
}, mainPages);
const cmsDetailPages: SitemapRecord[] = [];
const allPages = [...mainPages, ...cmsDetailPages];
const sitemap = new SitemapStream({ hostname: "https://www.tradetrust.io" });
const sitemapPath = path.join(__dirname, "public", "static");
fs.mkdirSync(sitemapPath, { recursive: true });
const writeStream = fs.createWriteStream(path.join(sitemapPath, "sitemap.xml"));
sitemap.pipe(writeStream);
allPages.forEach((page) => {
sitemap.write(page);
});
sitemap.end();