home / skills / eva813 / vue3-skills / vue3-best-practices

vue3-best-practices skill

/.agents/skills/vue3-best-practices

This skill helps you apply Vue 3 performance best practices and Composition API patterns across components and state management.

npx playbooks add skill eva813/vue3-skills --skill vue3-best-practices

Review the files below or copy the command above to add this skill to your agents.

Files (8)
SKILL.md
6.4 KB
---
name: vue3-best-practices
description: Vue 3 performance optimization and best practices guidelines for modern frontend applications. This skill should be used when writing, reviewing, or refactoring Vue 3 code to ensure optimal performance patterns, proper Composition API usage, and modern development practices. Triggers on tasks involving Vue 3 components, Composition API, reactivity, state management, or performance optimization.
license: MIT
metadata:
  author: Eva
  version: "1.0.0"
---

# Vue 3 Best Practices

Comprehensive performance optimization and development guide for Vue 3 applications. Contains 45 rules across 8 categories, prioritized by impact to guide automated refactoring and code generation.

## When to Apply

Reference these guidelines when:
- Writing new Vue 3 components or composables
- Implementing reactive data and computed properties
- Reviewing code for performance issues
- Refactoring from Vue 2 to Vue 3
- Optimizing bundle size or load times
- Working with state management (Pinia/Vuex)
- Implementing async operations in components

## Rule Categories by Priority

| Priority | Category | Impact | Prefix |
|----------|----------|--------|--------|
| 1 | Reactivity Performance | CRITICAL | `reactivity-` |
| 2 | Component Optimization | CRITICAL | `component-` |
| 3 | Bundle Size & Loading | HIGH | `bundle-` |
| 4 | Composition API | MEDIUM-HIGH | `composition-` |
| 5 | Template Performance | MEDIUM | `template-` |
| 6 | State Management | MEDIUM | `state-` |
| 7 | Lifecycle Optimization | LOW-MEDIUM | `lifecycle-` |
| 8 | Advanced Patterns | LOW | `advanced-` |

## Quick Reference

### 1. Reactivity Performance (CRITICAL)

- `reactivity-ref-vs-reactive` - Use ref for primitives, reactive for objects
- `reactivity-shallow-ref` - Use shallowRef for large immutable objects
- `reactivity-computed-caching` - Leverage computed property caching
- `reactivity-watch-vs-watcheffect` - Choose appropriate watcher
- `reactivity-unref-performance` - Minimize unref calls in hot paths
- `reactivity-readonly-immutable` - Use readonly for immutable data

### 2. Component Optimization (CRITICAL)

- `component-async-components` - Use defineAsyncComponent for heavy components
- `component-functional` - Use functional components for simple presentational logic
- `component-keep-alive` - Cache expensive components with keep-alive
- `component-lazy-hydration` - Implement lazy hydration for non-critical components
- `component-prop-validation` - Use efficient prop validation
- `component-emit-performance` - Optimize event emissions

### 3. Bundle Size & Loading (HIGH)

- `bundle-tree-shaking` - Structure imports for optimal tree-shaking
- `bundle-dynamic-imports` - Use dynamic imports for code splitting
- `bundle-plugin-imports` - Use unplugin-auto-import for better DX
- `bundle-lodash-imports` - Import lodash functions individually
- `bundle-moment-alternatives` - Use day.js instead of moment.js
- `bundle-icons-optimization` - Optimize icon imports and usage

### 4. Composition API (MEDIUM-HIGH)

- `composition-script-setup` - Prefer <script setup> for better performance
- `composition-composables-reuse` - Extract reusable logic into composables
- `composition-provide-inject` - Use provide/inject for dependency injection
- `composition-expose-selectively` - Expose only necessary properties
- `composition-reactive-transform` - Use reactive transform where appropriate
- `composition-auto-import` - Configure auto-imports for better DX

### 5. Template Performance (MEDIUM)

- `template-v-once` - Use v-once for static content
- `template-v-memo` - Use v-memo for expensive list rendering
- `template-key-optimization` - Optimize v-for keys for performance
- `template-conditional-rendering` - Choose v-if vs v-show appropriately
- `template-slot-performance` - Optimize slot usage and scoped slots
- `template-directive-optimization` - Create efficient custom directives

### 6. State Management (MEDIUM)

