To suppress the hydration mismatch warning in Vue SSR, you can try the following approaches:
- Manually Remove DOM Content:
Before hydration, manually remove the DOM content of the element where Vue will be mounted.const rootId = 'app' const root = document.getElementById(vueRootId) root.innerHTML = '' // Your hydration code // ...
See Remove all child elements of a DOM node in JavaScript for alternative ways to remove the childs of a DOM element.
- Set the Warning Option to False:
You can disable the warning message by setting the warning option to false in the Vue component or globally in the Vue.config object. - Set the Environment Variable:
You can also suppress the warning by setting the environment variableVUE_WARNING_DISABLE
to true. - Use a Client-Only Component:
Create a<ClientOnly>
component that conditionally renders its content only on the client side, preventing the warning from occurring. - Use a Plugin:
Install thevue-cli-plugin-suppress-warnings
plugin to suppress the warning globally in your Vue project.
<template> <div> <p v-warning="false">This will not generate a warning</p> </div> </template> Vue.config.warning = false
export VUE_WARNING_DISABLE = true
import { ref, onMounted, defineComponent } from 'vue' const ClientOnly = defineComponent({ setup(_, { slots }) { const show = ref(false) onMounted(() => { show.value = true }) return () => (show.value && slots.default ? slots.default() : null) } }) export { ClientOnly, }
The specific approach that works best for you will depend on the context and requirements of your application.