Skip to content

Commit b882cee

Browse files
authored
fix: update sitemap generation with improved formatting and hreflang structure (#452)
1 parent da2dfd3 commit b882cee

File tree

1 file changed

+16
-35
lines changed

1 file changed

+16
-35
lines changed

src/pages/sitemap.xml.tsx

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -50,48 +50,29 @@ const generatePaths = () => {
5050
});
5151
});
5252

53-
const localizedPaths: string[] = [];
54-
const supportedLocales = ['', 'ru', 'es', 'zh'];
55-
56-
paths.forEach((item) => {
57-
supportedLocales.forEach((locale) => {
58-
if (!locale) {
59-
localizedPaths.push(item.path);
60-
} else if (locale && !item.notLocalized) {
61-
localizedPaths.push(`/${locale}${item.path}`);
62-
}
63-
});
64-
});
65-
66-
return localizedPaths;
53+
return paths;
6754
};
6855

6956
export const getServerSideProps: GetServerSideProps = async (ctx) => {
70-
const paths = generatePaths();
57+
const basePaths = generatePaths();
58+
const supportedLocales = ['', 'ru', 'es', 'zh'];
59+
60+
const fields = basePaths.map((pathItem) => {
61+
const {path, notLocalized} = pathItem;
62+
63+
// Generate alternate refs for this path
64+
const alternateRefs = supportedLocales
65+
.filter((locale) => !locale || !notLocalized) // Include default locale always, other locales only if path is localizable
66+
.map((locale) => ({
67+
href: locale ? `${BASE_URL}/${locale}${path}` : `${BASE_URL}${path}`,
68+
hreflang: locale || 'en',
69+
}));
7170

72-
const fields = paths.map((path) => {
7371
return {
74-
loc: `${BASE_URL}${path}`,
72+
loc: `${BASE_URL}${path}`, // Always use the canonical (English) URL as the main loc
7573
changefreq: 'daily' as const,
7674
priority: 0.7,
77-
alternateRefs: [
78-
{
79-
href: `${BASE_URL}${path}`,
80-
hreflang: 'en',
81-
},
82-
{
83-
href: `${BASE_URL}/ru${path}`,
84-
hreflang: 'ru',
85-
},
86-
{
87-
href: `${BASE_URL}/zh${path}`,
88-
hreflang: 'zh',
89-
},
90-
{
91-
href: `${BASE_URL}/es${path}`,
92-
hreflang: 'es',
93-
},
94-
],
75+
alternateRefs,
9576
};
9677
});
9778

0 commit comments

Comments
 (0)