Next.js metadata and Googlebot: fix canonicals in body

Next.js metadata can reach Googlebot in the body, because the default bot list leaves it out. How 191 pages lost head canonicals, how to check, and the fix.

By

On a photo marketplace I work on, Googlebot Smartphone, the crawler behind mobile-first indexing, was getting the title, description and canonical inside <body> on 191 category and listing pages. Nothing looks wrong to a person in a browser, so this is easy to miss. The cause is how Next.js metadata reaches Googlebot: streaming sends the tags after the head has gone, and the list of bots that get a blocking render leaves out the main Google crawler. This post covers which crawlers are on that list, what Google says about canonicals, how to check your own site with curl, and what the fix costs.

How Next.js metadata reaches Googlebot

Since version 15.2, Next.js can send the first part of a page without waiting for generateMetadata to finish. The generateMetadata docs on streaming metadata say that when it resolves, the tags are appended to <body>. The benefit is real: a lower time to first byte, and people see the page sooner.

Clients that can't run JavaScript would miss those tags, so Next.js keeps a list of "HTML-limited bots" and matches it against the user agent. Anyone on the list gets a blocking render, with the metadata in <head> where it has always been. Everyone else gets the streamed version.

Whether a given page is affected depends on how it renders. On Next.js 16.2.7, a page whose metadata waits on request-time data streamed its title and canonical into the body. A fully prerendered page kept them in the head for every user agent, so those pages have nothing to fix.

Is Googlebot on the htmlLimitedBots default list?

No. On Next.js 16.2.7 the default list does not match Googlebot Smartphone or Googlebot Desktop, the two crawlers that do the indexing. It also misses Googlebot-Image, Googlebot-Video and GoogleOther. It does match AdsBot-Google, Mediapartners-Google, Storebot-Google and Google-InspectionTool, plus Bingbot.

The docs say the defaults include "Google crawlers", which is easy to read as "Google is covered". The regex in html-bots.js is [\w-]+-Google|Google-[\w-]+, which needs a hyphen touching the word Google, and "Googlebot" has none. The omission is deliberate. Next.js files Googlebot under bots that run JavaScript, and its docs say they verified that metadata is interpreted correctly by bots that inspect the full DOM.

The trap is Google-InspectionTool. Google's crawler docs say that user agent drives the Rich Results Test and URL Inspection in Search Console. It's on the list, so those tools get the blocking render and show a perfect head, while the crawler that indexes you gets the streamed page. In a test app, the same URL gave a head canonical to the inspection user agent and a body canonical to the Googlebot one. Chrome-Lighthouse is on the list too, so don't rely on an audit that identifies itself that way either.

Does a canonical have to be in the head?

Google's page on consolidating duplicate URLs is direct: the rel="canonical" link element "is only accepted if it appears in the <head> section of the HTML". It also prefers the canonical in the HTML source, and treats setting it with JavaScript as the fallback when you can't.

Set that beside the Next.js statement and the two documents leave a question open. Next.js says Googlebot reads the streamed tags after rendering. Google's page doesn't describe what happens to a canonical that starts in the body. I didn't want 191 pages depending on the answer, so I chose the reading that can't go wrong: the canonical sits in the head of the HTML that Googlebot receives.

To be plain about the result, I have no ranking number to show for this. The change removes a risk, and that was reason enough on pages whose job is to be found.

How to check your own site with curl

You need the real Googlebot user agent and a check that only looks at the part before </head>. This prints the canonical if it's in the head and prints nothing if it isn't:

UA='Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5X Build/MMB29P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)'

curl -s -A "$UA" https://example.com/some-category \
  | tr -d '\n' | sed 's#</head>.*##' \
  | grep -o '<link[^>]*rel="canonical"[^>]*>'

Empty output means trouble. Run the same curl without the tr and sed steps, and if the tag now appears, it's in the body. To check the title, end the pipe with grep -o '<title[^>]*>' instead.

Test one URL of each page type, and pick the ones that fetch data for their metadata. Category, listing and search pages are the likely ones. Your homepage may well pass while the pages below it fail. Run this against production, since a CDN or proxy in front of the app can change what a crawler receives. Some firewalls block a fake Googlebot, so if the output is empty, confirm you got the real page and not a block page.

Add Googlebot to htmlLimitedBots and keep the defaults

The htmlLimitedBots option is one line in next.config.js, with a catch: setting it replaces the default list. Pass only /Googlebot/ and every social preview bot goes back to streamed metadata, which is how link previews quietly lose their titles. Copy the defaults and add to them:

const nextConfig = {
  // Next's defaults from html-bots.js, with Googlebot added at the front.
  htmlLimitedBots:
    /Googlebot|[\w-]+-Google|Google-[\w-]+|Chrome-Lighthouse|Slurp|DuckDuckBot|baiduspider|yandex|sogou|bitlybot|tumblr|vkShare|quora link preview|redditbot|ia_archiver|Bingbot|BingPreview|applebot|facebookexternalhit|facebookcatalog|Twitterbot|LinkedInBot|Slackbot|Discordbot|WhatsApp|SkypeUriPreview|Yeti|googleweblight/i,
};

In my case Googlebot, bingbot and the AI search fetchers now get a blocking render. Since the list is a copy, compare it with html-bots.js when you upgrade Next.js. Next.js rebuilds the pattern with the i flag itself, so matching is case-insensitive either way.

The cost is response time. The docs warn that overriding the list could lead to longer response times, and the reason is simple: a listed bot waits for generateMetadata before it receives the first byte. If your metadata does a slow fetch, Googlebot now pays for that on every crawl, so keep that query fast. People in browsers still get streaming, so their experience doesn't change. Setting htmlLimitedBots: /.*/ turns streaming off for everyone, and I'd only do that if you really have no use for it.

What else Googlebot was seeing

Once you start looking at pages as a crawler does, other problems turn up. The same pass fixed three more:

  • Empty categories. Categories with no photos are now marked noindex, follow and left out of the sitemap until they have some.
  • Redirected URLs in sitemaps. One image was listed while a 301 pointed it at another photo. Sitemaps now skip any URL that a redirect rule sends elsewhere.
  • Shared descriptions. Each events filter page now gets its own description.

Then there was the welcome pop-up. On phones it covered the page right after someone arrived from search, which Google treats as an intrusive interstitial. Its guidance says these make content harder to understand, which may lead to poor search performance, and it recommends banners that take up only a small fraction of the screen. So phones now get a dismissible bar along the bottom at 9% of the screen, and desktop keeps the dialog. That same modal caused a different bug earlier, which I wrote up in fixing React hydration errors.

Metadata has plenty of other ways to go missing. A page-level openGraph object can wipe out your default share image, and if you're already reviewing which bots get what, controlling AI crawlers with robots.txt belongs on the same list.

The short version

Next.js streams metadata into the body for any user agent outside htmlLimitedBots, and the default list leaves out Googlebot while including the tool you'd use to check it. Google says a canonical is only accepted in the head. Curl a data-driven page with the Googlebot user agent, and if the canonical is missing from the head, add Googlebot to a full copy of the default list. Accept that crawlers then wait for your metadata, and keep that query quick.

If you want a second pair of eyes on a Next.js site, see my services page.

Want this kind of work on your product?

Get in touch