Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not using href values from alternaateRefs array and instead original URL path for all hreflang entries #796

Open
markojak opened this issue Apr 9, 2024 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@markojak
Copy link

markojak commented Apr 9, 2024

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

  • Define a custom transform function that generates alternateRefs array for each URL using a bunch of constants specifying the correct hreflang and href values for each alternate language
  • generate sitemap
  • Inspect sitemap

(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:

<xhtml:link rel="alternate" hreflang="en" href="https://example.com/en/blog"/>
<xhtml:link rel="alternate" hreflang="es" href="https://example.com/es/blog"/>
<xhtml:link rel="alternate" hreflang="de" href="https://example/de/blog"/>

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:

<xhtml:link rel="alternate" hreflang="en" href="https://example.com/fr/blog"/>
<xhtml:link rel="alternate" hreflang="es" href="https://example.com/fr/blog"/>
<xhtml:link rel="alternate" hreflang="de" href="https://example.com/fr/blog"/>
<xhtml:link rel="alternate" hreflang="fr" href="https://example.com/fr/blog"/>

Additional context

Next.js version: "next": "13.4.10-canary.1",
Latest sitemap version

Implementation

require('ts-node').register();
const { SUPPORTED_LOCALES } = require('./src/constants.ts');

const generateAlternateRefs = (path, locales) => {
    console.log('Path:', path);
    console.log('Locales:', locales);

    const alternateRefs = locales.map((locale) => {
        const localePath = path.replace(new RegExp(`^/(${locales.join('|')})`), `/${locale}`);
        return {
            hreflang: locale,
            href: `https://example.com${localePath}`,
        };
    });
    return alternateRefs;
};

/** @type {import('next-sitemap').IConfig} */
module.exports = {
    siteUrl: 'https://example.com',
    changefreq: 'daily',
    priority: 0.7,
    sitemapSize: 5000,
    generateRobotsTxt: true,
    transform: async (config, path) => {
        console.log('Transform Path:', path);

        const alternateRefs = generateAlternateRefs(path, Object.keys(SUPPORTED_LOCALES));
        console.log('Generated Alternate Refs:', alternateRefs);

        return {
            loc: `${config.siteUrl}${path}`,
            changefreq: config.changefreq,
            priority: config.priority,
            lastmod: config.autoLastmod ? new Date().toISOString() : undefined,
            alternateRefs,
        };
    },
    robotsTxtOptions: {
        policies: [
            {
                userAgent: '*',
                allow: '/',
            },
        ],
    },
};
@markojak markojak added the bug Something isn't working label Apr 9, 2024
@markojak markojak changed the title Bug: Not using href values from alternaateRefs array and instead original URL path for all hreflang entries Not using href values from alternaateRefs array and instead original URL path for all hreflang entries Apr 9, 2024
@luukkynda
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants