|
| 1 | +--- |
| 2 | +name: vue-best-practices |
| 3 | +description: Vue.js performance optimization guidelines for building fast, maintainable applications. This skill should be used when writing, reviewing, or refactoring Vue.js code to ensure optimal performance patterns. Triggers on tasks involving Vue components, reactivity, Composition API, state management, or performance improvements. |
| 4 | +license: MIT |
| 5 | +metadata: |
| 6 | + author: vinayakkulkarni |
| 7 | + version: "1.0.0" |
| 8 | +--- |
| 9 | + |
| 10 | +# Vue Best Practices |
| 11 | + |
| 12 | +Comprehensive performance optimization guide for Vue.js applications. Contains 40+ rules across 8 categories, prioritized by impact to guide automated refactoring and code generation. |
| 13 | + |
| 14 | +## When to Apply |
| 15 | + |
| 16 | +Reference these guidelines when: |
| 17 | +- Writing new Vue components |
| 18 | +- Implementing reactive state and computed properties |
| 19 | +- Reviewing code for performance issues |
| 20 | +- Refactoring existing Vue code |
| 21 | +- Optimizing rendering and re-renders |
| 22 | +- Working with Composition API or Options API |
| 23 | + |
| 24 | +## Rule Categories by Priority |
| 25 | + |
| 26 | +| Priority | Category | Impact | Prefix | |
| 27 | +|----------|----------|--------|--------| |
| 28 | +| 1 | Reactivity Fundamentals | CRITICAL | `reactivity-` | |
| 29 | +| 2 | Component Performance | CRITICAL | `component-` | |
| 30 | +| 3 | Computed & Watchers | HIGH | `computed-` | |
| 31 | +| 4 | Template Optimization | MEDIUM-HIGH | `template-` | |
| 32 | +| 5 | Composition API Patterns | MEDIUM | `composable-` | |
| 33 | +| 6 | State Management | MEDIUM | `state-` | |
| 34 | +| 7 | Async & Data Fetching | LOW-MEDIUM | `async-` | |
| 35 | +| 8 | Advanced Patterns | LOW | `advanced-` | |
| 36 | + |
| 37 | +## Quick Reference |
| 38 | + |
| 39 | +### 1. Reactivity Fundamentals (CRITICAL) |
| 40 | + |
| 41 | +- `reactivity-ref-vs-reactive` - Use ref() for primitives, reactive() for objects |
| 42 | +- `reactivity-avoid-destructure` - Don't destructure reactive objects |
| 43 | +- `reactivity-toRefs` - Use toRefs() when destructuring is needed |
| 44 | +- `reactivity-shallowRef` - Use shallowRef() for large non-reactive data |
| 45 | +- `reactivity-raw-values` - Use toRaw() for read-only operations on large data |
| 46 | + |
| 47 | +### 2. Component Performance (CRITICAL) |
| 48 | + |
| 49 | +- `component-v-once` - Use v-once for static content |
| 50 | +- `component-v-memo` - Use v-memo for expensive list items |
| 51 | +- `component-async` - Use defineAsyncComponent for heavy components |
| 52 | +- `component-keep-alive` - Cache component state with KeepAlive |
| 53 | +- `component-functional` - Prefer functional components for stateless UI |
| 54 | + |
| 55 | +### 3. Computed & Watchers (HIGH) |
| 56 | + |
| 57 | +- `computed-cache` - Use computed() for derived values, not methods |
| 58 | +- `computed-getter-only` - Avoid setters in computed when possible |
| 59 | +- `computed-dependencies` - Minimize computed dependencies |
| 60 | +- `watch-immediate` - Avoid immediate watchers, use computed instead |
| 61 | +- `watch-deep-avoid` - Avoid deep watchers on large objects |
| 62 | +- `watch-cleanup` - Always cleanup async watchers |
| 63 | + |
| 64 | +### 4. Template Optimization (MEDIUM-HIGH) |
| 65 | + |
| 66 | +- `template-v-show-vs-if` - v-show for frequent toggles, v-if for rare |
| 67 | +- `template-key-attribute` - Always use unique keys in v-for |
| 68 | +- `template-avoid-v-if-v-for` - Never use v-if and v-for on same element |
| 69 | +- `template-static-hoisting` - Let compiler hoist static content |
| 70 | +- `template-event-modifiers` - Use event modifiers instead of JS handlers |
| 71 | + |
| 72 | +### 5. Composition API Patterns (MEDIUM) |
| 73 | + |
| 74 | +- `composable-single-responsibility` - One concern per composable |
| 75 | +- `composable-return-refs` - Return refs, not reactive objects |
| 76 | +- `composable-cleanup` - Handle cleanup in composables |
| 77 | +- `composable-lazy-init` - Lazy initialize expensive resources |
| 78 | +- `composable-provide-inject` - Use provide/inject for deep prop drilling |
| 79 | + |
| 80 | +### 6. State Management (MEDIUM) |
| 81 | + |
| 82 | +- `state-pinia-stores` - Split stores by domain |
| 83 | +- `state-getters` - Use getters for computed state |
| 84 | +- `state-actions-mutations` - Keep mutations simple, logic in actions |
| 85 | +- `state-subscription-cleanup` - Cleanup store subscriptions |
| 86 | + |
| 87 | +### 7. Async & Data Fetching (LOW-MEDIUM) |
| 88 | + |
| 89 | +- `async-suspense` - Use Suspense for async component loading |
| 90 | +- `async-error-boundaries` - Handle async errors gracefully |
| 91 | +- `async-stale-while-revalidate` - Implement SWR pattern for data fetching |
| 92 | +- `async-abort-controller` - Cancel pending requests on unmount |
| 93 | + |
| 94 | +### 8. Advanced Patterns (LOW) |
| 95 | + |
| 96 | +- `advanced-custom-directives` - Create directives for DOM manipulation |
| 97 | +- `advanced-render-functions` - Use render functions for dynamic templates |
| 98 | +- `advanced-teleport` - Use Teleport for modals and overlays |
| 99 | +- `advanced-transition-groups` - Optimize list transitions |
| 100 | + |
| 101 | +## How to Use |
| 102 | + |
| 103 | +Read individual rule files for detailed explanations and code examples: |
| 104 | + |
| 105 | +``` |
| 106 | +rules/reactivity-ref-vs-reactive.md |
| 107 | +rules/component-v-memo.md |
| 108 | +rules/_sections.md |
| 109 | +``` |
| 110 | + |
| 111 | +Each rule file contains: |
| 112 | +- Brief explanation of why it matters |
| 113 | +- Incorrect code example with explanation |
| 114 | +- Correct code example with explanation |
| 115 | +- Additional context and references |
| 116 | + |
| 117 | +## Full Compiled Document |
| 118 | + |
| 119 | +For the complete guide with all rules expanded: `AGENTS.md` |
0 commit comments