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?

5 Replies
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:
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:
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:
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:
----------------------<@302102970768818187> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
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?
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:
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:
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:
Or configure specific routes to be client-side rendered in your nuxt.config.ts
:
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:
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:
----------------------<@302102970768818187> Kapa.ai is still learning and improving, please let me know how I did by reacting below.