core: v-html on `` does not work in SSR
Vue version
3.5.13
Link to minimal reproduction
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)
this should be considered a bug because it works on CSR. simpler reproductionif the
tagis not ap, it will work as expected.seeNested
<p>tags are automatically “corrected” by the browser. This is because<p>elements cannot contain other block-level elements (including other<p>tags).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:
his not working in SSR when using<script setup>syntax causing hydration errors. There is a fix from @linzhe141<component>with dynamic:isandv-htmlnot working in SSR, silently skipping the entire tag body. There is a fix from @noootwoIt 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-htmlon<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 withoutsetupsyntax.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!@Gwynerva here it is
Tried to use render function
hduring setup to conditionally createVNode. 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.I am not sure but I think this might be the same bug.
UPDATE:
Turns out when using setup syntax
his not working on server side at all: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.