Closed markojak closed 3 weeks ago
I haven't tested this yet, but since hreflang is defined appropriately, did you consider the fact that your regex might be incorrect/returns something different then you'd expect? Since this is a custom transformation, I would assume that something is going wrong in the transformation and that it hasn't got much to do with the package itself
Same issue here.
alternateRefs inside transform doesn't work and returns the same url from loc
.
+1 seeing the same, details below
Description:
When using next-sitemap
with next-i18next
for internationalization, the alternateRefs
in the sitemap are not generated correctly for locale-specific pages. The href
values for the alternate language versions remain the same as the current page's URL, even when using the transform
function to modify the paths.
Example:
next-i18next.config.js:
const { resolve } = require('path');
const DEFAULT_LOCALE = process.env.DEFAULT_LOCALE || 'en';
const config = {
i18n: {
defaultLocale: DEFAULT_LOCALE,
locales: [DEFAULT_LOCALE, 'es', 'fr'], // Simplified locales for example
},
// ... other i18n configurations
};
module.exports = config;
next-sitemap.config.js:
const siteUrl = process.env.NEXT_PUBLIC_SITE_URL;
const { i18n } = require('./next-i18next.config');
module.exports = {
siteUrl,
generateRobotsTxt: true,
i18n,
transform: async (config, path) => {
// Simplified transform function for example
const loc = `${config.siteUrl}${path}`;
const alternateRefs = [];
config.i18n.locales.forEach(locale => {
const href = locale === config.i18n.defaultLocale
? `${config.siteUrl}${path}`
: `${config.siteUrl}/${locale}${path.substring(3)}`;
alternateRefs.push({
href: href,
hreflang: locale,
});
});
return {
loc,
lastmod: new Date().toISOString(),
alternateRefs,
};
},
};
Expected Output:
<url>
<loc>https://myapp.firebaseapp.com/es/about</loc>
<xhtml:link rel="alternate" hreflang="en" href="https://myapp.firebaseapp.com/about"/>
<xhtml:link rel="alternate" hreflang="es" href="https://myapp.firebaseapp.com/es/about"/>
<xhtml:link rel="alternate" hreflang="fr" href="https://myapp.firebaseapp.com/fr/about"/>
</url>
Actual Output
<url>
<loc>https://myapp.firebaseapp.com/es/about</loc>
<xhtml:link rel="alternate" hreflang="en" href="https://myapp.firebaseapp.com/es/about"/>
<xhtml:link rel="alternate" hreflang="es" href="https://myapp.firebaseapp.com/es/about"/>
<xhtml:link rel="alternate" hreflang="fr" href="https://myapp.firebaseapp.com/es/about"/>
</url>
As you can see, the href values for the alternate language versions are incorrect. They all point to the Spanish version of the page (/es/about) instead of the corresponding English and French versions. I have 20+ languages, so it's a bit of problem.
Steps Taken:
Environment:
@lt7 , it was difficult to spot in their documentation, but I was able to get it working by using the following:
...
return {
loc: `${config.siteUrl}${url}`,
lastmod: new Date().toISOString(),
changefreq: "daily",
priority: 0.7,
alternateRefs: config.alternateRefs ?? [
{
href: `${config.siteUrl}${url}`,
hreflang: "en",
hrefIsAbsolute: true,
},
{
href: `${config.siteUrl}/ja-JP${url}`,
hreflang: "ja",
hrefIsAbsolute: true,
},
],
}
},
...
hrefIsAbsolute: true
did the trick.
@arno-fukuda I think you meant invisible not difficult, but thank you very much that seems to have helped !
In my case, I am using domain routing So my en-PK url looks like this: https://example.pk/about And en-AE url looks like this: https://example.com/about
I am having similar issue that the base url of loc is copied to both hreflangs i-e: https://example.pk/about
@arno-fukuda - hrefIsAbsolute did not work in this case
Any idea?
@Abdullah-J01 , In the return statement don't use config.siteUrl
, but try a custom variable.
module.exports = {
siteUrl: "https://example.com"
transform: async (config, path) => {
...
const siteUrls = {
'en': 'https://example.com',
'pk': 'https://example.pk',
};
...
return {
loc: `${siteUrl}${url}`,
lastmod: new Date().toISOString(),
changefreq: 'daily',
priority: 0.7,
alternateRefs: [
{
href: `${siteUrls['en']}${url}`,
hreflang: 'en',
hrefIsAbsolute: true,
},
{
href: `${siteUrls['pk']}${url}`,
hreflang: 'pk',
hrefIsAbsolute: true,
},
],
};
...
Does it work?
Closing this issue due to inactivity.
Describe the bug
When generating a sitemap for a Next.js application with multiple languages we incorrectly generates the
hreflang
attributes in the sitemap XML. It seems to have a limitation or bug in how it handles the alternateRefs array when generating the sitemap XML. The package is not correctly using the href values from the alternateRefs array and is instead using the original URL path for all hreflang entries.To Reproduce
(code below)
In the generated sitemap XML, the xhtml:link elements for alternate language URLs should have the correct hreflang and href attributes. The href attribute should contain the appropriate URL for each alternate language, matching the hreflang value.
For example, for a URL like https://example.com/fr/blog, the expected xhtml:link elements should be:
Actual behavior
In the generated sitemap XML, the xhtml:link elements for alternate language URLs have incorrect href attributes. The href attribute contains the same URL as the main URL, instead of the corresponding alternate language URL.
For example, for a URL like https://example.com/fr/blog, the actual xhtml:link elements in the sitemap XML are:
Additional context
Next.js version: "next": "13.4.10-canary.1", Latest sitemap version
Implementation