Software Engineering

A perfect Lighthouse SEO score does not prove SEO, the HTML your site serves does

Lighthouse gave my site an SEO score of 100 while the canonical pointed to a domain that returned 404. What the audit checks, what it cannot check and a 40-line checker that catches the defect.

Rodrigo Lucas · · 11 min read

On November 20, 2026, Lighthouse gave this site, rodrigolucas.dev, a score of 100 in the SEO category. On the same day, the checker I wrote to verify what the site publishes about itself passed 34 of 115 checks. Both were right and they measured different things, because while the score closed at 100, every address the site announced to Google as official led to an error page.

A canonical that returned 404 with SEO 100#

I ran Lighthouse (version 12.6, mobile profile) on the home page and on a post, and both scored 100 in SEO. But when I opened the HTML that production served, the canonical, the hreflang, the og:url, the preview images, every entry in sitemap.xml, the Sitemap: line in robots.txt and the feed links all pointed to an old Vercel subdomain that returned 404, with a double slash in the path on top of that (https://...vercel.app//blog/...).

Visitors saw nothing broken, because the page loaded and the internal links worked. The defect lived in the metadata that search engines and social networks read to learn the official address. Search Console showed that Google had read exactly that: in the URL inspection for the home page, the "User-declared canonical" field had the old domain, from a crawl on October 28, and the oldest post on the blog was listed as "discovered, currently not indexed".

The cause was mundane. The NEXT_PUBLIC_SITE_URL variable, set in the Vercel dashboard, held the old domain and ended with a slash. The code built every absolute URL as ${url}${path}, and every path starts with /, so one wrong value in the dashboard plus one extra slash were enough to break every address the site published about itself.

What the canonical audit checks#

My first reaction was to think Lighthouse had failed. I went and read the audit, which is a single file in its repository and takes a few minutes to read, and it does what it promises, it just promises less than the score suggests.

It fails the page in five situations: more than one conflicting canonical, an invalid URL, a relative URL, a canonical pointing to another page in the same hreflang group, and a canonical pointing to the domain root when the page is not the home page. The audit makes no request to the declared address, so a canonical that returns 404 passes as long as it is a well-formed absolute URL.

What if the canonical points to another domain? The audit's documentation page still lists that case as a reason to fail, but the code stopped checking it in November 2021, in pull request #13412, titled "remove cross-origin check". The reasoning makes sense, since a canonical pointing to another domain has been legitimate use ever since rel=canonical was created, in 2009. Someone who republishes an article on a portal points the canonical to the original text on their own blog, and that is correct.

To a generic tool, a canonical on another domain can be content syndication or a configuration error, and both have the same HTML. It does not know which domain is yours. Only whoever runs the site knows that every announced address should be on rodrigolucas.dev, so that rule is up to them. Lighthouse is still good at what it measures (title, meta description, indexable page, crawlable links), and a 100 only means none of its checks failed. None of them asks whether Google can reach the address you declared.

Four layers between the code and the HTML#

After Lighthouse, I looked at the code. The file that generates the metadata was correct and built the canonical from the site URL and the page path. The error was in the value it received, in a dashboard that no pull request reviewer sees.

This holds for any stack. The served HTML is the code applied to a configuration, in a specific build, behind a proxy and a cache, and each layer can change the result without changing a line, which is what happened with four of them on this site.

In Next.js, every variable with the NEXT_PUBLIC_ prefix is replaced by its literal value during next build, and the documentation warns that after the build the app no longer responds to changes in these variables. Changing the value in the dashboard does nothing until the next deploy. And even after the deploy the cache held on to the old version, because the home page was static and had been cached for about seven days.

The proxy got in the way too. During the fix, the preview image for the Portuguese pages returned 308, because it went through the proxy's language rule before reaching the image route. The image code was correct and the problem was in the path to it.

Last, layout inheritance. The /blog and /portfolio pages inherited the Open Graph data from the root layout, and with it the home page's og:url, so anyone sharing the blog page sent the home page link. When I declared a separate openGraph on those pages, they lost the inherited image.

None of these four shows up when you read the code, and all of them show up in a curl. The code shows what should go out, and only the HTTP response from production shows what actually went out.

The question the checker asks#

If the generic tool does not know which domain is yours, you can write a check that does. The question is whether every URL the site announces about itself exists and belongs to this site, and those URLs fit in a short list:

  • the <link rel="canonical"> of each page, which tells the search engine the official address;
  • the og:url, which the Open Graph protocol defines as the canonical URL of the object, used as a permanent identifier when someone shares it;
  • each <loc> in sitemap.xml, which Google asks for as an absolute URL and which should list only the addresses you want in the results;
  • the Sitemap: line in robots.txt;
  • and, if present, hreflang, og:image, feed links and llms.txt links.

Each one needs to be on the domain of the base I am testing, with no double slash in the path, and return 200. A 301 or a 308 does not count, because Google treats a redirect as a strong signal that the target should become the canonical, and a canonical that redirects sends two signals that conflict. The announced address has to be the final one.

A checker in 40 lines#

The checker that runs on this site is about 600 lines, because it also checks JSON-LD, one <h1> per page, icons, the manifest and the IndexNow file. The core, which would have caught the defect in this post, fits in 40 lines of Node with no dependencies.

// check.mjs: node check.mjs https://example.com
const BASE = new URL(process.argv[2]).origin;
const failures = [];

// Every URL the site announces about itself: requested domain, no double slash, 200.
async function require200(url, where) {
  if (!url) return failures.push(`${where}: missing`);
  if (!URL.canParse(url)) return failures.push(`${where}: invalid URL ${url}`);
  const u = new URL(url);
  if (u.origin !== BASE) return failures.push(`${where}: wrong domain ${u.origin}`);
  if (u.pathname.includes("//")) return failures.push(`${where}: double slash ${url}`);
  const r = await fetch(url, { redirect: "manual" });
  if (r.status !== 200) failures.push(`${where}: HTTP ${r.status} at ${url}`);
}

const read = (text, re) => text.match(re)?.[1];
const download = async (url) => (await fetch(url)).text();

const robots = await download(`${BASE}/robots.txt`);
await require200(read(robots, /^sitemap:\s*(\S+)/im), "robots.txt Sitemap");

const sitemap = await download(`${BASE}/sitemap.xml`);
const locs = [...sitemap.matchAll(/<loc>([^<]+)<\/loc>/g)].map((m) => m[1].trim());
if (!locs.length) failures.push("sitemap.xml: empty");

for (const loc of locs) {
  await require200(loc, "sitemap");
  // Audit the page on the requested base, even if the sitemap announces another domain.
  const path = new URL(loc).pathname;
  const html = await download(BASE + path);
  const canonical = read(html, /<link[^>]*rel="canonical"[^>]*href="([^"]+)"/i);
  const ogUrl = read(html, /<meta[^>]*property="og:url"[^>]*content="([^"]+)"/i);
  await require200(canonical, `${path} canonical`);
  if (URL.canParse(canonical ?? "") && new URL(canonical).pathname !== path)
    failures.push(`${path}: canonical points to another page (${canonical})`);
  if (ogUrl !== canonical) failures.push(`${path}: og:url differs from canonical (${ogUrl})`);
}

