npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

yzs-keep-alive-v2

v0.1.4

Published

A custom KeepAlive component for Vue 2 that doesn't depend on Vue's built-in KeepAlive

Readme

yzs-keep-alive-v2

A custom KeepAlive component for Vue 2 that replicates Vue's built-in KeepAlive functionality without depending on it.

Features

  • Component Instance Caching: Cache and preserve component state
  • Lifecycle Hooks: Supports activated and deactivated hooks
  • Pattern Matching: include and exclude props with string, RegExp, or array patterns
  • Max Cache Limit: LRU (Least Recently Used) eviction policy
  • No Dependency: Doesn't rely on Vue's built-in KeepAlive
  • Vue 2 Native: Uses Options API and abstract: true pattern

Installation

npm install yzs-keep-alive-v2

Basic Usage

<template>
  <div>
    <button @click="currentView = 'home'">Home</button>
    <button @click="currentView = 'about'">About</button>

    <YzsKeepAlive>
      <component :is="currentView" />
    </YzsKeepAlive>
  </div>
</template>

<script>
import YzsKeepAlive from 'yzs-keep-alive-v2'

export default {
  components: {
    YzsKeepAlive
  },
  data() {
    return {
      currentView: 'home'
    }
  },
  components: {
    home: {
      name: 'home',
      template: '<div>Home View - Count: {{ count }}</div>',
      data() {
        return { count: 0 }
      },
      activated() {
        console.log('Home activated')
      },
      deactivated() {
        console.log('Home deactivated')
      }
    },
    about: {
      name: 'about',
      template: '<div>About View</div>'
    }
  }
}
</script>

Props

include

  • Type: string | RegExp | Array
  • Description: Only cache components matching these patterns
<!-- Cache only ComponentA and ComponentB -->
<YzsKeepAlive :include="['ComponentA', 'ComponentB']">
  <component :is="currentView" />
</YzsKeepAlive>

<!-- Cache components matching regex -->
<YzsKeepAlive :include="/^Component/">
  <component :is="currentView" />
</YzsKeepAlive>

exclude

  • Type: string | RegExp | Array
  • Description: Don't cache components matching these patterns
<!-- Don't cache NoCacheComponent -->
<YzsKeepAlive :exclude="['NoCacheComponent']">
  <component :is="currentView" />
</YzsKeepAlive>

max

  • Type: string | number
  • Description: Maximum number of component instances to cache
  • Default: 10
<!-- Cache at most 5 components -->
<YzsKeepAlive :max="5">
  <component :is="currentView" />
</YzsKeepAlive>

Lifecycle Hooks

Cached components can use activated and deactivated hooks:

export default {
  name: 'MyComponent',
  data() {
    return {
      scrollPosition: 0
    }
  },
  activated() {
    // Called when component becomes active
    console.log('Component activated')
    this.restoreScroll()
  },
  deactivated() {
    // Called when component becomes inactive
    console.log('Component deactivated')
    this.saveScroll()
  }
}

Public Methods

Access the component instance via ref to call public methods:

<template>
  <div>
    <YzsKeepAlive ref="keepAlive" :max="10">
      <component :is="currentView" />
    </YzsKeepAlive>

    <button @click="clearAllCache">Clear Cache</button>
  </div>
</template>

<script>
export default {
  methods: {
    clearAllCache() {
      this.$refs.keepAlive.clearCache()
    }
  }
}
</script>

clearCache()

Clear all cached components.

this.$refs.keepAlive.clearCache()

clearCacheByKey(key)

Clear a specific cache entry by key.

this.$refs.keepAlive.clearCacheByKey('component-key')

getCachedKeys()

Get all cached keys.

const keys = this.$refs.keepAlive.getCachedKeys()
console.log('Cached keys:', keys)

getCacheSize()

Get current cache size.

const size = this.$refs.keepAlive.getCacheSize()
console.log('Cache size:', size)

Component Naming

For include and exclude to work properly, ensure your components have a name property:

export default {
  name: 'MyComponent',  // Required for include/exclude
  // ...
}

How It Works

  1. Abstract Component: Uses Vue 2's abstract: true to avoid creating a wrapper DOM element
  2. VNode Interception: Intercepts render function to cache component instances
  3. Instance Reuse: Reuses both the component instance and DOM element
  4. LRU Eviction: When max limit is reached, removes the least recently used cache entry
  5. Keep-Alive Flag: Sets vnode.data.keepAlive = true for Vue 2 renderer

Differences from Vue 3 Version

| Feature | Vue 2 (v2) | Vue 3 (v3) | |---------|------------|------------| | Component API | Options API | Composition API | | Pattern | abstract: true | sharedContext.renderer | | VNode Type | componentOptions | Shape flags | | Cache Storage | Object/Array | Map/Set | | Component Name | componentOptions.Ctor.options.name | Component.__name | | DOM Element | vnode.elm | vnode.el | | Component Instance | vnode.componentInstance | vnode.component |

License

MIT

Author

hnwangzhenwei