日本語

Fixing Lighthouse “Charset declaration is missing” Warning

​​Lighthouse​HTML,Mojibake,Troubleshooting

Lighthouse's best-practices audit reports "Charset declaration is missing or occurs too late in the HTML" when the browser is not told the page's character encoding. Adding <meta charset="utf-8"> as the first thing in <head> clears the warning. A Japanese page without a declaration can also show garbled text in the browser, and this one line fixes that too.

In my test environment, I confirmed that a page without a charset declaration triggers the warning, and that declaring it with a meta tag or the HTTP Content-Type header clears it. I also tested a declaration that comes late in the page and a file with a BOM.

Note

The test environment was Ubuntu 24.04, Node.js 22.23.5, and Lighthouse 13.6.0 (CLI installed with npm). The browser was headless Google Chrome for Testing 158, specified with CHROME_PATH. I checked the browser display with Chromium 158 launched by Playwright 1.63.0. I ran only the best-practices category (--only-categories=best-practices) with the default mobile settings. I created the pages locally and served them from 127.0.0.1 with a small Node.js server. I did not test the Lighthouse panel in Chrome DevTools, PageSpeed Insights, or server settings for Apache or nginx.

The page that triggers the warning

I used a Japanese page with one heading and one paragraph. The file is saved as UTF-8, but it has no <meta charset>.

html
<!doctype html>
<html lang="ja">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>文字コードの検証(直す前)</title>
</head>
<body>
<main>
<h1>今日の猫</h1>
<p>窓辺で寝ているところです。日なたが好きで、午後はずっとここにいます。</p>
</main>
</body>
</html>

The server returns this page with Content-Type: text/html, and the header has no charset either. I ran Lighthouse on this state with the following command.

bash
npx lighthouse http://127.0.0.1:8000/bad.html \
  --only-categories=best-practices \
  --output=json --output=html --output-path=./out/bad \
  --chrome-flags="--headless=new --no-sandbox" --quiet

In the HTML report, this warning appeared under BROWSER COMPATIBILITY (the Japanese-language report shows this label in Japanese, meaning "browser compatibility"). The explanation says that a <meta> tag must appear within the first 1,024 bytes of the HTML, or the encoding must be declared in the Content-Type header of the HTTP response.

