Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Enforces Vue 3 Composition API best practices with script setup, TypeScript, Pinia, and Vite.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/component-keep-alive.md
1---2title: KeepAlive Component Best Practices3impact: HIGH4impactDescription: KeepAlive caches component instances; misuse causes stale data, memory growth, or unexpected lifecycle behavior5type: best-practice6tags: [vue3, keepalive, cache, performance, router, dynamic-components]7---89# KeepAlive Component Best Practices1011**Impact: HIGH** - `<KeepAlive>` caches component instances instead of destroying them. Use it to preserve state across switches, but manage cache size and freshness explicitly to avoid memory growth or stale UI.1213## Task List1415- Use KeepAlive only where state preservation improves UX16- Set a reasonable `max` to cap cache size17- Declare component names for include/exclude matching18- Use `onActivated`/`onDeactivated` for cache-aware logic19- Decide how and when cached views refresh their data20- Avoid caching memory-heavy or security-sensitive views2122## When to Use KeepAlive2324Use KeepAlive when switching between views where state should persist (tabs, multi-step forms, dashboards). Avoid it when each visit should start fresh.2526**BAD:**27```vue28<template>29<!-- State resets on every switch -->30<component :is="currentTab" />31</template>32```3334**GOOD:**35```vue36<template>37<!-- State preserved between switches -->38<KeepAlive>39<component :is="currentTab" />40</KeepAlive>41</template>42```4344## When NOT to Use KeepAlive4546- Search or filter pages where users expect fresh results47- Memory-heavy components (maps, large tables, media players)48- Sensitive flows where data must be cleared on exit49- Components with heavy background activity you cannot pause5051## Limit and Control the Cache5253Always cap cache size with `max` and restrict caching to specific components when possible.5455```vue56<template>57<KeepAlive :max="5" include="Dashboard,Settings">58<component :is="currentView" />59</KeepAlive>60</template>61```6263## Ensure Component Names Match include/exclude6465`include` and `exclude` match the component `name` option. Explicitly set names for reliable caching.6667```vue68<!-- TabA.vue -->69<script setup>70defineOptions({ name: 'TabA' })71</script>72```7374```vue75<template>76<KeepAlive include="TabA,TabB">77<component :is="currentTab" />78</KeepAlive>79</template>80```8182## Cache Invalidation Strategies8384Vue 3 has no direct API to remove a specific cached instance. Use keys or dynamic include/exclude to force refreshes.8586```vue87<script setup>88import { ref, reactive } from 'vue'8990const currentView = ref('Dashboard')91const viewKeys = reactive({ Dashboard: 0, Settings: 0 })9293function invalidateCache(view) {94viewKeys[view]++95}96</script>9798<template>99<KeepAlive>100<component :is="currentView" :key="`${currentView}-${viewKeys[currentView]}`" />101</KeepAlive>102</template>103```104105## Lifecycle Hooks for Cached Components106107Cached components are not destroyed on switch. Use activation hooks for refresh and cleanup.108109```vue110<script setup>111import { onActivated, onDeactivated } from 'vue'112113onActivated(() => {114refreshData()115})116117onDeactivated(() => {118pauseTimers()119})120</script>121```122123## Router Caching and Freshness124125Decide whether navigation should show cached state or a fresh view. A common pattern is to key by route when params change.126127```vue128<template>129<router-view v-slot="{ Component, route }">130<KeepAlive>131<component :is="Component" :key="route.fullPath" />132</KeepAlive>133</router-view>134</template>135```136137If you want cache reuse but fresh data, refresh in `onActivated` and compare query/params before fetching.138