Mastering keep-alive in Vue: Optimize Your Component Performance

Mayank Patel

  1. Jul 09, 2025
  2. 4 min read

Imagine a dashboard where users apply filters, search for specific items, scroll through long lists, create custom views, and enter data into forms. Naturally, you want all of these interactions - the selections, inputs, and scroll positions - to be preserved exactly as the user left them, even if they navigate away and return later.

But how do you achieve this kind of seamless experience without rewriting large parts of your component or overhauling your app's structure?

The answer is simple: <keep-alive>.

What is <keep-alive>

<keep-alive> is a built-in Vue component that allows you to cache other components so that they don't get destroyed and recreated every time they're toggled or navigated away from.

Instead of reloading a component from scratch, Vue will keep it alive in memory, preserving its state, data, scroll position, and DOM structure.

When you wrap a dynamic component or <router-view> in <keep-alive>, Vue:

  • Mounts the component only once.
  • Keeps it in memory when it is no longer visible.
  • Restores it instantly the next time it's needed.

When to Use <keep-alive>

- You're toggling between multiple views or tabs.
- A component is expensive to re-render.
- You want to keep user input or internal state when the view is hidden.

Basic Usage


<template>
  <keep-alive>
    <component :is="viewComponent" />
  </keep-alive>
</template>

<script>
export default {
  data() {
    return {
      viewComponent: 'HomeView'
    }
  }
}
</script>
Using with Vue Router
<template>
  <keep-alive>
    <router-view v-if="$route.meta.keepAlive" />
  </keep-alive>
  <router-view v-else />
</template>
In router setup:

{
  path: '/dashboard',
  component: DashboardView,
  meta: { keepAlive: true }
}

include and exclude

Vue’s <keep-alive> lets you selectively cache components using the include and exclude props. These props control which components get kept alive and which ones don't.

The include prop tells Vue: Only cache these specific components.


<keep-alive :include="HomeView,SettingsView">
  <component :is="viewComponent" />
</keep-alive>

The exclude prop tells Vue: Cache everything except these components.


<keep-alive :exclude="HomeView,SettingsView">
  <component :is="viewComponent" />
</keep-alive>

max

The max prop in Vue’s <keep-alive> component controls how many components Vue should keep in memory at once.

Think of it like a cache size limit—Vue will only store the specified number of components, and it will evict the least recently used component when the limit is exceeded.


<keep-alive :max="4">
  <component :is="viewComponent" />
</keep-alive>

In this example:

Only 4 components will be kept alive.

When the 5th is added, Vue will destroy the least recently used one.

Lifecycle Hooks

Special hooks for cached components:
activated() - called when the component is re-activated.
deactivated() - called when the component is hidden but kept in memory.

Example:


export default {
  activated() {
    console.log('Activated');
  },
  deactivated() {
    console.log('Deactivated');
  }
}

What to Avoid

- Don’t cache components that must fetch fresh data every time and if you must cache them then make sure you are using the activated hook to pull data everytime the component is in use.
- Avoid excessive memory usage by caching too many components.
- Be careful when using it with large or complex dynamic views.

Final Thoughts

<keep-alive> is a simple yet powerful way to make your Vue applications more performant. Use it wisely to keep your components alive when necessary, preserve state, and deliver a better user experience without complex hacks.

Happy Caching!

About Author
Mayank Patel

See What Our Clients Say

Mindgap

Incentius has been a fantastic partner for us. Their strong expertise in technology helped deliver some complex solutions for our customers within challenging timelines. Specific call out to Sujeet and his team who developed custom sales analytics dashboards in SFDC for a SoCal based healthcare diagnostics client of ours. Their professionalism, expertise, and flexibility to adjust to client needs were greatly appreciated. MindGap is excited to continue to work with Incentius and add value to our customers.

Samik Banerjee

Founder & CEO

World at Work