Lighthouse report with the BROWSER COMPATIBILITY section expanded, showing "Charset declaration is missing or occurs too late in the HTML." The explanation says a character encoding declaration is needed.
Report for a page without a declaration. The audit appears under BROWSER COMPATIBILITY (labeled in Japanese in the Japanese-language report, meaning 'browser compatibility’)

From the JSON report, the score and the result of the charset audit are as follows.

text
best-practices score: 96  (weight 25/26)
[charset] weight=1 score=0 (binary)
  title: Charset declaration is missing or occurs too late in the HTML
failed best-practices audits: charset

Only charset failed, and the best-practices score was 96.

Without a declaration, the page is garbled

I opened the same page in Chromium 158, launched by Playwright (the same version as Lighthouse). The heading and body text were both garbled. The browser's character encoding (document.characterSet) was windows-1252. The result was the same whether I set the browser language to Japanese (ja-JP) or English (en-US).

Page without a charset declaration opened in a browser. The heading (Japanese for "Today's Cat") appears as meaningless symbols such as "今日ã®çŒ«."
Page without a declaration. UTF-8 Japanese was read as windows-1252 and garbled

The file itself is still UTF-8, but without a declaration, the browser may read it using a different encoding. This audit has only a small effect on the score, but on Japanese pages it directly determines whether the text displays correctly.

Add meta charset at the top of head

The fix is a single line inside <head>. The Chrome for Developers explanation page for this audit also recommends making <meta charset> the first element in <head>.

html
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>文字コードの検証(直した後)</title>
</head>

When I ran Lighthouse again under the same conditions, charset passed and the score was 100.

text
best-practices score: 100  (weight 26/26)
[charset] weight=1 score=1 (binary)
  title: Properly defines charset
failed best-practices audits: (none)

The page also displayed correctly in the browser, and document.characterSet was UTF-8.

Page with meta charset opened in a browser. The heading (Japanese for "Today's Cat") and the Japanese body text display correctly.
Page with meta charset. Japanese displays correctly

Use utf-8 as the value. The MDN page for the <meta> element says that the charset attribute value must match "utf-8" case-insensitively, and that a <meta> element declaring the encoding must be entirely within the first 1,024 bytes of the document.

I also tested the older form, <meta http-equiv="Content-Type" content="text/html; charset=utf-8">. charset passed and the score was 100. For new pages, however, the short <meta charset="utf-8"> is enough.

Declare it in the server's Content-Type header

If you cannot edit the HTML, adding charset to the Content-Type header returned by the server also passes. In my test, I served the same page without a meta tag and changed only the header.

text
Content-Type: text/html; charset=utf-8

In this case, charset also passed and the score was 100. The page did not garble in the browser, and document.characterSet was UTF-8. How you set headers depends on your server software, so check the documentation for your server.

How the declaration method affects the result

I tested pages with different declaration methods. In every case, the difference in score came only from the charset audit.

Declaration method charset audit Score Encoding in browser
No declaration Failed 96 windows-1252 (garbled)
<meta charset=""> (empty value) Failed 96 windows-1252 (garbled)
<meta charset="utf-8"> after a comment of 1,150 bytes and other content Failed 96 UTF-8
File with a BOM (no meta) Failed 96 UTF-8
<meta charset="utf-8"> as the first thing in <head> Passed 100 UTF-8
<meta http-equiv="Content-Type" ...> Passed 100 UTF-8
Content-Type: text/html; charset=utf-8 header Passed 100 UTF-8

When the value was empty, as in <meta charset="">, the page was treated as having no declaration, and it was garbled.

The declaration comes after 1,024 bytes

I put a long comment made only of ASCII characters before <meta charset>, so that the part before the declaration was 1,150 bytes. This page failed. It was not garbled in the Chromium I tested, but Lighthouse still reported the warning. Do not put comments or scripts before <meta charset>.

On the other hand, I put 400 Japanese characters (1,200 bytes in UTF-8) before the declaration, so the part before it was 1,250 bytes in total. This page passed. The source of the charset audit in Lighthouse 13.6.0 checks whether a meta tag appears within the first 1,024 characters. Japanese characters take 3 bytes each in UTF-8, so a page can be within 1,024 characters and still exceed 1,024 bytes. The HTML spec defines the limit in bytes. Even if Lighthouse passes, put <meta charset> as the first thing in <head>, before <title> and other Japanese text.

A BOM alone did not pass

The Chrome for Developers explanation page says a BOM (the 3-byte sequence EF BB BF at the start of a file) counts as a charset declaration. However, a page with a BOM and no meta tag failed in my test environment. The browser detected the BOM and displayed the page as UTF-8, so the text was not garbled.

The Lighthouse audit checks for a BOM by testing whether the first character of the page body is U+FEFF. When I looked at the data Lighthouse collected (saved with --gather-mode), the first bytes of the response were ef bb bf, but the first character of the body Lighthouse received was < (0x3C). The BOM had been removed before the body reached the audit, so the BOM check did not match. For a file with a BOM, you still need to add <meta charset="utf-8"> or declare the encoding in the Content-Type header to clear the warning.

The effect on the score is small

In Lighthouse 13.6.0's default config file (default-config.js), the charset audit has a weight of 1 in best practices. For my test page, the total weight of the audits included in the calculation was 26. When charset fails, the score is 25 ÷ 26, which rounds to 96. On a page where the total weight is larger, the drop is 1 ÷ (total weight), which is even smaller. Even so, a missing declaration can garble Japanese text, so fix it whenever the warning appears.

​Posted by 86