The useHydration
composable provides SSR hydration state management, allowing you to detect when your application has been hydrated on the client side. This is essential for preventing hydration mismatches and controlling when browser-only features should activate.
First, install the hydration plugin in your application:
import { createApp } from 'vue'
import { createHydrationPlugin } from '@vuetify/v0'
import App from './App.vue'
const app = createApp(App)
app.use(createHydrationPlugin())
app.mount('#app')
Once the plugin is installed, use the useHydration
composable in any component:
<script setup lang="ts">
import { useHydration } from '@vuetify/v0'
const hydration = useHydration()
</script>
<template>
<div>
<p v-if="hydration.isHydrated.value">
Application is hydrated - browser features available
</p>
<p v-else>
Rendering on server or waiting for hydration
</p>
</div>
</template>
Composable | Description |
---|---|
useStorage→ | Storage system |
createPlugin→ | Plugin creation pattern |
The useHydration()
composable returns a context with the following properties and methods:
interface HydrationContext {
isHydrated: Readonly<ShallowRef<boolean>>
hydrate: () => void
}
isHydrated
: Reactive boolean indicating whether the application has been hydratedhydrate()
: Manually trigger hydration (typically called automatically by the plugin)The hydration plugin automatically detects when the root component has mounted and marks the application as hydrated:
isHydrated
is false
hydrate()
isHydrated
becomes true
, signaling that browser APIs are safe to useThe plugin uses a Vue mixin to detect the root component mount:
app.mixin({
mounted() {
if (this.$parent !== null) return // Skip child components
context.hydrate() // Only hydrate on root mount
}
})