Hydration hint false positive: `class=""` appears on dynamic components with an undefined class

未关闭 适合新手
#409 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@lafllamme 已经在做这个了。

开始于 2026年11月12日。

  • #412 来自 @lafllamme —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
76/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
nuxt, typescript
领域
frontend, testing

调研方向

先阅读 normalizeHTMLForComparison 的实现,以及针对 issue #351 和 #352 的现有回归测试;issue 指明该规范化步骤是修改点。复现动态组件中 class 未定义的情况,并比较 hydration 前后的 HTML。完成标准是:空的 class 和 style 属性在比较前被规范化,且此情况不再产生误报。

由索引模型根据 Issue 内容生成。

描述

Environment
  • nuxt 4.6.2 (SSR, dev mode)
  • @nuxt/hints 1.1.5
  • vue 3.5.44
  • @nuxt/ui 4.12.0 (where I first hit it; not needed to reproduce)
  • Node.js 26.13.0, Windows 11
Reproduction

A component on any SSR page, components/EmptyClass.vue:

<script setup lang="ts">
const tag = ref('div')
const noClass = undefined
</script>

<template>
  <component :is="tag" :class="noClass">
    content
  </component>
</template>

The same happens with Nuxt UI's <UForm> without a class prop: its root is a <component :is> and its computed class is undefined.

Describe the bug

@nuxt/hints warns about a hydration difference whose only content is an empty class attribute:

- <div>
+ <div class="">

Vue itself reports no hydration mismatch.

Controls I tried:

  • the same with :class="''": the server also renders class="", no warning;
  • a plain <div :class="noClass">: neither side has the attribute, no warning.

Why it happens:

  • SSR renders an undefined class as no attribute.
  • On the client, the compiled render wraps the binding in normalizeClass(), which turns undefined into ''.
  • Because the element is a dynamic component, class is among the vnode's dynamicProps, and hydrateElement patches every dynamic prop during hydration.
  • patchClass(el, '') then sets el.className = '', which creates class="".
  • Vue's own propHasMismatch treats a missing class as "", so it sees no mismatch.
  • useHydrationCheck compares raw outerHTML and only normalizes style.

Expected: no warning, since class="" and no class attribute are equivalent (and Vue agrees).

Suggestion: extend the normalization from #352 and drop empty class="" (and style="") attributes in normalizeHTMLForComparison before comparing.

Additional context

Same class of false positive as #351 (fixed in #352 for style serialization), and a concrete case of "make attribute comparison more precise" in #257.

Logs
[hydration] Component EmptyClass at .../components/EmptyClass.vue seems to have different html pre and post-hydration. Please make sure you don't have any hydration issue.
主要语言
TypeScript
星标
347
派生
15
平均合并
7 分钟
30 天内合并 PR
1

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

nuxt/hints 的其他 Issue

查看 nuxt/hints 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。