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

next-sitemap duplicated language in alternate ref path (href) #884

Open
boyanhtrai opened this issue Oct 24, 2024 · 1 comment
Open

next-sitemap duplicated language in alternate ref path (href) #884

boyanhtrai opened this issue Oct 24, 2024 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@boyanhtrai
Copy link

boyanhtrai commented Oct 24, 2024

Describe the bug
Refer: https://stackoverflow.com/questions/74108508/next-sitemap-duplicated-language-in-alternate-ref-path-href
We have duplicated for '/en' when I config as below
module.exports = {
siteUrl: 'https://www.ora-candle.com'
generateRobotsTxt: true,
i18n: {
locales: ['en', 'vi'],
defaultLocale: 'vi',
},
changefreq: 'monthly',
generateIndexSitemap: false,
alternateRefs: [
{
href: ',
hreflang: 'en',
},
],
};

Result behavior

<url>
        <loc>https://www.ora-candle.com</loc>
        <lastmod>2024-10-24T18:32:12.746Z</lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.7</priority>
        <xhtml:link rel="alternate" hreflang="en" href="https://www.ora-candle.com/en"/>
    </url>
<url>
        <loc>https://www.ora-candle.com/en</loc>
         <lastmod>2024-10-24T18:32:12.748Z</lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.7</priority>
        **<xhtml:link rel="alternate" hreflang="en" href="https://www.ora-candle.com/en/en"/>** ==> error
    </url>

Expected behavior

<url>
        <loc>https://www.ora-candle.com</loc>
        <lastmod>2024-10-24T18:32:12.746Z</lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.7</priority>
        <xhtml:link rel="alternate" hreflang="en" href="https://www.ora-candle.com/en"/>
    </url>
<url>
        <loc>https://www.ora-candle.com/en</loc>
         <lastmod>2024-10-24T18:32:12.748Z</lastmod>
        <changefreq>monthly</changefreq>
        <priority>0.7</priority>
        <xhtml:link rel="alternate" hreflang="en" href="https://www.ora-candle.com/en"/>
    </url>
@boyanhtrai boyanhtrai added the bug Something isn't working label Oct 24, 2024
@chr4ss12
Copy link

chr4ss12 commented Oct 29, 2024

yeah lookslike bug, I had hard time generating sitemap with correctlocales,

had to resort to this:

const nextConfig = require('./next-i18next.config.js');

const {locales} = nextConfig.i18n;

const pages = [
  'page1',
  'page2'

];

const localizedPages = [
  ...locales.flatMap(locale =>
    pages.map(page => ({
      locale,
      page,
      href: `https://www.random.com/${locale === 'en' ? page : `${locale}/${page}`}`,
    })),
  ),

  ...locales
    .filter(l => l !== 'en')
    .map(locale => ({
      locale,
      page: '',
      href: `https://www.random.com/${locale}`,
    })),
];

const transform = async (config, path) => ({
  loc: path, // => this will be exported as http(s)://<config.siteUrl>/<path>
  changefreq: config.changefreq,
  priority: config.priority,
  lastmod: config.autoLastmod ? new Date().toISOString() : undefined,
  alternateRefs: config.alternateRefs ?? [],
});

module.exports = {
  siteUrl: 'https://www.random.com',
  changefreq: 'daily',
  priority: 0.7,
  sitemapSize: 5000,
  generateRobotsTxt: true,

  // exclude: ['/server-sitemap.xml'],

  alternateRefs: [
    ...nextConfig.i18n.locales
      .map(locale => ({
        href: `https://www.random.com/${locale}`,
        hreflang: locale,
      }))
      .filter(h => h.hreflang !== 'en'),

    {
      href: 'https://www.random.com',
      hreflang: 'en',
    },
  ],

  // Default transformation function
  transform,

  additionalPaths: async config => [
    ...(await Promise.all(
      localizedPages.map(({locale, href, page}) =>
        transform(
          {
            ...config,
            alternateRefs: localizedPages
              .filter(x => x.page === page)
              .map(localizedPage => ({
                href: localizedPage.href,
                hrefIsAbsolute: true,
                hreflang: localizedPage.locale,
              })),
          },
          `${href}`,
        ),
      ),
    )),
  ],
  robotsTxtOptions: {
    policies: [
      {
        userAgent: '*',
        allow: '/',
      },
    ],
    additionalSitemaps: ['https://www.random.com/blog/sitemap.xml'],
  },
};

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