- `state-pinia-optimization` - Optimize Pinia store structure
- `state-store-composition` - Use store composition patterns
- `state-persistence` - Implement efficient state persistence
- `state-normalization` - Normalize complex state structures
- `state-subscription` - Optimize state subscriptions
- `state-devtools` - Configure devtools for debugging

### 7. Lifecycle Optimization (LOW-MEDIUM)

- `lifecycle-cleanup` - Properly cleanup resources in onUnmounted
- `lifecycle-async-setup` - Handle async operations in setup
- `lifecycle-watchers-cleanup` - Clean up watchers and effects
- `lifecycle-dom-access` - Access DOM safely in lifecycle hooks
- `lifecycle-ssr-considerations` - Handle SSR lifecycle differences

### 8. Advanced Patterns (LOW)

- `advanced-teleport-usage` - Use Teleport for portal patterns
- `advanced-suspense-async` - Implement Suspense with async components
- `advanced-custom-renderer` - Create custom renderers when needed
- `advanced-compiler-macros` - Use compiler macros effectively
- `advanced-plugin-development` - Develop efficient Vue plugins

## Framework Integration

### Vite Integration
- Utilize Vite's fast HMR and build optimizations
- Configure proper chunk splitting strategies
- Use Vite plugins for Vue-specific optimizations

### TypeScript Integration
- Leverage Vue 3's improved TypeScript support
- Use proper type definitions for better DX
- Configure TypeScript for optimal build performance

### Testing Integration
- Use Vue Test Utils with Composition API
- Implement efficient component testing strategies
- Optimize test performance and reliability

## How to Use

Read individual rule files for detailed explanations and code examples:

```
rules/reactivity-ref-vs-reactive.md
rules/component-async-components.md
rules/composition-script-setup.md
```

Each rule file contains:
- Brief explanation of why it matters
- Incorrect Vue 3 code example with explanation
- Correct Vue 3 code example with explanation
- Performance impact and measurements
- Additional context and Vue 3-specific considerations

## Migration from Vue 2

Special considerations for migrating from Vue 2:
- Composition API vs Options API patterns
- Reactivity system changes and optimizations
- Component definition and registration updates
- Event handling and lifecycle changes

## Performance Monitoring

Tools and techniques for monitoring Vue 3 performance:
- Vue DevTools integration
- Performance profiling with browser tools
- Bundle analysis and optimization
- Runtime performance monitoring

Overview

This skill provides Vue 3 performance optimization and best-practices guidance for modern frontend applications. It prioritizes high-impact rules across reactivity, component design, bundle strategies, and Composition API usage. Use it to guide writing, reviewing, or refactoring Vue 3 code toward efficient, maintainable patterns.

How this skill works

The skill inspects tasks involving Vue 3 components, Composition API, reactivity, state management, and performance hotspots. It maps common code patterns to prioritized rules (e.g., ref vs reactive, async component loading, code-splitting) and recommends concrete changes to reduce runtime work and bundle size. Recommendations focus on measurable impact and practical code changes you can apply immediately.

When to use it

  • Creating new Vue 3 components or composables
  • Refactoring Vue 2 code to Vue 3 and Composition API
  • Reviewing PRs for performance regressions or anti-patterns
  • Optimizing bundle size, load time, or hydration behavior
  • Working with Pinia/Vuex stores or complex reactive state

Best practices

  • Prefer ref for primitives and reactive for objects; use shallowRef for large immutable values
  • Lazy-load heavy components with defineAsyncComponent and split routes with dynamic imports
  • Favor <script setup> and extract reusable logic into composables to reduce boilerplate
  • Use computed properties for cached derivations and minimize unref/unwrapping in hot paths
  • Optimize templates: v-memo/v-once for static parts, correct v-for keys, and prefer v-show vs v-if strategically

Example use cases

  • Refactor a large dashboard: convert expensive child components to async and cache with keep-alive
  • Improve form performance: replace deep reactive patterns with refs and targeted watchers
  • Trim bundle size: replace moment.js with day.js and import lodash functions individually
  • Migrate Vue 2 options API modules to Composition API using typed composables and auto-imports
  • Optimize state stores: normalize complex structures in Pinia and reduce component subscriptions

FAQ

Should I always use ref over reactive?

Use ref for primitives and simple values; use reactive for nested objects. Choose shallowRef for large immutable values to avoid deep reactivity overhead.

When is defineAsyncComponent preferable?

Use it for heavy or rarely used components to defer loading and improve initial render times, especially when paired with route-based code splitting.