SSR Compatibility
Server-side rendering compatibility, ClientOnly component, and handling browser-only code
The Rule
<script setup>
import { onMounted, ref } from 'vue'
const windowWidth = ref(0)
onMounted(() => {
// Safe - runs only in browser
windowWidth.value = window.innerWidth
})
</script><script setup>
// WRONG - runs during SSR where window doesn't exist
const width = window.innerWidth
</script>ClientOnly Component
Libraries That Access Browser on Import
Dynamic Import in onMounted
Conditional Import
In enhanceApp
defineClientComponent
Teleports
Common SSR Errors
"window is not defined"
"document is not defined"
Hydration Mismatch
Checking Environment
Key Points
Last updated
Was this helpful?