-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-sitemap.js
92 lines (79 loc) · 3.35 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const fs = require('fs');
const path = require('path');
const https = require('https');
const { parseString } = require('xml2js'); // Using xml2js for XML parsing
const site = 'https://rnmobility.com';
const buildFolderPath = path.join(__dirname, 'build');
const sourceFolderPath = path.join(__dirname, 'build');
const sitemapPath = path.join(sourceFolderPath, 'sitemap.xml');
// Function to generate sitemap XML content
function generateSitemap(files, lastmodDate) {
let xml = '<?xml version="1.0" encoding="UTF-8"?>\n';
xml += '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n';
const currentDate = new Date().toISOString();
files.forEach(file => {
// Convert file path to URL and remove file extensions
const url = file.replace(buildFolderPath, '').replace(/\\/g, '/').replace(/index\.html?$/, '').replace(/\.html?$/, '');
xml += `<url><loc>${site}${url}</loc>`;
xml += `<lastmod>${lastmodDate.toISOString()}</lastmod>`; // Use lastmodDate if it exists
xml += url === '/' ? '<priority>1.00</priority>' : '<priority>0.80</priority>';
xml += '</url>\n';
});
xml += '</urlset>';
return xml;
}
// Recursive function to find HTML files in subdirectories
function getHTMLFiles(dir, fileList) {
const files = fs.readdirSync(dir);
fileList = fileList || [];
files.forEach(file => {
const filePath = path.join(dir, file);
if (fs.statSync(filePath).isDirectory()) {
getHTMLFiles(filePath, fileList);
} else if (path.extname(file) === '.html' || path.extname(file) === '.htm') {
fileList.push(filePath);
}
});
return fileList;
}
// Fetch sitemap.xml from the provided URL
https.get(`${site}/sitemap.xml`, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
// Check if sitemap.xml file exists
parseString(data, (err, result) => {
if (err) {
generateAndWriteSitemap(new Date());
return;
}
// Get the lastmod date of the first URL
const lastmod = new Date(result.urlset.url[0].lastmod[0]);
const today = new Date();
// Calculate the difference in days
const differenceInTime = today.getTime() - lastmod.getTime();
const differenceInDays = Math.round(differenceInTime / (1000 * 3600 * 24));
// Generate the sitemap using lastmod if the difference is more than 30 days, else use currentDate
const sitemapDate = differenceInDays > 30 ? today : lastmod;
generateAndWriteSitemap(sitemapDate);
});
});
}).on('error', (err) => {
console.error('Error fetching sitemap:', err);
// If there's an error fetching the sitemap, generate the sitemap with the current date
generateAndWriteSitemap(new Date());
});
// Function to generate and write sitemap
function generateAndWriteSitemap(lastmodDate) {
// Find all HTML files in build folder and its subdirectories
const htmlFiles = getHTMLFiles(buildFolderPath);
// Generate sitemap XML content
const sitemapXML = generateSitemap(htmlFiles, lastmodDate);
// Write sitemap XML to file
fs.writeFile(sitemapPath, sitemapXML, (err) => {
if (err) throw err;
console.log('Sitemap.xml generated successfully!');
});
}