vue icon indicating copy to clipboard operation
vue copied to clipboard

Hydration mismatch at {{ "" }} inside v-for

Open Fabioni opened this issue 2 years ago • 5 comments

Version

2.6.14

Reproduction link

codesandbox.io

Steps to reproduce

  • nuxt: v2.15.18
  • node: v16.14.0
<span v-for="(obj, index) in objs" :key="obj.id">{{ "" }}<span>Test</span></span>

Important for me was it in this usecase

<span v-for="(obj, index) in objs" :key="obj.id">{{ index > 0 ? ", " : "" }}<wbr v-if="index > 0"><span>{{obj.text}}</span></span>

I don't know that much about vue without nuxt, so I can't say how to reproduce sss, just came here by this comment https://github.com/nuxt/nuxt.js/issues/10390#issuecomment-1100752569

I used nuxt dev with target: 'static' in nuxt.config.

What is expected?

normal hydration

What is actually happening?

I get a hydration mismatch because nuxt on ssr somehow thinks there is a node with empty content and undefined type and on clientside there correctly no node.


Workaround

use instead:

<span v-for="(obj, index) in objs" :key="obj.id"><span>{{ "" }}</span><span>Test</span></span>

resp.

<span v-for="(obj, index) in objs" :key="obj.id"><span v-if="index > 0">, </span><wbr v-if="index > 0"><span>Test</span></span>

First posted issue here: https://github.com/nuxt/nuxt.js/issues/10390

Fabioni avatar Apr 17 '22 01:04 Fabioni

<span v-for="(obj, index) in objs" :key="obj.id">{{ index > 0 ", " : "" }}<wbr v-if="index > 0"><span>{{obj.text}}</span></span>

{{ index > 0 ", " : "" }} This sentence is missing a question mark, it should be {{ index > 0 ? ", " : "" }}

clyan avatar Apr 18 '22 10:04 clyan

<span v-for="(obj, index) in objs" :key="obj.id">{{ index > 0 ", " : "" }}<wbr v-if="index > 0"><span>{{obj.text}}</span></span>

{{ index > 0 ", " : "" }} This sentence is missing a question mark, it should be {{ index > 0 ? ", " : "" }}

Obviously just a typo in my question :D I corrected it

Fabioni avatar Apr 18 '22 10:04 Fabioni

In your codesandbox link, you use the id as the key... and it's the same for every entry. The key as to be unique. Try using :key="obj.text", or change the value of the id. Not sure if this solve your problem, it seems to work properly with the correct key....

emavitta avatar Apr 27 '22 14:04 emavitta

@Fabioni Proper SSR Reproduction link: https://stackblitz.com/edit/vue-12523-ctf5n4?file=app.js

You may toogle the bool at line 15 to switch between workaround(restart the server after toggle)

I think it is fixed in Vue 3

John-Weak avatar Jun 14 '22 18:06 John-Weak

@Fabioni Proper SSR Reproduction link: https://stackblitz.com/edit/vue-12523-ctf5n4?file=app.js

You may toogle the bool at line 15 to switch between workaround(restart the server after toggle)

I think it is fixed in Vue 3

If you restart the hyrdation error is not present,but if do not restart the error is present.

John-Weak avatar Jun 15 '22 04:06 John-Weak