question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hydration node mismatch when overwriting prose components

See original GitHub issue

Environment


  • Operating System: Linux
  • Node Version: v16.14.2
  • Nuxt Version: 3.0.0-rc.10
  • Nitro Version: 0.5.3
  • Package Manager: npm@7.17.0
  • Builder: vite
  • User Config: -
  • Runtime Modules: -
  • Build Modules: -

Reproduction

https://stackblitz.com/edit/nuxt-starter-dmjv7l?file=components/content/ProseImg.vue

Describe the bug

Updated: my previous report was incorrect and I have isolated the issue to my prose component overwriting. See edit history for my old report if you need.

It looks like the hydration node mismatch happens if I overwrite the img prose component (I haven’t tested other type yet). See the linked reproduction above.

It looks like if I add any extra element to the new ProseImg component, it will result as a hydration node mismatch error:

[Vue warn]: Hydration children mismatch in <div>: server rendered element contains more child nodes than client vdom. 
  at <ContentRendererMarkdown value= {_path: '/', _draft: false, _partial: false, _locale: 'en', _empty: false, …} excerpt=false tag="div" > 
  at <ContentRenderer key="content:index.md" value= {_path: '/', _draft: false, _partial: false, _locale: 'en', _empty: false, …} > 
  at <Anonymous > 
  at <Anonymous > 
  at <Anonymous name="default" > 
  at <DocumentDriven onVnodeUnmounted=fn<onVnodeUnmounted> ref=Ref< undefined > > 
  at <Anonymous key="/" routeProps= {Component: {…}, route: {…}} pageKey="/"  ... > 
  at <BaseTransition mode="out-in" appear=false persisted=false  ... > 
  at <Transition name="page" mode="out-in" > 
  at <RouterView name=undefined route=undefined > 
  at <NuxtPage> 
  at <App key=1 > 
  at <NuxtRoot>

To be more specific, having the <div> tag will trigger this error:

// ProseImg.vue
<template>
  <div>
    <img :src="src" :alt="alt" :width="width" :height="height" />
  </div>
</template>

Maybe the SSR code didn’t see the overwritten component?

Thank you.

Additional context

No response

Logs

No response

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
fanzeyicommented, Sep 21, 2022

@michaelvcolianna My current work around is to add <client-only> tag at the root of my customized ProseImg component, but it will also disable SSR for images though.

0reactions
michaelvcoliannacommented, Sep 22, 2022

@fanzeyi I’m not a fan of this but it works:

/components/content/ProseP.vue

<script setup>
const slots = useSlots()

// Hacky check for an image
const isImage = computed(() => {
  return slots.default()[0].type.tag === 'img'
})
</script>

<template>
  <div class="prose-image" v-if="isImage"><slot /></div>
  <p v-else><slot /></p>
</template>
Read more comments on GitHub >

github_iconTop Results From Across the Web

Hydration mismatch error due to plugins generating script ...
If a script tag is inserted before the head tag due to the user's browser environment such as a plugin, it is judged...
Read more >
Debugging and fixing hydration issues - somewhat abstract
Spotting hydration errors and fixing them can prevent serious user-facing performance issues, and since we do not get a signal during production ...
Read more >
node.js - Hydration error in next.js - unmatched text, ...
I have tried modifying getStaticPaths and getStaticProps but this issue still persists.
Read more >
Should I ignore these kind of warnings? "Hydration node ...
I am using this package "FormKit" for handling forms and validation. it shows this warning for every element I use and I can't...
Read more >
Understand and solve hydration errors in Vue.js
If a component is initially mounted or hydrated in the DOM, it needs to precisely correspond to the HTML generated on the server...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found