日本語

How to Fix the Lighthouse “hreflang” Invalid Error

​​Lighthouse​hreflang,HTML,multilingual,SEO,Troubleshooting

The Lighthouse SEO audit "Document doesn't have a valid hreflang" (the Japanese report shows a label meaning "Document doesn't have a valid hreflang specified") has only two causes: the language code is not a registered language code, or the href does not start with http: or https:. Open the finding, and Lighthouse shows a reason for each problematic <link>. Fix each one according to that reason.

hreflang tells search engines which URL is meant for which language when a multilingual site has separate pages for each language or region, such as a Japanese version and an English version (Chrome for Developers explains this audit).

I prepared 19 pages with different hreflang markup in a test environment, ran Lighthouse on each, and checked which markup failed and how to fix it so it passes.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.5, and Lighthouse 13.6.0 (the CLI installed with npm). The browser was headless Google Chrome for Testing 153. I ran only the SEO category (--only-categories=seo) with the default mobile settings. The pages were served from http://127.0.0.1:8302 by a small Node.js server. I did not test the Lighthouse panel in Chrome DevTools, PageSpeed Insights, hreflang declarations in sitemaps, or how actual search engines handle hreflang.

Use the reason under the table to tell which error it is

I tested a Japanese page whose <head> contained the following. The language code was jp, and the href was a root-relative URL starting with /.

html
<link rel="alternate" hreflang="jp" href="/both/">
<link rel="alternate" hreflang="en" href="http://127.0.0.1:8302/en/both/">

I ran Lighthouse like this. To get the Japanese report, add --locale=ja.

bash
npx lighthouse http://127.0.0.1:8302/both/ \
  --only-categories=seo --locale=ja \
  --output=json --output=html --output-path=./out/ja-both \
  --chrome-flags="--headless=new --no-sandbox" --quiet

A page with valid hreflang scored 100 in SEO, but this page scored 90. When you open the finding, the problematic <link> elements are listed in a table, and the reasons appear below. The correctly written English <link> does not appear in the table.

Lighthouse Japanese report with the hreflang finding expanded. The table shows the link element hreflang="jp", and below it are two reasons: a label meaning "Unexpected language code" and a label that literally means "Related href value" (the Japanese rendering of "Relative href value")
Report when one link has two errors. Two reason lines are shown

There are two kinds of reasons:

Japanese report (translated) English report Cause
Unexpected language code Unexpected language code The language part of hreflang is not a registered language code
Related href value Relative href value The href does not start with http: or https:

The Japanese report's label for the second reason uses "related" in place of the English "Relative." It means the href is a relative URL.

In the default configuration file for Lighthouse 13.6.0, the weight of the hreflang audit is 1. On the test page, seven SEO audits counted toward the score, with a total weight of about 10.04 (is-crawlable about 4.04, and the other six at 1 each). Losing the hreflang weight of 1 brought the score to 90.

Make the language code a registered language code

When you see "Unexpected language code," review the hreflang value. These are the values that failed in my test and the corrected values:

Value that failed Corrected value Error
jp ja A region code (Japan is JP) was written as a language code
gb en-GB Only a region code (United Kingdom is GB) was written
ja_JP, en_US ja-JP, en-US The language and region were joined with _

The language code for Japanese is ja, not jp. In the list Lighthouse uses to check language codes (a list based on the IANA Language Subtag Registry), jp is not registered as a language.

Forms such as ja_JP are often used in programs to represent a locale. If you output them as-is in hreflang, they fail. In the source code of this audit, the first part of the value, split at -, is checked as the language code. If you join with _, the whole ja_jp is treated as one language code, which is not in the list, so it fails.

Pages with values such as ja-JP and en-US joined with - passed. Case is not significant, and x-default (the value used for a page where the user chooses a language) also passed.

Write href as a URL starting with https

When you see "Relative href value," rewrite the href as a full URL that includes the scheme and domain.

html
<!-- Fails -->
<link rel="alternate" hreflang="en" href="/en/cats/">
<!-- Passes -->
<link rel="alternate" hreflang="en" href="https://example.com/en/cats/">

In testing, relative URLs such as /relative/ failed, and so did protocol-relative URLs such as //127.0.0.1:8302/protocol-relative/ that omit http:. In the source code, the audit only checks whether the string in href starts with http: or https:.

The same check applies when hreflang is sent in an HTTP response header rather than in HTML.

console
$ curl -sI http://127.0.0.1:8302/header-relative/ | grep -i "^link"
Link: </header-relative/>; rel="alternate"; hreflang="ja", </en/header-relative/>; rel="alternate"; hreflang="en"

This page failed. The table listed each link from the header on a separate row. If the finding appears but you cannot see a <link> in the HTML, check whether the table has rows starting with Link:. A page where every URL in the header was written in full passed.

Lighthouse Japanese report with the hreflang finding expanded. The table shows two rows: "Link: </header-relative/>; rel="alternate"; hreflang="ja"" and "Link: </en/header-relative/>; …hreflang="en"", and each has the label that literally means "Related href value" (the Japanese rendering of "Relative href value") below it
Report when the hreflang in the response header uses relative URLs

Lighthouse checks the page after JavaScript has run. A page where JavaScript added a relative hreflang to <head> also failed.

Passing does not mean correct

This audit only checks the language part of the hreflang value and the beginning of the href. In testing, each of the following pages scored 100 in SEO with no findings.

hreflang markup The actual problem
A Korean page with hreflang="kr" kr is the code for Kanuri. Korean is ko
A page for the UK with en-UK The region code for the UK is GB
A Japanese page with only a link to the English version No link to itself
A page pointing to an English version whose link returns 404 The destination cannot be opened
A jp link with a relative URL inside <body> Lighthouse does not check it

kr is registered in IANA as the language code for Kanuri, not Korean. Because it is a registered language code, Lighthouse passes it. Korean is ko. A wrong value that matches another language's code does not produce a finding.

Lighthouse does not check the region part. The explanation says so too. According to the explanation, region codes are written in ISO 3166-1 alpha-2 format. The United Kingdom is GB, and the IANA registry has no region code UK.

The explanation says that each language version should link to all language versions, including itself. It also says that otherwise search engines may ignore hreflang or interpret it incorrectly. However, a page with no link to itself also passed. A page pointing to an English version whose link returns 404 also passed.

Links inside <body> are excluded from the audit in the source code. Wrong values do not produce a finding, so if you intended to put the link in <head> but it ended up in <body>, check the page source to confirm.

A page with no hreflang at all also passes this audit. If you forgot to write hreflang on a multilingual site, no finding will appear.

Point

After fixing the failing findings, check the rest yourself. Open each language's page and confirm that the hreflang values in <head> are correct codes such as ja, en, and ko, and that every language version, including the page itself, is linked.

​Posted by 86