日本語

How to Fix the Lighthouse HTTP Status Code Audit

​​Lighthouse​HTTP,Node.js,SEO,SPA

When Lighthouse reports "Page has unsuccessful HTTP status code" (the Japanese report uses different wording, meaning that the HTTP status code set for the page is not correct), the server for the page you tested is returning a status code from 400 to 599. If the page should appear in search results, make the server return a 2xx status code and the warning goes away. The page may display normally in the browser while the server setting still returns 404. That is why this item is worth checking when you are looking for the cause of pages that search engines do not index.

In a test environment, I built an SPA (single-page application) whose URLs all returned HTML with a 404 status code, and then checked the Lighthouse results. After I changed the server to return 200 only for the correct URLs, the warning disappeared and the SEO score went from 90 to 100.

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 Google Chrome for Testing 158, specified with CHROME_PATH and run in headless mode. I checked the status codes seen in the browser with Playwright 1.63.0. I ran only the SEO category (--only-categories=seo) with the default mobile settings. I created the pages in the test environment and served them from 127.0.0.1 with a small server written in Node.js. I checked the wording of the Japanese report against Lighthouse's translation files. I did not test the Lighthouse panel in Chrome DevTools, PageSpeed Insights, or how search engines actually register pages.

The page displayed normally but returned 404

The test used a small SPA where the JavaScript renders different content under /app/. There is one HTML file, and the page reads the URL and inserts the heading and body text.

The server returned this HTML with a 404 status code for SPA URL requests. This behaves the same as a static host that returns the SPA's HTML instead of 404.html for missing URLs.

js
if (p.startsWith('/app/')) {
  // Before the fix: every SPA URL returns app.html with 404
  return send(res, 404, 'app.html');
}

With this setup, you cannot notice the problem just by opening the page in a browser. These are the results when I opened the pages with Playwright and checked the status code and heading.

text
/app/: status=404  title="日記の一覧 | 猫の観察日記"  h1="日記の一覧"
/app/diary/1: status=404  title="窓辺で寝る猫の一日 | 猫の観察日記"  h1="窓辺で寝る猫の一日"
/app/nothing: status=404  title="ページが見つかりません | 猫の観察日記"  h1="ページが見つかりません"

/app/diary/1 displays as a diary page, but its status code is 404. The only difference from the nonexistent /app/nothing was the content on the screen.

The CLI stops with an error before reporting the issue

When I ran the Lighthouse CLI with default options against /app/diary/1, it did not produce audit results and ended with an error (exit code 1).

console
$ npx lighthouse http://127.0.0.1:8000/app/diary/1 \
  --only-categories=seo \
  --output=json --output=html --output-path=./out/bad-default \
  --chrome-flags="--headless=new --no-sandbox" --quiet
Runtime error encountered: Lighthouse was unable to reliably load the page you requested. Make sure you are testing the correct URL and that the server is properly responding to all requests. (Status code: 404)

(In practice, I ran it from a small shell script that set CHROME_PATH.)

The Japanese report shows a message with the same meaning: Lighthouse could not reliably load the requested page, and the status code was 404. In the JSON report, the SEO score was null, and the http-status-code audit was treated as an error.

In the Lighthouse 13.6.0 source, navigation-error.js checks the result of the page load and returns this error when the status code is an error (400 or higher, per the check in network-request.js). It only reports a warning instead of an error when the ignoreStatusCode setting is enabled. The CLI default is disabled, and you can enable it with the --ignore-status-code option in the CLI options.

On the other hand, the settings for the Chrome DevTools Lighthouse panel (devtools-entry.js) set ignoreStatusCode: true. When you run from the DevTools panel, the audit continues without stopping on the error. If you want to see this issue from the CLI, add --ignore-status-code. When the CLI fails with a runtime error, the fix is the same: change the status code the server returns.

With –ignore-status-code, the issue is reported

When I ran the same page with --ignore-status-code, it finished, and the SEO "Crawling and Indexing" section showed the issue. The status code returned appears after the title.

Lighthouse report with the "Page has unsuccessful HTTP status code — 404" audit expanded. The description says that pages with unsuccessful status codes may not be indexed correctly
The report for a page that returns 404. The status code appears after the title

A warning with the same text as the CLI error appears at the top of the report.

Warning at the top of the Lighthouse report. Under the line "There were issues affecting this run of Lighthouse", one sentence ends with "Status code: 404"
The warning shown at the top of the report when –ignore-status-code is used

I extracted the score and the http-status-code result from the JSON report with a small script I wrote. weight is the audit's weight, and weight total is the sum of the weights that were included in the calculation.

text
seo score: 90  (http-status-code weight 1, group seo-crawl; weight total 10.043478260869566)  failed: http-status-code
[http-status-code] score=0 (binary)  displayValue=404  title: Page has unsuccessful HTTP status code

Only http-status-code failed, and the SEO score was 90. In the default config (default-config.js), this audit's weight is 1. For the test page, the total weight was about 10.04, so (10.04 − 1) ÷ 10.04 gives 90.

Fix it by returning 200 for the correct URLs

The official Lighthouse explanation says to configure the server to return a 2xx status for all valid URLs, and a 3xx status for URLs that have moved. The test server returns 200 only for URLs the SPA can display, and keeps 404 for everything else.

js
const SPA_ROUTES = ['/app/', '/app/diary/1'];

if (p.startsWith('/app/')) {
  // After the fix: 200 only for URLs the SPA knows, 404 for the rest
  return send(res, SPA_ROUTES.includes(p) ? 200 : 404, 'app.html');
}

In the test, I wrote the URLs in an array. In a real app, you would decide this based on the router definitions or whether the data exists in the database.

After the fix, the run finished without --ignore-status-code, and the warning was gone.

text
requestedUrl: http://127.0.0.1:8000/app/diary/1  finalDisplayedUrl: http://127.0.0.1:8000/app/diary/1
seo score: 100  (http-status-code weight 1, group seo-crawl; weight total 10.043478260869566)  failed: (none)
[http-status-code] score=1 (binary)  displayValue=-  title: Page has successful HTTP status code

The nonexistent /app/nothing still returns 404 after the fix. Running Lighthouse on that page produces the same error as before, and you do not need to fix that. The official explanation also says that pages showing an error, such as a 404 page, should not be put in search results, and that you should first decide whether you want search engines to crawl the page.

The official explanation also notes that if you publish an SPA on GitHub Pages, you may have to return the content with a 404 status code. If your hosting does not let you set the status code yourself, you cannot use this fix on the server side.

500, 503, and redirect targets are judged the same way

I also tested other status codes with --ignore-status-code.

URL tested Server response http-status-code SEO
/ 200 Pass 100
/error 500 Fail (500) 90
/maintenance 503 (with Retry-After: 3600) Fail (503) 90
/old-diary 301 to /diary/deleted, and the destination returns 404 Fail (404) 90
/soft-404 200 (content is a "page not found" message) Pass 100

The audit source (http-status-code.js) fails the audit when the status code is from 400 to 599. The 503 used for maintenance failed even with Retry-After set.

For a URL that redirects, the status code of the destination page was used. The 301 from /old-diary itself was not flagged; the report showed the 404 from the final destination, /diary/deleted. The report also showed a warning to test the destination URL directly. If you still link to the old URL, check whether the destination still exists.

The last case, /soft-404, returns the "page not found" screen with 200. This audit only checks the number in the status code, so it passes even when the content is an error screen. Returning 200 for a nonexistent page just to clear the warning is not the correct fix. Keep 404 for pages that do not exist, and leave them out of the pages you run Lighthouse on.

​Posted by 86