Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect vite warning regarding nesting tr directly in table element #12088

Open
tomtheisen opened this issue Sep 30, 2024 · 9 comments
Open

Comments

@tomtheisen
Copy link

tomtheisen commented Sep 30, 2024

Vue version

3.5.10

Link to minimal reproduction

https://play.vuejs.org/#eNp9kDELwjAQhf9KuVnqoJMUQcVBBxV1zFLrWatpEpKLFqT/3SSlrYN0CS/vexfe5QMLpeKXRZhBQlgqnhLOmYiihNILb6S/6Hkydkcg4xY51Y/ACMhkUtyKPH4YKdyLHx9nkMlSFRz1XlEhhWEwiwLxLOVcvrfBI21x1PrZHbPnH/9hKu8xOGg0qF/IoGOU6hypwevTDiunO1jKq+UuPQCPaCS3vmMTW1pxdbV/cqHtplRSUyHys1lXhMK0S/miPlmHPAP3rauB1fu6k3ga5pioof4CfnKCSA==

Steps to reproduce

Create a SFC with this content.

<template>
  <table>
    <tr></tr>
  </table>
</template>

Vite will issue a warning:

warning: <tr> cannot be child of <table>, according to HTML specifications. This can cause hydration errors or potentially disrupt future functionality.

This warning is factually incorrect. The relevant spec says that a <table> may contain:

In this order: optionally a caption element, followed by zero or more colgroup elements, followed optionally by a thead element, followed by either zero or more tbody elements or one or more tr elements, followed optionally by a tfoot element, optionally intermixed with one or more script-supporting elements.

This is because htmlNesting.ts does not list tr as a valid child of table, despite the spec allowing it.

What is expected?

I expect no warning to be produced.

What is actually happening?

A warning is produced from vite:

warning: <tr> cannot be child of <table>, according to HTML specifications. This can cause hydration errors or potentially disrupt future functionality.

System Info

@KazariEX
Copy link
Contributor

KazariEX commented Oct 1, 2024

const table = document.createElement('table');
table.innerHTML = '<tr></tr>';
console.log(table.innerHTML);

@edison1105
Copy link
Member

edison1105 commented Oct 1, 2024

https://html.spec.whatwg.org/multipage/tables.html#the-tr-element
Maybe we should modify the warning message.

@Justineo
Copy link
Member

Justineo commented Oct 1, 2024

I’d suggest making the check more flexible, as it’s not very comprehensive at the moment.

@tomtheisen
Copy link
Author

const table = document.createElement('table');
table.innerHTML = '<tr></tr>';
console.log(table.innerHTML);

I don't think this is showing what you think it is showing.

This is a serialized DOM. In HTML, <tbody> is explicitly optional, by specification, as I've cited above.

@KazariEX
Copy link
Contributor

KazariEX commented Oct 1, 2024

I don't think this is showing what you think it is showing.我不认为这显示了你认为它所显示的内容。

This is a serialized DOM. In HTML, <tbody> is explicitly optional, by specification, as I've cited above.这是一个序列化的 DOM。在 HTML 中, <tbody>根据规范是明确可选的,正如我上面引用的那样。

The directly generated <table><tr></tr></table> during SSR becomes <table><tbody><tr></tr></tbody></table> during CSR, as mentioned in the warning, resulting in hydration mismatch. This is what I want it to showing.

@tomtheisen
Copy link
Author

At a minimum the warning shouldn't claim that this is due to a spec. It seems this is constraint introduced by the implementation details of hydration. It could say something like this.

Despite being valid HTML, Vue SSR does not support direct nesting of <tr> within a <table>. Doing this can cause discrepancies during hydration.

Personally, I'm not using SSR at all, so then I would know it's perfectly safe to ignore this warning.

@KazariEX
Copy link
Contributor

KazariEX commented Oct 1, 2024

Yes, the warning should be corrected. Perhaps we can try adding some transformations to similar behaviors so that <tbody> can be automatically generated.

@Kennedy242
Copy link

I see the same thing. Fix the warning or provide a work around?

@aelgn
Copy link

aelgn commented Oct 15, 2024

I'm seeing my stdout absolutely brim-filled with this warning and its accompanying template print out in both dev and build environments, to a point that it swallows and obscures any legitimate warnings.
I do not use SSR so I'm not impacted by the discrepancy between the hydration feature implementation and the html specification.

Could this warning be predicated by SSR actually being used? Or could we disable this warning in any way? (except writing a custom vite logger to filter it out)
If not I think this warning should be removed. Can't really expect everyone to change all table based components to an optional html syntax or lose functionality of the console.

I see that this issue is labeled as an edge-case, my vote is that it is not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants