Mega Menu SEO: Your Dropdown Links May Not Be in the HTML

本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。

Mega Menu SEO: Your Dropdown Links May Not Be in the HTML

A mega menu can look perfect, open on hover, pass every accessibility check, and still give Google zero links. If the dropdown is built in React and only renders its panel when it opens, the links aren’t in the HTML your server sends, and Googlebot never opens the menu to make them appear.

大型菜单看起来可能非常完美,悬停即可打开,通过了所有可访问性检查,但却可能无法为 Google 提供任何链接。如果下拉菜单是使用 React 构建的,并且仅在打开时才渲染其面板,那么这些链接就不会出现在服务器发送的 HTML 中,而 Googlebot 从不通过打开菜单来使它们显示。

I found this on my own site this week: the header listed nine service pages, and the live home page linked none of them from the header. This post covers how to check your own menu in 30 seconds, which popular React menu components behave this way (I tested Radix, shadcn/ui’s base and Headless UI), and the fix I shipped, with its real cost in DOM size.

本周我在自己的网站上发现了这个问题:页眉列出了九个服务页面,但实时主页的页眉中并没有链接到其中任何一个。本文将介绍如何在 30 秒内检查你自己的菜单,哪些流行的 React 菜单组件会有这种行为(我测试了 Radix、shadcn/ui 的基础组件和 Headless UI),以及我所采用的修复方案及其在 DOM 大小方面的实际成本。

The short answer: Does Google crawl mega menu links? Only if they’re real <a href> links in the page’s HTML. Google renders JavaScript, but it doesn’t hover or click, so a panel that renders only on hover or click never appears for it.

简短的回答是:Google 会抓取大型菜单链接吗?只有当它们是页面 HTML 中真实的 <a href> 链接时才会。Google 会渲染 JavaScript,但它不会进行悬停或点击操作,因此仅在悬停或点击时才渲染的面板对它来说永远不会出现。

Which menus are affected? In my test, the closed state of Radix NavigationMenu, Radix DropdownMenu, Headless UI Menu, Popover and Disclosure, and any {open && <ul>} pattern, all rendered 0 of 2 links.

哪些菜单会受到影响?在我的测试中,Radix NavigationMenu、Radix DropdownMenu、Headless UI Menu、Popover 和 Disclosure 的关闭状态,以及任何 {open && <ul>} 模式,在渲染时都未能显示两个链接中的任何一个。

The fix: keep the links in the HTML while the menu is closed. Use forceMount (Radix), unmount={false} (Headless UI), a CSS-hidden panel, or a hidden list of plain links.

修复方法:在菜单关闭时将链接保留在 HTML 中。可以使用 forceMount (Radix)、unmount={false} (Headless UI)、CSS 隐藏面板或隐藏的纯链接列表。

What I found on my own site: knowledgemarkg.com runs on Next.js. In November I replaced the header with a mega menu: Services, Free Tools and Blog, each with a two-column dropdown. The label is a link to the section page, and a chevron button opens the panel. It works well for people.

我在自己网站上发现的情况:knowledgemarkg.com 运行在 Next.js 上。11 月,我将页眉替换为一个大型菜单:服务、免费工具和博客,每个都有一个双列下拉菜单。标签是通往该部分页面的链接,而一个 V 形按钮用于打开面板。这对用户来说效果很好。

While checking why a few service pages were slow to get indexed, I fetched the live home page with curl and counted the links to /services/. There were 4, all from the footer and the page body. The header’s dropdown links to the nine service pages weren’t in the HTML at all, because the component rendered the panel like this:

在检查为什么一些服务页面索引缓慢时,我用 curl 获取了实时主页,并统计了指向 /services/ 的链接。结果只有 4 个,全部来自页脚和页面主体。页眉下拉菜单中指向九个服务页面的链接根本不在 HTML 中,因为组件是这样渲染面板的:

// The pattern most hand-rolled menus use
export function MegaMenu({ menu }) {
  const [open, setOpen] = useState(false);
  return (
    <div onMouseEnter={() => setOpen(true)} onMouseLeave={() => setOpen(false)}>
      <Link href={menu.href}>{menu.label}</Link>
      <button aria-expanded={open} onClick={() => setOpen(!open)}>▾</button>
      {open ? <Panel menu={menu} /> : null} {/* closed = nothing in the HTML */}
    </div>
  );
}

当面板关闭时,React 渲染为 null。这就是服务器渲染的状态,因此也是 Google 获得的 HTML。

Check your own menu in 30 seconds: Don’t use the browser’s Elements panel for this. By the time you look, you’ve probably hovered the menu, and DevTools shows the live DOM, not what the server sent. Use View Source (Ctrl+U), or curl:

在 30 秒内检查你自己的菜单:不要使用浏览器的“元素”面板进行检查。当你查看时,你可能已经悬停在菜单上了,DevTools 显示的是实时 DOM,而不是服务器发送的内容。请使用“查看源代码”(Ctrl+U) 或 curl:

