# Suspense SSR Hydration Issues and Workarounds ## Rule `` has known issues with SSR hydration, particularly with async components. During initial hydration, Suspense may not properly include child components within its "cloak of suspense," leading to hydration mismatches, flickering, or runtime crashes. ## Why This Matters In SSR applications, hydration mismatches cause: - Visual flickering as the client re-renders - Loss of state in affected components - Console warnings in development (silent failures in production) - Potential runtime crashes in edge cases - Poor user experience, especially on slower networks ## Bad Code ```vue ``` ## Good Code ### Solution 1: Wrap Async Components with Suspense ```vue ``` ### Solution 2: Use ClientOnly Wrapper (Nuxt/SSR Frameworks) ```vue ``` ### Solution 3: Prefetch with Proper Stale Time (with TanStack Query) ```vue ``` ### Solution 4: Handle Hydration Errors Gracefully ```vue ``` ## Common SSR + Suspense Issues | Issue | Cause | Solution | |-------|-------|----------| | Hydration mismatch | Async chunk not loaded in time | Wrap with Suspense or use ClientOnly | | Empty flash on Safari | Slow chunk loading | Preload critical chunks, use skeleton | | useQuery after await error | Vue context lost after await | Put all useQuery calls before any await | | Immediate refetch after hydration | staleTime too low | Set appropriate staleTime value | ## Key Points 1. Suspense + SSR has known edge cases - test thoroughly 2. Safari has slower chunk loading that triggers more hydration issues 3. With data-fetching libraries, ensure queries are set up before awaiting suspense 4. Consider ClientOnly wrappers for non-critical async content 5. Set appropriate staleTime to prevent unnecessary refetches after hydration 6. Use skeleton screens that match server-rendered content structure ## References - [Vue.js Suspense Documentation](https://vuejs.org/guide/built-ins/suspense) - [Vue Issue #6638 - Suspense hydration](https://github.com/vuejs/core/issues/6638) - [Vue Issue #7672 - defineAsyncComponent SSR](https://github.com/vuejs/core/issues/7672) - [TanStack Query SSR Discussion](https://github.com/TanStack/query/discussions/4870)