forked from kamranahmedse/developer-roadmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsitemap.js
144 lines (118 loc) · 3.98 KB
/
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// This is a development script executed in the build step of pages
const fs = require('fs');
const path = require('path');
const glob = require('glob');
const guides = require('../content/guides.json');
const roadmaps = require('../content/roadmaps.json');
const DOMAIN = 'https://roadmap.sh';
const PAGES_DIR = path.join(__dirname, '../pages');
const SITEMAP_PATH = path.join(__dirname, '../public/sitemap.xml');
const PAGES_PATH = path.join(__dirname, '../pages');
const ROADMAPS_PATH = path.join(__dirname, '../content/roadmaps');
const GUIDES_PATH = path.join(__dirname, '../content/guides');
// Set the header
const xmlHeader = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">`;
// Wrap all pages in <urlset> tags
const xmlUrlWrapper = (nodes) => `${xmlHeader}
${nodes}
</urlset>`;
function getSlugPriority(pageSlug) {
if (pageSlug === '/') {
return '1.0';
}
const slugPriorities = [
['/roadmaps', '/guides', '/watch', '/podcasts'], // 1.0
['/signup'], // 0.9
['/about'], // 0.8
];
const foundIndex = slugPriorities.findIndex((routes) =>
routes.some((route) => pageSlug.startsWith(route))
);
if (foundIndex !== -1) {
return parseFloat((10 - foundIndex) / 10).toFixed(1);
}
return 0.5;
}
function getPageRoutes() {
const files = glob.sync(`${PAGES_PATH}/**/*.tsx`, {
ignore: [
'**/_*.tsx', // private non-page files e.g. _document.js
'**/[[]*[]].tsx', // Ignore dynamic pages i.e. `page/[something].js` files
'**/[[]*[]]/*.tsx', // Ignore files inside dynamic pages i.e. `[something]/abc.js`
'**/components/*.tsx', // Ignore the component files
],
});
const pageRoutes = {};
files.forEach((file) => {
const pageName = file.replace(PAGES_PATH, '').replace('.tsx', '');
const pagePath = pageName.replace('/index', '') || '/';
pageRoutes[pagePath] = { page: `${pageName}` };
});
return pageRoutes;
}
function generateNode(nodeProps) {
const {
slug,
basePath,
fileName,
priority = null,
date = null,
frequency = 'monthly',
} = nodeProps;
if (slug.includes('upcoming') || slug.includes('pdfs')) {
return null;
}
const pagePath = path.join(basePath, fileName);
let pageStats = {};
try {
pageStats = fs.lstatSync(pagePath);
} catch (e) {
console.log(`File not found: ${pagePath}`);
pageStats = { mtime: new Date() };
}
return `<url>
<loc>${DOMAIN}${slug}</loc>
<changefreq>${frequency}</changefreq>
<lastmod>${date || pageStats.mtime.toISOString()}</lastmod>
<priority>${priority || getSlugPriority(slug)}</priority>
</url>`;
}
function generateSiteMap() {
const pageRoutes = getPageRoutes();
const pageSlugs = Object.keys(pageRoutes).filter(
(route) => !['/privacy', '/terms'].includes(route)
);
const pagesChunk = pageSlugs.map((pageSlug) => {
return generateNode({
basePath: PAGES_DIR,
fileName: `${pageRoutes[pageSlug].page}.tsx`,
slug: pageSlug,
});
});
const guidesChunk = guides.map((guide) => {
return generateNode({
basePath: GUIDES_PATH,
fileName: `${guide.id}.md`,
slug: `/guides/${guide.id}`,
date: guide.updatedAt,
priority: '1.0',
});
});
const roadmapsChunk = roadmaps.map((roadmap, roadmapCounter) => {
return generateNode({
basePath: ROADMAPS_PATH,
fileName: roadmap.metaPath.replace('/roadmaps', ''),
slug: `/${roadmap.id}`,
date: roadmap.updatedAt,
priority: '1.0',
});
});
const nodes = [...roadmapsChunk, ...guidesChunk, ...pagesChunk];
const sitemap = `${xmlUrlWrapper(nodes.join('\n'))}`;
fs.writeFileSync(SITEMAP_PATH, sitemap);
console.log(
`sitemap.xml with ${nodes.length} entries was written to ${SITEMAP_PATH}`
);
}
generateSiteMap();