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

alpinejs-cache

v1.0.1

Published

Persist and expire data easily with Alpine

Readme

Alpine JS Cache

Sometimes you want to persist data, but not until the end of times...

With Alpine Cache, you can easily persist the state across page loads until an expiration date.

Specially useful to remember temporary UI state, to cache API calls, to schedule recurring notifications, etc.

You can even use it inside Alpine context and outside of it, without losing reactivity!

Installation

From a script tag

You can import Alpine Cache through a CDN like:

<html>
    <head>
        <script src="https://unpkg.com/[email protected]/dist/cdn.min.js" defer></script>

        <script src="https://unpkg.com/alpinejs-cache@latest/dist/cache.min.js"></script>
    </head>
</html>

As a module

If you're using a web bundler, you can install Alpine Cache via NPM:

npm i alpinejs-cache -D

Then import it and register the plugin:

import Alpine from 'alpinejs'
import cache from 'alpinejs-cache'

Alpine.plugin(cache)

window.Alpine = Alpine

Alpine.start()

Example

You can find an examples on the examples/ directory of this repository.

Usage

Cache was inspired by Persist, and so, they share the same caveats and style of configuration.

$cache

The primary API for using this plugin is the magic $cache method.

<div x-data="{ count: $cache(0).for(20).as('my-counter') }">
    <button x-on:click="count++">Increment</button>
    <span x-text="count"></span>
</div>

You can set a default value, and an optional custom key which will be used to save the data with the .as modifier.

Unlike $persist, you can set the expiration time of the value, in seconds, with the .for modifier. Each time the value gets changed the cache will be revalidated by .for seconds (60 by default). If the cache gets invalidated the default value is set again on load.

An .until modifier can be used instead of .for, to put a timestamp instead of the amount of seconds.

Custom Storage Driver

Alpine Cache uses the localStorage Browser API by default to store all data.

You can change the storage driver for a single record with the .using modifer.

<div x-data="{ count: $cache(0).using(cookieStorage).for(10) }">
    <button x-on:click="count++">Increment</button>

    <span x-text="count"></span>
</div>

You can also change the default storage driver globally via:

Alpine.cache.defaultDriver = MyCustomStorageDriver;

To create a custom driver, you must implement the following contract:

interface CacheStorageDriver {
    setItem: (key: string, record: CacheRecord) => void,
    getItem: (key: string) => ?CacheRecord
}

The way Alpine Cache works, is with simple CacheRecord objects that wraps your data and adds a timestamp to it:

type CacheRecord = {
    data: any,
    expires_at: number
}

You're responsable to serialize and deserialize this construct on the setItem and getItem methods.

Using outside Alpine context

You can get, set and manually invalidate data, outside the Alpine context while still maintaining the reactivity of the Alpine components that are using $cache.

Alpine.cache is an object with the following signature:

type cacheUtils = {
    defaultDriver: CacheStorageDriver,
    invalidate: (key: string, storage: CacheStorageDriver?) => void,
    set: (key: string, data: any, time: number, storage: CacheStorageDriver?) => void,
    get: (key: string, storage: CacheStorageDriver?) => any
}

set and get works as you expect they would. invalidate is a shorthand to set the value to undefined which Alpine Cache detects as an expired record. The storage parameter is the equivalent to the .using modifier, to override the default driver.

Using $cache with Alpine.data

As it happens with $persist, if you want to use $cache within Alpine.data, you need to be sure to use a standard function instead of an arrow function.

Alpine.data('productsIndex', function () {
    return {
        fetchData: this.$cache(null).until(Date.now() + 1000 * 5),
        init(){
            if(!this.fetchData){
                this.$nextTick(async () => {
                    this.fetchData = await fetch(/*...*/).then((v) => v.json());
                })
            }
        }
    }
})

A caveat not explained in the docs. The values from $cache (and $persist), are read-only on the first tick. No data will be saved if you try to set the properties on init(). If this is your case, wrap the setter in a $nextTick callback.

Stats