# What the server actually sends (no JavaScript run, no clicks)
curl -s -A "Mozilla/5.0" https://example.com/ | grep -o 'href="/services/[^"]*"' | sort -u
# Count them
curl -s -A "Mozilla/5.0" https://example.com/ | grep -o 'href="/services/[^"]*"' | sort -u | wc -l

Swap /services/ for any path your menu links to. If a page in your dropdown doesn’t show up, it isn’t in the server HTML.

将 /services/ 替换为你菜单链接到的任何路径。如果下拉菜单中的某个页面没有显示,说明它不在服务器的 HTML 中。

Two more checks are useful: Search Console → URL Inspection → View crawled page → HTML. That’s the HTML after Google rendered it. If the links aren’t there either, Google didn’t see them. Compare with the sitemap. A page that is in your sitemap but linked from nowhere visible to a crawler often ends up in “Discovered - currently not indexed”.

另外两个有用的检查:Search Console → URL 检查 → 查看抓取的页面 → HTML。这是 Google 渲染后的 HTML。如果链接在那里也不存在,说明 Google 没有看到它们。与站点地图进行比较。一个出现在站点地图中,但没有被爬虫可见的任何地方链接到的页面,通常最终会处于“已发现 - 当前未编入索引”状态。

Why Google doesn’t see links in a closed menu: Two parts of Google’s documentation explain it: Google’s crawlable-links guide says it can generally crawl a link only if it’s an <a> element with an href. Links inserted with JavaScript count too, as long as they end up as that same markup. Its lazy-loading guide says plainly that Google Search does not interact with your page: no scrolling, no clicking. Hover is not mentioned at all.

为什么 Google 看不到关闭菜单中的链接:Google 文档的两个部分解释了这一点:Google 的可抓取链接指南指出,它通常只有在链接是带有 href 的 <a> 元素时才能抓取。只要最终呈现为相同的标记,通过 JavaScript 插入的链接也算数。其延迟加载指南明确表示,Google 搜索不会与你的页面交互:没有滚动,没有点击。根本没有提到悬停。

So rendering isn’t the issue. Google does run your JavaScript. The issue is that nothing ever sets open to true during rendering. Content that waits for a user action never exists for the crawler. This is different from content that’s on the page but hidden with CSS, like a collapsed accordion. Those links are in the HTML. The question for an accordion is how much weight hidden text gets. For a conditionally rendered menu, there’s nothing there to weigh.

所以渲染不是问题所在。Google 确实会运行你的 JavaScript。问题在于,在渲染过程中没有任何东西将 open 设置为 true。等待用户操作的内容对于爬虫来说永远不存在。这与页面上存在但通过 CSS 隐藏的内容(如折叠的手风琴菜单)不同。那些链接在 HTML 中是存在的。对于手风琴菜单,问题在于隐藏文本能获得多少权重。而对于条件渲染的菜单,根本没有任何东西可以衡量。

I tested the popular React menu components: To see whether this is just my hand-rolled menu, I rendered each component on the server with React’s renderToString, closed, with the same two <a href="/services/..."> links inside, and counted the links in the output.

我测试了流行的 React 菜单组件:为了看看这是否只是我手写的菜单的问题,我使用 React 的 renderToString 在服务器上渲染了每个组件(处于关闭状态),并在其中放入了相同的两个 <a href="/services/..."> 链接,然后统计了输出中的链接数量。

Component (closed)Links in server HTML
Radix NavigationMenu0 of 2
Radix NavigationMenu, Content forceMount2 of 2
Radix DropdownMenu0 of 2
Headless UI Menu0 of 2
Headless UI Popover0 of 2
Headless UI Popover, unmount={false}2 of 2
Headless UI Disclosure0 of 2
Hand-rolled {open &&
    }
0 of 2
Native <details><summary>2 of 2
Panel always rendered, hidden with CSS2 of 2

shadcn/ui’s Navigation Menu and Dropdown Menu are built on these Radix components, so I’d expect the same result, but I tested the Radix packages, not the shadcn copies. Server-rendering the closed state like this is a fair stand-in for the first HTML a crawler gets, but it isn’t Googlebot itself. Use the URL Inspection check above to confirm what Google saw on your site. None of this is a bug in those libraries. Unmounting closed popups is a sensible default for dropdowns full of actions like “Delete” or “Sign out”. It just isn’t what you want for site navigation.

shadcn/ui 的导航菜单和下拉菜单是基于这些 Radix 组件构建的,所以我预计结果是一样的,但我测试的是 Radix 包,而不是 shadcn 的副本。像这样在服务器端渲染关闭状态是爬虫获取的初始 HTML 的一个很好的替代品,但它毕竟不是 Googlebot 本身。请使用上面的 URL 检查来确认 Google 在你的网站上看到了什么。这些都不是这些库的错误。对于充满“删除”或“退出”等操作的下拉菜单,卸载关闭的弹出窗口是一个合理的默认设置。但这并不是你进行网站导航时所需要的。

Four ways to fix it: 1. Keep the panel mounted (library…

修复它的四种方法:1. 保持面板挂载(库…