core: v-html on `` does not work in SSR

Vue version

3.5.13

Link to minimal reproduction

https://play.vuejs.org/#__PROD____SSR__eNp9kcFPgzAUxv+V+i67TIhBLwSXqFniPGzGcWxiEJ7QWVrSFiRZ+N9tC8MdliU9vHy/r33fez3CU9MEXYsQQ2KwbnhmcEUFIUnBOl9MZVoxTezpmGZfHJPwjOeybqRAYUjM9COFhWULCqS7rUzNnZBoo6QoV+nrZk/s2e7SmyScRGcNx6bTq0k4Z4ElGJ1L8c3K4KClsEGPzkrBdWUc1a4xTApNISaeOJZxLn/fvGZUi8uTnleY/1zQD7p3GoV3hRpVhxRmZjJVohnxer/F3tYzrGXRcuu+Aj9QS966jKPtuRWFjX3m82k3dofKMFGmet0bFPo0lAvqnIP3U7C/9XJl9P+4UXDv71Ex2C1+dqjcm3aBUfAQ3EUw/AFR/qMz

Steps to reproduce

<template>
  <div>
    <div>This is visible</div>
    <component :is="'div'" v-html="'<strong>THIS IS NOT!</strong>'" />
  </div>
</template>

What is expected?

The <component> should render in both SSR + client-only

What is actually happening?

THIS IS NOT is not rendered in SSR.

System Info

No response

Any additional comments?

reported in https://github.com/nuxt/nuxt/issues/30202

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 3
  • Comments: 24 (6 by maintainers)

Most upvoted comments

@Gwynerva here it is

this should be considered a bug because it works on CSR. simpler reproduction

if the tag is not a p, it will work as expected.see

Nested <p> tags are automatically “corrected” by the browser. This is because <p> elements cannot contain other block-level elements (including other <p> tags). image

Tried to use render function h during setup to conditionally create VNode. It works without SSR, and partially works in SSR but creates hydration mismatches — on server side it creates an empty comment <!---->. This time not even creating correct wrapping tag, as it was in the example in the first post here.

<script setup>
import { h } from 'vue';

const isBlock = true;
const MyNode = h(isBlock ? 'div' : 'span', { innerHTML: '<strong>Hello World!</strong>' });
</script>

<template>
  <MyNode />
</template>

I am not sure but I think this might be the same bug.

UPDATE:

Turns out when using setup syntax h is not working on server side at all:

<script setup>
import { h } from 'vue';
const MyNode = h('div', { innerHTML: '<strong>Hello World!</strong>' });
</script>

<template>
  <MyNode />
</template>

This is considered an edge case. IMO, a VNode cannot be rendered directly, at least, it should be <component :is="VNode"/> see, and rendering a VNode directly will no reactivity, such as in this case.

To sum up, there are two bugs, both working just fine in normal Vue and not working in Vue SSR:

  1. h is not working in SSR when using <script setup> syntax causing hydration errors. There is a fix from @linzhe141
  2. <component> with dynamic :is and v-html not working in SSR, silently skipping the entire tag body. There is a fix from @noootwo

It was pointed out that second bug needs discussion, but the first one I think obviously is an SSR bug.

What is even worse, the first bug actually prevents a simple possible workaround to avoid using v-html on <component> in the use case I metioned above and therefore, avoid meeting second bug.

@danielroe I think it would be better to rename issue to something like “Dynamic tag generation fails in SSR” to address both problems in one issue.

@yyx990803 Would be really cool to see both or at least the first one to be fixed. Because right now the only way is to duplicate tags directly in <template>, writing all attributes twice, or use sub-components without setup syntax.

As the use case doesn’t involve custom components, I think it’s legit for dynamic element tags.

@Ghosterbeef turns out it’s a <p> problem, not a Vue bug!

Tried to use render function h during setup to conditionally create VNode. It works without SSR, and partially works in SSR but creates hydration mismatches — on server side it creates an empty comment <!---->. This time not even creating correct wrapping tag, as it was in the example in the first post here.

<script setup>
import { h } from 'vue';

const isBlock = true;
const MyNode = h(isBlock ? 'div' : 'span', { innerHTML: '<strong>Hello World!</strong>' });
</script>

<template>
  <MyNode />
</template>

I am not sure but I think this might be the same bug.

UPDATE:

Turns out when using setup syntax h is not working on server side at all:

<script setup>
import { h } from 'vue';
const MyNode = h('div', { innerHTML: '<strong>Hello World!</strong>' });
</script>

<template>
  <MyNode />
</template>

I think using v-html or v-text on a component(or dynamic component) is somewhat of an anti-pattern, not sure if it will be supported, Let’s wait for Evan’s input.

@Gwynerva Here is a workaround may be better.