question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dont take into account default alternateref and self locale

See original GitHub issue

next-sitemap dont take into account the default alternate ref and duplicate self locale, included defaultLocale. Here is my configuration:

my next config:

...
i18n: {
    locales: [
      "en",
      "es",
    ],
    defaultLocale: "es",
  },
...

My alternateRef:

alternateRefs = [
  {
    href: process.env.NEXT_PUBLIC_WEB_URL,
    hreflang: "es",
  },
  {
    href: process.env.NEXT_PUBLIC_WEB_URL + "/en",
    hreflang: "en",
  },
];

The generated xml:

...
<url><loc>https://web-test.com/faq</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-06-04T13:17:26.429Z</lastmod><xhtml:link rel="alternate" hreflang="es" href="https://web-test.com/faq"/><xhtml:link rel="alternate" hreflang="en" href="https://web-test.com/en/faq"/></url>
<url><loc>https://web-test.com/es/faq</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-06-04T13:17:26.429Z</lastmod><xhtml:link rel="alternate" hreflang="es" href="https://web-test.com/es/faq"/><xhtml:link rel="alternate" hreflang="en" href="https://web-test.com/en/es/faq"/></url>
<url><loc>https://web-test.com/en/faq</loc><changefreq>daily</changefreq><priority>0.7</priority><lastmod>2021-06-04T13:17:26.429Z</lastmod><xhtml:link rel="alternate" hreflang="es" href="https://web-test.com/en/faq"/><xhtml:link rel="alternate" hreflang="en" href="https://web-test.com/en/en/faq"/></url>
...

However on getServerSideProps I managed it, i leave my configuration there:

export const getServerSideProps = async (ctx) => {
  const { data: ports } = await axios.get(getPorts());

  const fields = ports.map((port) => {
    const endpoint = `/ports/${String(port.ID)}`;
    return {
      loc: process.env.NEXT_PUBLIC_WEB_URL + endpoint,
      lastmod: port.UpdatedAt || new Date().toISOString(),
      // changefreq
      // priority
      alternateRefs: alternateRefs.map((ar) => ({
        ...ar,
        href: ar.href + endpoint,
      })),
    };
  });

  return getServerSideSitemap(ctx, fields);
};

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:16
  • Comments:9

github_iconTop GitHub Comments

5reactions
gkielwassercommented, Mar 9, 2022

Hi @iamvishnusankar

I’m running into the same issue, the locale from the loc is reused to build the alternate link:

<url>
    <loc>http://localhost:3000/en-US/contact</loc>
    <xhtml:link rel="alternate" hreflang="en-US" href="http://localhost:3000/en-US/en-US/contact"/>
    <xhtml:link rel="alternate" hreflang="de-DE" href="http://localhost:3000/de-DE/en-US/contact"/>
    <xhtml:link rel="alternate" hreflang="it-IT" href="http://localhost:3000/it-IT/en-US/contact"/>
  </url>

May I know why did you close this ticket ? I’m using “^2.5.7” Also this is about the same issue I guess https://github.com/iamvishnusankar/next-sitemap/issues/284.

For now I have applied this workaround Here is my config

alternateRefs: [
    {
      href: process.env.NEXT_PUBLIC_FRONT + "/en-US",
      hreflang: "en-US",
    },
    {
      href: process.env.NEXT_PUBLIC_FRONT + "/de-DE",
      hreflang: "de-DE",
    },
    {
      href: process.env.NEXT_PUBLIC_FRONT + "/it-IT",
      hreflang: "it-IT",
    },
  ]

And the fix

transform: async (config, path) => {
    return {
      loc: path,
      changefreq: config.changefreq,
      priority: config.priority,
      lastmod: config.autoLastmod ? new Date().toISOString() : undefined,
      alternateRefs: config.alternateRefs.map(alternate => {
        // Ex: try to find '/en-US/'
        const hasPathLocale = locales.indexOf(path.substring(1, 6)) !== -1

        //  Only fix alternateRefs if path has a locale
        return hasPathLocale ? {
          ...alternate,
           // Note: concat original alternate with  '/en-US/my-page' => my-page
          href: `${alternate.href}/${path.substring(7)}`,
          hrefIsAbsolute: true
        } : alternate
      }) ?? [],
    };
  }

Thanks!

4reactions
mosuskycommented, Mar 29, 2022

solution from @gkielwasser fixed the issue for me. Thanx!

@iamvishnusankar I think this patch should be applied to the package.

Read more comments on GitHub >

github_iconTop Results From Across the Web

curl: (60) SSL certificate problem: unable to get local issuer ...
If the default bundle file isn't adequate, you can specify an alternate file using the --cacert option. If this HTTPS server uses a...
Read more >
Style Sheets in HTML documents - W3C
Documents that include elements that set the style attribute but which don't define a default style sheet language are incorrect.
Read more >
National Guidelines for Behavioral Health Crisis Care
The National Guidelines for Crisis Care – A Best Practice Toolkit advances national guidelines in crisis care within a toolkit that supports ...
Read more >
Test an insecure registry - Docker Documentation
Edit the daemon.json file, whose default location is ... Substitute the address of your insecure registry for the one in the example.
Read more >
Deploy an Azure Pipelines agent on Linux - Microsoft Learn
Prepare permissions · Information security for self-hosted agents · Decide which user you'll use · Confirm the user has permission.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found