forked from OpenCerts/opencerts-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-sitemap.js
70 lines (58 loc) · 2.5 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
/*
Generates a sitemap based on the entries in exportPathMap in next.config.js file
Author: Joran Quinten
Don't forget to add the domain name as process variable PUBLIC_DOMAIN!
Trigger the file with a separate script in your package.json and add it to your build command in Netlify
(for instance, set Build command to: "npm run production && npm run postexport")
The postexport command runs the script post-export.js in my repo, which calls the generator:
// file contents "post-export.js":
require('dotenv').config();
const { generateSitemap } = require('./generate-sitemap');
generateSitemap(process.env.PUBLIC_DOMAIN, './out/static/');
*/
const fs = require("fs");
// Read from the static map that's provided by next
const { exportPathMap } = require("./next.config");
// Format to the right date
const formatDate = (date) => `${date.toISOString().split(".")[0]}+0:00`;
// Priority is determined by path depth. Feel free to modify this if needed:
const getPriority = (url) => ((100 - (url.split("/").length - 2) * 10) / 100).toFixed(2);
// Just pick current date as last modified
const lastModified = formatDate(new Date());
// 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>`;
// Determine and return the nodes for every page
const xmlUrlNode = (domain, pageUrl, lastmod) => {
const url = `${pageUrl}${pageUrl === "/" ? "" : "/"}`;
const loc = `${domain}${url}`;
const priority = getPriority(url);
return `<url>
<loc>${loc}</loc>
<lastmod>${lastmod}</lastmod>
<priority>${priority}</priority>
</url>`;
};
exports.generateSitemap = async (domain, targetFolder) => {
if (!domain) {
throw new Error("No domain provided!");
}
if (!targetFolder) {
throw new Error("No targetFolder provided!");
}
const fileName = "sitemap.xml";
const writeLocation = `${targetFolder.endsWith("/") ? targetFolder : `${targetFolder}/`}${fileName}`;
const entries = await exportPathMap();
const pages = Object.entries(entries).map((item) => item[0]);
const sitemap = `${xmlUrlWrapper(
pages.map((page) => xmlUrlNode(domain, page, lastModified)).join(`
`)
)}`;
fs.writeFile(`${writeLocation}`, sitemap, (err) => {
if (err) throw err;
});
};