This repository was archived by the owner on Feb 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathserializeSitemap.js
109 lines (100 loc) · 3.03 KB
/
serializeSitemap.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
const { groupBy } = require("lodash");
const { defaultLocale, supportedLanguages } = require("./i18n");
const getPathInfo = ({ path, context }) => {
const [_, maybeLocale, ...rest] = path.split("/");
const pathWithoutLocale = rest.join("/").replace(/\/$/, "");
// All of our translated pages begin with the locale code, like `/es/blah`.
// If the second section (because the leading slash means the 0th will be '')
// is in our list of supported languages, bam that's the locale. If not,
// that's the default locale, and it's a top level route. We only want to
// Keep top level routes for the default locale, so we need to know that.
if (supportedLanguages.includes(maybeLocale)) {
return {
locale: maybeLocale,
path: pathWithoutLocale,
originalPath: path,
isTopLevel: false,
context,
};
}
return {
locale: defaultLocale,
path: `${maybeLocale}/${pathWithoutLocale}`.replace(/\/$/, ""),
originalPath: path,
isTopLevel: true,
context,
};
};
const checkIsBlogPostRegex = /blog\/.+/;
const serializeLocale = (locale) => {
return ({ site, allSitePage }) => {
const { edges } = allSitePage;
const pages = edges
.map(({ node }) => getPathInfo(node))
// Technically "/api" doesn't have any content, it's just a filler page for
// other content to live on. Strip it from the sitemap.
.filter(({ originalPath }) => originalPath !== "/api/");
const byPath = groupBy(pages, "path");
const pagesWithAlternates = Object.values(byPath).reduce(
(accum, pageVersions) => {
const mainPage = pageVersions.find(
(pageVersion) => pageVersion.locale === locale,
);
const alternates = pageVersions.filter(
(pageVersion) => pageVersion.locale !== locale,
);
// Don't include blog posts on international sitemaps
if (
locale !== defaultLocale &&
checkIsBlogPostRegex.test(mainPage.originalPath)
) {
return accum;
}
accum.push({
main: mainPage,
alternates,
});
return accum;
},
[],
);
return pagesWithAlternates.map(({ main, alternates }) => {
return {
// Strip no-js url segments, which fixes 2 bugs:
// * API Reference sections don't have full pages, so they don't show up
// in the sitemap
// * We don't want to direct people to the no-js/ versions of the docs,
// so highlighting them in the sitemap isn't what we want.
url:
site.siteMetadata.siteUrl + main.originalPath.replace("no-js/", ""),
lastmod: main.context.lastModified,
links: alternates.map((a) => ({
lang: a.locale,
url: site.siteMetadata.siteUrl + a.originalPath,
})),
};
});
};
};
const query = `
{
site {
siteMetadata {
siteUrl
}
}
allSitePage {
edges {
node {
path
context {
lastModified
}
}
}
}
}`;
module.exports = {
serializeLocale,
query,
};