All skills
hyf0 avatar

/vue-debug-guides

@a5fc891 official
by hyf0hyf0/vue-skills2.9k stars
167

Vue 3 debugging and error handling for runtime errors, warnings, async failures, and SSR/hydration issues. Use when diagnosing or fixing Vue issues.

Use this Skill: https://skilld.dev/gh/hyf0/vue-skills/vue-debug-guides

This session only. Nothing lands on disk.

referencekeepalive-router-nested-double-mount.md

≈1.3k tokens on demand. Your agent reads this file only when SKILL.md points to it.

KeepAlive with Nested Routes Double Mount Issue

Impact: HIGH - When using <KeepAlive> with nested Vue Router routes, child route components may mount twice. This is a known issue that can cause duplicate API calls, broken state, and confusing behavior.

Task Checklist

  • Test nested routes thoroughly when using KeepAlive
  • Avoid mixing KeepAlive with deeply nested route structures
  • Use workarounds if double mount is observed
  • Consider alternative caching strategies for nested routes

The Problem

<!-- App.vue -->
<template>
  <router-view v-slot="{ Component }">
    <KeepAlive>
      <component :is="Component" />
    </KeepAlive>
  </router-view>
</template>
// router.js
const routes = [
  {
    path: '/parent',
    component: Parent,
    children: [
      {
        path: 'child',
        component: Child  // This may mount TWICE!
      }
    ]
  }
]

Symptoms:

  • onMounted called twice in child component
  • Duplicate API requests
  • State initialization runs twice
  • Console logs appear doubled

Diagnosis

Add logging to confirm the issue:

<!-- Child.vue -->
<script setup>
import { onMounted, onActivated } from 'vue'

let mountCount = 0

onMounted(() => {
  mountCount++
  console.log('Child mounted - count:', mountCount)
  // If you see "count: 2", you have the double mount issue
})

onActivated(() => {
  console.log('Child activated')
})
</script>

Workarounds

Option 1: Use useActivatedRoute Pattern

Don't use useRoute() directly with KeepAlive:

<script setup>
import { ref, onActivated } from 'vue'
import { useRoute } from 'vue-router'

// Problem: useRoute() can cause issues with KeepAlive
// const route = useRoute()

// Solution: Get route info in onActivated
const routeParams = ref({})

onActivated(() => {
  const route = useRoute()
  routeParams.value = { ...route.params }
})
</script>

Option 2: Avoid KeepAlive for Nested Route Parents

Only cache leaf routes, not parent layouts:

<script setup>
import { computed } from 'vue'
import { useRoute } from 'vue-router'

const route = useRoute()

// Only cache specific leaf routes
const cachedRoutes = computed(() => {
  // Don't cache parent routes that have children
  return ['UserProfile', 'UserSettings'] // Only leaf components
})
</script>

<template>
  <router-view v-slot="{ Component, route: currentRoute }">
    <KeepAlive :include="cachedRoutes">
      <component :is="Component" :key="currentRoute.fullPath" />
    </KeepAlive>
  </router-view>
</template>

Option 3: Guard Against Double Initialization

Protect your component from double mount effects:

<script setup>
import { ref, onMounted } from 'vue'

const isInitialized = ref(false)

onMounted(() => {
  if (isInitialized.value) {
    console.warn('Double mount detected, skipping initialization')
    return
  }
  isInitialized.value = true

  // Safe to initialize
  fetchData()
  setupEventListeners()
})
</script>

Option 4: Use Route-Level Cache Control

<!-- App.vue -->
<script setup>
import { computed } from 'vue'
import { useRoute } from 'vue-router'

const route = useRoute()

// Define which routes should be cached in route meta
const shouldCache = computed(() => {
  return route.meta.keepAlive !== false
})
</script>

<template>
  <router-view v-slot="{ Component }">
    <KeepAlive v-if="shouldCache">
      <component :is="Component" />
    </KeepAlive>
    <component v-else :is="Component" />
  </router-view>
</template>
// router.js
const routes = [
  {
    path: '/parent',
    component: Parent,
    meta: { keepAlive: false }, // Don't cache parent routes
    children: [
      {
        path: 'child',
        component: Child,
        meta: { keepAlive: true } // Cache leaf routes
      }
    ]
  }
]

Option 5: Flatten Route Structure

Avoid nesting if possible:

// Instead of nested routes
const routes = [
  // Flat structure avoids the issue
  { path: '/users', component: UserList },
  { path: '/users/:id', component: UserDetail },
  { path: '/users/:id/settings', component: UserSettings }
]

Key Points

  1. Known Vue Router issue - Double mount with KeepAlive + nested routes
  2. Watch for symptoms - Duplicate API calls, doubled logs
  3. Avoid caching parent routes - Only cache leaf components
  4. Add initialization guards - Protect against double execution
  5. Test thoroughly - This issue may not appear immediately

Reference

Source: SKILL.md on GitHub

No alerts17d5 checks · Risk SAFE
  • Gen Agent Trust Hub17d

    This skill is a comprehensive collection of educational debugging guides and best practices for Vue 3 developers. It contains no executable code or malicious instructions and focuses solely on diagnosing common runtime errors and performance issues.

  • Socket17d

    No alerts

  • Snyk17d

    Risk: LOW · No issues

  • Runlayer7mo

    140 files scanned · No issues

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at a5fc891. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 months ago.

Steadyupdated 8 months ago

README badge

README badge for hyf0/vue-skills/vue-debug-guides

Guides for diagnosing and fixing Vue 3 runtime errors, reactivity bugs, async failures, and hydration issues across components, templates, watchers, forms, and lifecycle hooks. Covers specific failure modes like stale watchers, ref unwrapping edge cases, v-model sync problems, and SSR mismatches with concrete reference links for each issue.

Generated from the current SKILL.md.

Does this skill cover Vue 2?
No. This skill is specific to Vue 3 debugging and error handling.
What should I use for Vue development best practices instead of debugging?
The skill references a separate `vue-best-practices` skill for development best practices and common gotchas.
Does this cover SSR and hydration issues?
Yes. The skill includes guides for diagnosing and fixing SSR rendering differences and hydration bugs.
Can this help with async watcher and watchEffect problems?
Yes. The skill covers async operation stale data, dependency tracking after await, and proper flush timing for watchers.
Does this address v-model and form-related issues?
Yes. The skill includes guides for v-model initial values, textarea interpolation, IME composition, and custom checkbox form submission.

Generated from the current SKILL.md. These answers refresh after source changes.