Performance: Suspense for async components
This affects page load speed and interaction responsiveness by managing how async components load and render.
<template>
<Suspense>
<template #default>
<AsyncComponent />
</template>
<template #fallback>
<div>Loading...</div>
</template>
</Suspense>
</template>
<script setup>
import AsyncComponent from './AsyncComponent.vue'
</script><template> <AsyncComponent /> </template> <script setup> import AsyncComponent from './AsyncComponent.vue' </script>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Async component without Suspense | Minimal initial DOM | Single reflow after load | Large paint delay | [X] Bad |
| Async component with Suspense fallback | More DOM nodes for fallback | Single reflow for fallback + reflow for component | Smaller initial paint delay | [OK] Good |
<Suspense> component in Vue?<Suspense> with a fallback in Vue?#fallback inside <Suspense> to show fallback content.<Suspense><template #fallback>Loading...</template><AsyncComp /></Suspense> correctly uses <template #fallback> with fallback text and wraps the async component inside Suspense.<Suspense><template #fallback>Loading...</template><AsyncComp /></Suspense> [OK]<Suspense> <template #fallback>Loading...</template> <AsyncUser /> </Suspense>
AsyncUser is loading?<Suspense> <AsyncComp /> <template fallback>Loading...</template> </Suspense>
#fallback inside <template>.<template fallback> instead of <template #fallback>, which is invalid.#fallback slot syntax [OK]<Suspense><template #fallback><Spinner /></template><AsyncComp1 /><AsyncComp2 /></Suspense> [OK]Pass the test and this pattern moves unknown