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
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
Real-Time Data Streaming with Python Flask and Quasar Framework

Implementing real-time data streaming from a server to a client can be challenging, especially when working with APIs that return data in chunks. Let me share a story of how I tackled this problem while using Python Flask for the backend and Vue.js with the Quasar framework for the frontend. It was a journey filled with trials, errors, and some exciting discoveries.

Yash Pukale

  1. Dec 23, 2024
  2. 4 min read
A Fresh Take on Agentic AI: Transforming How We Work and Innovate

Agentic AI is quickly becoming a buzzword in the world of technology, and for good reason. Imagine AI agents capable of thinking, planning, and executing tasks with minimal human input—this is the promise of Agentic AI. It’s a revolutionary step forward, allowing businesses to operate smarter, faster, and more efficiently.

Chetan Patel

  1. Dec 19, 2024
  2. 4 min read
Simplifying Data Workloads: Amazon S3 Tables and Apache Iceberg

In the world of big data, efficient management and analysis of large datasets is crucial. Amazon S3 Tables offer a fully managed solution built on Apache Iceberg, a modern table format designed to handle massive-scale analytical workloads with precision and efficiency.

Mayur Patel

  1. Dec 11, 2024
  2. 4 min read