I display my website in two languages: French and English.
I built my sitemap and I realised instead of having in my sitemap.xml this:
/blog
/en-US/blog
I have :
/fr-FR/blog
/en-US/blog
I would like to remove the fr-FR string from my sitemaps because it causes a duplication of page which is bad for SEO.
How can I do that? Also how to indicate canonical pages on an XML ?
Here is my i18n config:
i18n: {
locales: ["fr-FR", "en-US"],
defaultLocale: "fr-FR"
}
And my getStaticPaths look like this:
export async function getStaticPaths({ locales }) {
const products = await fetchAPI(`/products`);
const productsData = await products;
const resProductsEn = await fetchAPI(`/products?${enTranslation}`);
const productsDataEn = await resProductsEn;
const paths = []
productsData.map((product) => paths.push(
{ params: { slug : product.slug} }
))
productsDataEn.map((product) => paths.push(
{ params: { slug : product.slug}, locale: 'en-US'}
))
return {
paths,
fallback: true
};
}
I use next-sitemap to generate my sitemap - next-sitemap.config:
module.exports = {
siteUrl: process.env.FRONT_END_URL,
generateRobotsTxt: true,
exclude: [
'/test',
'/commande',
'/forgotten-password',
'/reset-password',
'/panier',
'/mon-compte',
'/mon-compte/*',
'/fr-FR',
],
robotsTxtOptions: {
additionalSitemaps: [
`${process.env.FRONT_END_URL}/sitemap.xml`,
`${process.env.FRONT_END_URL}/server-sitemap.xml`,
`${process.env.FRONT_END_URL}/en-server-sitemap.xml`
]
}
}
A more concrete example:
<url><loc>https://www.website.com/fr-FR/questions-reponses</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-03T18:26:08.673Z</lastmod></url>
<url><loc>https://www.website.com/en-US/questions-reponses</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-12-03T18:26:08.673Z</lastmod></url>
You can use the transform property in next-sitemap.js to remove the default locale from the generated paths.
module.exports = {
// Other `next-sitemap.js` config here
transform: async (config, path) => {
return {
loc: path.replace('/fr-FR', ''), // Remove `/fr-FR` from paths - could get value from i18n config to make it dynamic
changefreq: config.changefreq,
priority: config.priority,
lastmod: config.autoLastmod ? new Date().toISOString() : undefined,
alternateRefs: config.alternateRefs ?? []
}
}
}