Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Vue 3 debugging reference for reactivity issues, computed errors, watcher bugs, async failures, and SSR hydration problems.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
reference/async-component-error-handling.md
1# Async Component Error Handling23## Rule45Always configure error handling for async components using `errorComponent` and/or `onError` callback. Without proper error handling, failed component loads can leave the UI in an undefined state with no user feedback.67## Why This Matters89Network failures, timeouts, and server errors are common in production. Without error handling, users see blank spaces or broken UIs with no indication of what went wrong or how to recover.1011## Bad Code1213```vue14<script setup>15import { defineAsyncComponent } from 'vue'1617// No error handling - fails silently18const AsyncWidget = defineAsyncComponent(() =>19import('./Widget.vue')20)21</script>22```2324```vue25<script setup>26import { defineAsyncComponent } from 'vue'2728// isLoading never becomes false on error - infinite spinner29const isLoading = ref(true)30const Widget = defineAsyncComponent({31loader: () => import('./Widget.vue').finally(() => {32isLoading.value = false // Only runs on success33})34})35</script>36```3738## Good Code3940```vue41<script setup>42import { defineAsyncComponent } from 'vue'43import LoadingSpinner from './LoadingSpinner.vue'44import ErrorDisplay from './ErrorDisplay.vue'4546const AsyncWidget = defineAsyncComponent({47loader: () => import('./Widget.vue'),48loadingComponent: LoadingSpinner,49errorComponent: ErrorDisplay,50delay: 200, // Prevent loading flicker51timeout: 10000 // Show error after 10 seconds52})53</script>54```5556```vue57<script setup>58import { defineAsyncComponent } from 'vue'5960// With retry logic using onError61const AsyncWidget = defineAsyncComponent({62loader: () => import('./Widget.vue'),63loadingComponent: LoadingSpinner,64errorComponent: ErrorDisplay,65onError(error, retry, fail, attempts) {66if (attempts <= 3) {67// Retry up to 3 times68retry()69} else {70// Give up and show error component71fail()72}73}74})75</script>76```7778```vue79<script setup>80import { defineAsyncComponent } from 'vue'8182// Fallback component pattern - catch in loader83const AsyncWidget = defineAsyncComponent(() =>84import('./Widget.vue').catch(() => import('./WidgetFallback.vue'))85)86</script>87```8889## onError Callback Parameters9091The `onError` callback receives four arguments:9293| Parameter | Type | Description |94|-----------|------|-------------|95| `error` | `Error` | The error that caused the load to fail |96| `retry` | `Function` | Call to retry loading the component |97| `fail` | `Function` | Call to give up and show errorComponent |98| `attempts` | `number` | Number of load attempts so far |99100## Key Points1011021. Always provide an `errorComponent` for production applications1032. Use `timeout` to prevent indefinite loading states1043. Consider retry logic with `onError` for transient network issues1054. The `delay` option (default 200ms) prevents loading flicker on fast networks1065. Use the fallback pattern (`.catch()` in loader) when you want a seamless degradation107108## SSR Warning109110Using `onError` with SSR can cause issues in some configurations, potentially leading to infinite loading. Test thoroughly in SSR environments.111112## References113114- [Vue.js Async Components Documentation](https://vuejs.org/guide/components/async)115- [Handling Async Components' loading errors](https://awad.dev/blog/handling-async-component-loading-errors/)116