for (const f of failures) console.log(`✗ ${f}`);
console.log(`${failures.length} failure(s) at ${BASE}`);
process.exit(failures.length ? 1 : 0);

fetch follows redirects by default, so a 308 would turn into a 200 without anyone knowing. With redirect: "manual", the 308 on the preview image shows up as a failure.

The page is downloaded from the base being tested, not from the address the sitemap gives. If the sitemap announces the wrong domain, which is the defect we are looking for, following the <loc> would hit a 404 and the rest of the page would go unaudited. So the checker records the <loc> failure and keeps reading the same page on the right domain.

The og:url is compared with the canonical, and the canonical has to point to the page itself. The pages that inherited the home page's og:url would pass a domain and status check, since the home page exists and is on the right domain, and the defect only shows up when both fields have to say the same thing. In the same way, a canonical pointing to the home page on every page would pass the three requirements and drop the rest of the site from the index.

The output is the exit code, an exit 1 on any failure, and that is what lets the script act as a gate in CI.

The regular expressions over HTML are the fragile part. The ones in the example assume rel comes before href and that attributes use double quotes, which works because the framework generates stable HTML. On a site with handwritten HTML, a real parser is worth it. The checker also does not run JavaScript, so a canonical injected in the browser shows up as "missing". I prefer it that way, because metadata that only exists after JavaScript is already a different problem to fix.

From red to CI#

Before fixing anything, I ran the checker against the broken production site and confirmed it failed, for the reason I explain in Tests that pass without protecting anything. In production, 34 of the 115 checks passed.

Then came the fixes. I normalized the URL in the code with a .replace(/\/+$/, ""), which strips any trailing slash wherever the value comes from, and tested locally with the variable ending in a slash, just like the one in the dashboard. That local test is where the 308 on the preview image showed up, which I had missed in the manual audit. Over the course of the work the checker gained new checks (JSON-LD, icons, IndexNow) and the total grew from 115 to 151, and after the redeploy production closed with all 151 passing.

In CI, the test build runs with NEXT_PUBLIC_SITE_URL=http://localhost:3000/, trailing slash on purpose, to reproduce the value that caused the defect. The pipeline starts the server and runs the checker against it. If someone removes the normalization, or a new page inherits the wrong og:url, the pull request goes red before it reaches production.

The price is one more script to maintain. Every new piece of metadata the site starts announcing has to be added to the list, and if nobody adds it, the checker stays green over a field it does not even look at. A different domain can also be correct: if you republish third-party content with a canonical pointing to the source, you need an exceptions list, for the same reason that led Lighthouse to remove the check.

What Google has not answered yet#

On Google's side, the official source is Search Console. In URL inspection, "User-declared canonical" is the canonical the page declared and "Google-selected canonical" is the one Google chose after comparing similar pages. The documentation warns that this choice happens at indexing time and that the live test cannot predict whether the tested version will be considered canonical.

In practice, Search Console shows what Google read in the last crawl, which in my case was weeks old. It confirmed the defect, but it would not have caught it in time, and after the fix it takes the search engine's own time to reflect the new state. When a crawl of the home page after the fix shows up, I will report the result in an addendum to this post.

The checker also does not tell you whether the page will be indexed. A 200 with duplicate content, or a page with noindex, passes the three requirements, and indexing depends on quality, on links and on the search engine's decision. That is why I keep all three tools: Lighthouse for the technical quality of the page, Search Console for what Google actually did and the checker for what the site announces right now, in the HTTP response. If you have a site on your own domain and have never opened the HTML it serves, a curl on the home page looking for rel="canonical" already shows whether the address you announce is yours.

References#

updated on

01 blog

related

02 blog

Who writes

Contact

Let's swap ideas

Get the next articles

No spam, one email when a new article ships. Unsubscribe anytime.