DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question
  1. Home
  2. Articles
  3. Frontend Engineering
  4. What is keep-alive in Vue.js and When to Use It
XLinkedInReddit
MediumFrontend Engineering

What is keep-alive in Vue.js and When to Use It

D
DevPrep Team
February 9, 2026·2 min read·0
Table of Contents
  • The Problem
  • Basic Usage
  • With Vue Router
  • Controlling What Gets Cached
  • Lifecycle Hooks
  • Production Issues
  • Summary

By Rahul — Google Frontend Engineer

The Problem

When you switch between Vue components (tabs, routes), the component is destroyed and recreated. All state is lost — form inputs reset, scroll position gone, API data re-fetched. <keep-alive> caches the component instance instead of destroying it.

Basic Usage

<!-- Without keep-alive: component destroyed on switch -->
<component :is="currentTab" />

<!-- With keep-alive: component cached -->
<keep-alive>
  <component :is="currentTab" />
</keep-alive>

With Vue Router

<!-- Vue 3 -->
<router-view v-slot="{ Component }">
  <keep-alive>
    <component :is="Component" />
  </keep-alive>
</router-view>

Controlling What Gets Cached

<!-- Only cache specific components -->
<keep-alive include="SearchPage,ProductList">
  <component :is="currentView" />
</keep-alive>

<!-- Exclude components from cache -->
<keep-alive exclude="CheckoutForm">
  <component :is="currentView" />
</keep-alive>

<!-- Limit cache size (LRU eviction) -->
<keep-alive :max="10">
  <component :is="currentView" />
</keep-alive>

Lifecycle Hooks

// When component is activated (switched to)
activated() {
  // Refresh data if stale
  if (Date.now() - this.lastFetched > 60000) {
    this.fetchData();
  }
}

// When component is deactivated (switched away)
deactivated() {
  // Pause expensive operations
  clearInterval(this.pollingInterval);
}

Production Issues

  • Memory leaks: Caching too many components eats memory. Always set :max
  • Stale data: Cached components show old data. Use activated hook to refresh
  • Event listeners: Listeners set in mounted() are not cleaned up on deactivate. Clean up in deactivated(), set up in activated()

Summary

keep-alive caches component instances to preserve state between switches. Use include/exclude to control caching, set :max to limit memory, and use activated/deactivated hooks to manage data freshness.

Related Articles

MediumFrontend Engineering

System Design #12: Design a Multi-Step Form Wizard

7 min read
MediumFrontend Engineering

Mastering Senior-Level JavaScript Interview Concepts

2 min read
MediumFrontend Engineering

System Design #9: Design a Collaborative Text Editor

9 min read

Comments (0)

Sign in to leave a comment.

No comments yet. Be the first to comment.

Table of Contents

  • The Problem
  • Basic Usage
  • With Vue Router
  • Controlling What Gets Cached
  • Lifecycle Hooks
  • Production Issues
  • Summary

Series

View all Frontend Engineering articles →

Practice

  • JavaScript
  • DSA
  • Machine Coding
  • System Design

Resources

  • Learning Tracks
  • Articles
  • Roadmaps
  • Compare Concepts
  • Glossary
  • Developer Tools
  • All Questions

Company

  • About
  • Pricing

Legal

  • Privacy Policy
  • Terms of Service
DevPrep

© 2026 DevPrep. All rights reserved.