huge hydration mismatch, elements are being rendered in the wrong divs

Hey, On first render it seems elements are getting swapped from the header div to the body div, like 1 level down, also some divs get duplicated.
<template>
<FontsLoader/>
<ResourceBar v-if="auth.loggedIn"/>
<div class="header">
<span class="gameTitle">TotalControl - Strategy Enhanced</span>

<div class="headerControls">
<!-- <ActionMenu/>-->
</div>
</div>
<div class="bodyWrapper">
<div class="body">
<AuthComponent v-if="!displayBody"/>
<slot v-else/>
</div>
</div>
<div class="footer">

</div>
</template>
<template>
<FontsLoader/>
<ResourceBar v-if="auth.loggedIn"/>
<div class="header">
<span class="gameTitle">TotalControl - Strategy Enhanced</span>

<div class="headerControls">
<!-- <ActionMenu/>-->
</div>
</div>
<div class="bodyWrapper">
<div class="body">
<AuthComponent v-if="!displayBody"/>
<slot v-else/>
</div>
</div>
<div class="footer">

</div>
</template>
No description
6 Replies
kapa.ai
kapa.ai3mo ago
Hi @khamas (ping me). I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Hydration mismatches in Nuxt—where elements appear in the wrong places, get duplicated, or are swapped between containers—are almost always caused by inconsistencies between what is rendered on the server and what is rendered on the client during hydration. This can happen for several reasons: 1. State Differences Between Server and Client:
If your template or component logic depends on data that is only available or changes on the client (for example, authentication state, browser-only APIs, or data fetched only on the client), the server and client will render different DOM structures, causing hydration errors. For example, if auth.loggedIn is determined only on the client, the server might render <ResourceBar/> as absent, but the client adds it, shifting all subsequent elements down by one level. 2. Direct Mutation of Props or Shared Data:
Mutating props or shared data in child components can cause the data to be different between server and client, leading to mismatches. Always clone props before mutating them in child components. See this explanation for more details. 3. Conditional Rendering and Asynchronous Data:
Using v-if or other conditional rendering based on data that is not guaranteed to be the same on both server and client can cause elements to appear or disappear, shifting the DOM tree. For example, if displayBody is computed differently on server and client, <AuthComponent/> and the slot content may swap places or duplicate. 4. Client-Only Features:
If you have components or logic that should only run on the client (e.g., those depending on browser APIs or authentication), wrap them in <client-only> to prevent SSR from rendering them and causing mismatches. How to Fix: - Ensure that any data used for conditional rendering (like auth.loggedIn or displayBody) is available and consistent on both server and client. If this is not possible, use <client-only> for those components:
<template>
<FontsLoader/>
<client-only>
<ResourceBar v-if="auth.loggedIn"/>
</client-only>
<!-- ...rest of your template -->
</template>

<template>
<FontsLoader/>
<client-only>
<ResourceBar v-if="auth.loggedIn"/>
</client-only>
<!-- ...rest of your template -->
</template>

- Avoid mutating props or shared state directly in child components. Always work with a local copy if you need to modify data. - For API requests or async data, use SSR-friendly composables like useAsyncData or useFetch so that the data is fetched on the server and reused on the client, preventing double-fetching and mismatches. See Nuxt Lifecycle: Hydration. - If you have logic that must only run on the client or server, use import.meta.client or import.meta.server to split the code paths. Summary:
Hydration mismatches with elements being swapped or duplicated are almost always due to differences in the rendered output between server and client. Audit your conditional rendering and data sources to ensure consistency, and use <client-only> for client-specific components or logic. For more details, see the Nuxt documentation on hydration and SSR data and common causes of hydration mismatches. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3mo ago
<@237189429159002112> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
khamasito 🇵🇸
khamasito 🇵🇸OP3mo ago
it seems client-only fixes it
danielroe
danielroe3mo ago
probably display Body returns different results on server and client it should return the same value at the time of hydration or that is by definition a mismatch
khamasito 🇵🇸
khamasito 🇵🇸OP3mo ago
yeah not sure what to do about this it's a client only layout so that's fine but i'd love to know why this fails
danielroe
danielroe3mo ago
impossible to say without seeing your code, but it's almost certainly an issue in your code

Did you find this page helpful?