Skip to content

Commit 0fbdfc6

Browse files
committed
Update sitemap generation to have index with additional /
1 parent fb1e74d commit 0fbdfc6

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

scripts/generateSitemap.js

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@ const { glob } = require("glob");
88
dotenv.config({ path: path.resolve(__dirname, "../.env") });
99
dotenv.config({ path: path.resolve(__dirname, "../.env.local"), override: true });
1010

11+
const makeUrlContent = ({ url, lastMod, changeFreq = "monthly", priority = "0.5" }) => {
12+
let content = "";
13+
14+
content += `\t<url>\n`;
15+
content += `\t\t<loc>${url}</loc>\n`;
16+
if (lastMod) content += `\t\t<lastmod>${lastMod}</lastmod>\n`;
17+
content += `\t\t<changefreq>${changeFreq}</changefreq>\n`;
18+
content += `\t\t<priority>${priority}</priority>\n`;
19+
content += `\t</url>\n`;
20+
21+
return content;
22+
};
23+
1124
const runScript = async () => {
1225
const outDir = path.resolve(__dirname, "../out");
1326
const pages = await glob("**/*.html", {
@@ -29,13 +42,21 @@ const runScript = async () => {
2942
const changeFreqElement = fileDocument.querySelector("meta[name='page_change_frequency']");
3043
const priorityElement = fileDocument.querySelector("meta[name='page_priority']");
3144

45+
urlContent += makeUrlContent({
46+
url: canonicalUrl,
47+
lastMod: lastModElement?.getAttribute("content"),
48+
changeFreq: changeFreqElement?.getAttribute("content"),
49+
priority: priorityElement?.getAttribute("content")
50+
});
3251

33-
urlContent += `\t<url>\n`;
34-
urlContent += `\t\t<loc>${canonicalUrl}</loc>\n`;
35-
if (lastModElement?.getAttribute("content")) urlContent += `\t\t<lastmod>${lastModElement.getAttribute("content")}</lastmod>\n`;
36-
urlContent += `\t\t<changefreq>${changeFreqElement?.getAttribute("content") ?? "monthly"}</changefreq>\n`;
37-
urlContent += `\t\t<priority>${priorityElement?.getAttribute("content") ?? "0.5"}</priority>\n`;
38-
urlContent += `\t</url>\n`;
52+
if (process.env.NEXT_PUBLIC_FRONTEND_URL === canonicalUrl) {
53+
urlContent += makeUrlContent({
54+
url: canonicalUrl + "/",
55+
lastMod: lastModElement?.getAttribute("content"),
56+
changeFreq: changeFreqElement?.getAttribute("content"),
57+
priority: priorityElement?.getAttribute("content")
58+
});
59+
}
3960

4061
return urlContent;
4162
}));

0 commit comments

Comments
 (0)