Input resetting after hydration is done

Hi, this might be a stupid question as I am not super familiar with Nuxt yet, but if I have the following setup (see image). If I would input anything in the textbox before the hydration is finished, it will just clear the whole textbox. I kind of expected the hydration to keep the value of the input in mind instead of resetting it. Am I doing something wrong here?
No description
5 Replies
kapa.ai
kapa.ai7d ago
Hi @Patrickdemooij9. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- This is a common issue with hydration in Nuxt applications. When you type in an input field before hydration is complete, the value gets reset once hydration finishes. This happens because during hydration, Vue recreates the client-side DOM to match what was rendered on the server. Why this happens During the hydration process, Vue compares the client-side DOM with what was rendered on the server. If you've typed something into an input field before hydration completes, there will be a mismatch between: - What the server rendered (empty input) - What exists in the browser (your typed text) When hydration completes, Vue will reconcile this difference by resetting the input to match the server-rendered state. Solutions Here are a few approaches to handle this: 1. Check if hydration is complete before allowing input You can use useNuxtApp().isHydrating to determine if the app is still in the hydration phase:
<template>
<div>
<UInput
v-model="value"
:disabled="nuxtApp.isHydrating"
placeholder="Type something..."
/>
<div v-if="nuxtApp.isHydrating">Loading...</div>
</div>
</template>

<script setup>
const nuxtApp = useNuxtApp()
const value = ref('')
</script>
<template>
<div>
<UInput
v-model="value"
:disabled="nuxtApp.isHydrating"
placeholder="Type something..."
/>
<div v-if="nuxtApp.isHydrating">Loading...</div>
</div>
</template>

<script setup>
const nuxtApp = useNuxtApp()
const value = ref('')
</script>
This will disable the input until hydration is complete, preventing users from typing before the app is ready Docs > Api > Composables > useNuxtApp > Properties. 2. Listen for hydration events For components that support it, you can listen for the @hydrated event:
<template>
<div>
<LazyMyComponent hydrate-on-visible @hydrated="onHydrate" />
</div>
</template>

<script setup lang="ts">
function onHydrate() {
console.log("Component has been hydrated!")
// Now it's safe to interact with inputs
}
</script>
<template>
<div>
<LazyMyComponent hydrate-on-visible @hydrated="onHydrate" />
</div>
</template>

<script setup lang="ts">
function onHydrate() {
console.log("Component has been hydrated!")
// Now it's safe to interact with inputs
}
</script>
This works with components using delayed hydration Docs > Guide > Directory-structure > components > Listening to Hydration Events. 3. Be careful with default values If you're using form libraries like VeeValidate with Zod, be cautious about how you set default values. Using dynamic values like Date.now() can cause hydration mismatches. Instead, initialize with static values and update them after hydration:
// Don't do this
const myVar = ref(Date.now());

// Do this instead
const myVar = ref(0);
onMounted(() => {
myVar.value = Date.now();
});
// Don't do this
const myVar = ref(Date.now());

// Do this instead
const myVar = ref(0);
onMounted(() => {
myVar.value = Date.now();
});
This approach was mentioned in a Stack Overflow solution for hydration mismatches Stack Overflow Post > Hydration completed but contains mismatches using VeeValidate and Pinia in Nuxt Part 5. Best practices 1. Avoid complex state initialization before hydration completes 2. Consider using useHydration composable for controlling the hydration cycle if needed 3. For critical forms, consider client-side only rendering if the SEO impact is acceptable Remember that hydration is a fundamental part of how Nuxt works with SSR, ensuring that the client-side Vue app properly takes over the server-rendered HTML. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai7d ago
<@302102970768818187> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Patrickdemooij9
Patrickdemooij9OP7d ago
That doesn't sound like great UX waiting for the hydration to finish before letting people interact with the UI @kapa.ai Is there any way to keep the input and reapply it after hydration?
kapa.ai
kapa.ai7d ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- Yes, there are a few ways to preserve input values during hydration in Nuxt. Here are some approaches: 1. Use onPrehydrate Hook Nuxt provides an onPrehydrate lifecycle hook that runs immediately before hydration. You can use this to capture input values:
<script setup>
const inputValue = ref('')
const tempValue = ref('')

onPrehydrate((el) => {
// Find and store input values before hydration
const input = el.querySelector('input')
if (input && input.value) {
// Store the value to be applied after hydration
window.__tempInputValue = input.value
}
})

onMounted(() => {
// Restore the value after hydration
if (window.__tempInputValue) {
inputValue.value = window.__tempInputValue
delete window.__tempInputValue
}
})
</script>

<template>
<input v-model="inputValue" />
</template>
<script setup>
const inputValue = ref('')
const tempValue = ref('')

onPrehydrate((el) => {
// Find and store input values before hydration
const input = el.querySelector('input')
if (input && input.value) {
// Store the value to be applied after hydration
window.__tempInputValue = input.value
}
})

onMounted(() => {
// Restore the value after hydration
if (window.__tempInputValue) {
inputValue.value = window.__tempInputValue
delete window.__tempInputValue
}
})
</script>

<template>
<input v-model="inputValue" />
</template>
This approach uses the onPrehydrate hook which runs before Nuxt hydrates the page GitHub PR. 2. Use Client-Side Storage You can use cookies to persist the input value across hydration:
<script setup>
// Use a cookie to persist the value through hydration
const inputValue = useCookie('input-value', {
default: () => '',
watch: true
})
</script>

<template>
<input v-model="inputValue" />
</template>
<script setup>
// Use a cookie to persist the value through hydration
const inputValue = useCookie('input-value', {
default: () => '',
watch: true
})
</script>

<template>
<input v-model="inputValue" />
</template>
This approach uses useCookie which is hydration-safe and works well for persisting state Stack Overflow Post > Persist State Between Reloads in Nuxt v3. 3. Client-Side Only Rendering For forms where SEO isn't critical, you can make the component client-side only:
<template>
<ClientOnly>
<input v-model="inputValue" />
</ClientOnly>
</template>
<template>
<ClientOnly>
<input v-model="inputValue" />
</ClientOnly>
</template>
Or configure specific routes to be client-side rendered in your nuxt.config.ts:
export default defineNuxtConfig({
routeRules: {
'/your-form-page': {
ssr: false
}
}
})
export default defineNuxtConfig({
routeRules: {
'/your-form-page': {
ssr: false
}
}
})
This approach avoids hydration issues entirely by rendering only on the client Stack Overflow Post > Handling simple hydration mismatch after fetch in Nuxt 3 Part 1. 4. Use Form Composables For more complex forms, consider using Nuxt UI's form composables which are designed to handle hydration properly:
<script setup>
const { inputId, emitFormBlur, emitFormInput, emitFormChange } = useFormField()
</script>
<script setup>
const { inputId, emitFormBlur, emitFormInput, emitFormChange } = useFormField()
</script>
This provides better integration with Nuxt's hydration cycle UI Nuxt Composables. The best approach depends on your specific use case, but using cookies or the onPrehydrate hook are generally the most reliable methods for preserving input values through hydration. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai7d ago
<@302102970768818187> Kapa.ai is still learning and improving, please let me know how I did by reacting below.

Did you find this page helpful?