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.

Adding hreflang alternate links in head

See original GitHub issue

I’m trying to automatically add the hreflang links in the head but it’s not that easy.

I moved the locales array outside of the module.exports and added the following code in thenuxt.config.js file and it kinda works:

const locales = [
  { code: 'en', ... },
  { code: 'fr', ... }
]

module.exports = {
  ...
  head: {
    link: [
      ...
    ].concat((function () {
      let alternates = []
      locales.forEach((locale) => {
        alternates.push({
          hid: `alternate-${locale.code}`,
          rel: 'alternate',
          hreflang: locale.code,
          href: `https://example.com/${locale.code}`
        })
      })
      return alternates
    })())
  }
}

However, I didn’t find a way to dynamically change the href value. The method switchLocalePath() is not accessible from the nuxt.config.js file and it seems we can’t access to this.$route.name too.

Any idea on how to do that? What about nuxt-i18n injecting theses alternate links by default since Nuxt is sold as SEO-friendly out of the box. It would be a great addition to the module.

Finally an unrelated question. Why nuxt-i18n is not a scoped package like the sitemap module for instance @nuxtjs/sitemap?

<div align="right">This question is available on Nuxt.js community (#c4)</div>

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:4
  • Comments:11

github_iconTop GitHub Comments

7reactions
phortxcommented, Apr 11, 2019

@paulgv This is a serious issue not just because of the broken tags but also because it generates thousands of warnings which really causes high cpu usage and makes the app slow.

4reactions
mirash333commented, Jan 13, 2021

Ok, I think I found a solution to the problem, in layouts, in default.vue we create a base head for our application and take the fields with this.$nuxtI18nSeo():

head() {
    return {
        link: this.$nuxtI18nSeo().link.filter(
            (item) => (item.rel = 'alternate' && item.hreflang)
            ),
        }
    },

This option will dynamically create links, so far I see the solution to this problem like this.Hope helped.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Hreflang: The Easy Guide for Beginners - Ahrefs
To implement hreflang tags correctly for this setup, we'd add this code to the <head> section of each of our pages: <link rel="alternate" ......
Read more >
Dynamically add a link tag to the page head section with ...
1. One of the options is to add the links or the link in the head section of the pages manually. · 2....
Read more >
What are Hreflang Tag Attributes And How To Implement Them
Introduced by Google in December 2011, the hreflang attribute allows you to show search engines what the relationship is between web pages in...
Read more >
hreflang: the ultimate guide • Yoast
This guide discusses what hreflang is, what it is for and gives in-depth information on how to implement it for your multilingual websites....
Read more >
How to Add Hreflang Tags (Without Breaking Your SEO)
Many people use hreflang tags wrong, and that's bad news for your SEO. Our clear guide shows you how to add them correctly....
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