Having worked so closely for half a year on our website project, I wanted to thank Incentius for all your fantastic work and efforts that helped us deliver a truly valuable experience to our WorldatWork members. I am in awe of the skills, passion, patience, and above all, the ownership that you brought to this project every day! I do not say this lightly, but we would not have been able to deliver a flawless product, but for you. I am sure you'll help many organizations and projects as your skills and professionalism are truly amazing.

Shantanu Bayaskar

Senior Project Manager

Gogla

It was a pleasure working with Incentius to build a data collection platform for the off-grid solar sector in India. It is rare to find a team with a combination of good understanding of business as well as great technological know-how. Incentius team has this perfect combination, especially their technical expertise is much appreciated. We had a fantastic time working with their expert team, especially with Amit.

Viraj gada

Gogla

Humblx

Choosing Incentius to work with is one of the decisions we are extremely happy with. It's been a pleasure working with their team. They have been tremendously helpful and efficient through the intense development cycle that we went through recently. The team at Incentius is truly agile and open to a discussion in regards to making tweaks and adding features that may add value to the overall solution. We found them willing to go the extra mile for us and it felt like working with someone who rooted for us to win.

Samir Dayal Singh

CEO Humblx

Transportation & Logistics Consulting Organization

Incentius is very flexible and accommodating to our specific needs as an organization. In a world where approaches and strategies are constantly changing, it is invaluable to have an outsourcer who is able to adjust quickly to shifts in the business environment.

Transportation & Logistics Consulting Organization

Consultant

Mudraksh & McShaw

Incentius was instrumental in bringing the visualization aspect into our investment and trading business. They helped us organize our trading algorithms processing framework, review our backtests and analyze results in an efficient, visual manner.

Priyank Dutt Dwivedi

Mudraksh & McShaw Advisory

Leading Healthcare Consulting Organization

The Incentius resource was highly motivated and developed a complex forecasting model with minimal supervision. He was thorough with quality checks and kept on top of multiple changes.

Leading Healthcare Consulting Organization

Sr. Principal

US Fortune 100 Telecommunications Company

The Incentius resource was highly motivated and developed a complex forecasting model with minimal supervision. He was thorough with quality checks and kept on top of multiple changes.

Incentive Compensation

Sr. Director

Most Read
Optimizing Performance in Large-Scale SaaS Applications: A Practical Guide

Building a large-scale SaaS application is an exciting journey, but as the application grows, so do the performance challenges. In this blog, we’ll explore how to optimize a SaaS application built with Python Flask (backend), PostgreSQL (database), and Vue.js + Quasar Framework (frontend).

Yash Pukale

  1. Feb 03, 2025
  2. 4 min read
The Rise of Autonomous AI Agents: Building Intelligent Systems with AutoGPT and LangChain

Artificial intelligence is evolving rapidly, and autonomous AI agents represent one of the most exciting advancements in the field. These agents are designed to act independently, make decisions, and execute tasks with minimal human intervention. Leveraging cutting-edge technologies like AutoGPT and LangChain, developers can create powerful systems that transform workflows, boost productivity, and foster innovation. This blog explores what autonomous AI agents are, how these tools work, and practical steps to build your own intelligent systems.

Vinay Chaudhari

  1. Jan 22, 2025
  2. 4 min read
Avoiding Common Pitfalls: The Best Approach to AWS SES Integration in Flask

Email functionality is crucial for any web application. Whether it's for sending registration confirmations, password resets, or system alerts, ensuring emails are sent reliably is a top priority. AWS Simple Email Service (SES) provides a powerful, scalable, and cost-effective solution for sending emails in Flask applications. However, many developers run into common pitfalls when setting up AWS SES.

Saurav Jaiswal

  1. Jan 07, 2025
  2. 4 min read
Writing, packaging, and deploying AWS Lambda function written in Python

AWS Lambda is a serverless computing service provided by AWS. It is a service that runs your code in response to an event and automatically manages the resources required for running your code. You don't need to worry about any underlying resources which are required.

Mayank Patel

  1. Jan 02, 2025
  2. 4 min read