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 🙏

© 2025 – Pkg Stats / Ryan Hefner

hx-keep

v1.1.1

Published

keep htmx client state across page refreshes

Readme

Keep your htmx client state persistent across refreshes, new windows, and re-opening of your pages - as simple as:

<div id="logs" hx-keep="first 3">...</div>
<form id="my-form" hx-keep="form">...</form>

banner

We also include bindings to allow client side code to read/write into the hx-page data for custom form elements, see examples.

Getting Started

CDN

<script src="https://unpkg.com/[email protected]"></script>

Bundle

To bundle in an npm-style build system with ES modules, you will have to add htmx to the document like so:

npm i hx-drag
// index.js
import "./htmx";
import "hx-keep";
// htmx.js
import htmx from "htmx.org";
window.htmx = htmx; // to support hx-drag
export default htmx;

Enable

<body hx-ext="hx-keep">...</body>

HTML Attributes

All html attributes can have the data- prefix if required by your framework.

hx-keep="{mode}"

This attribute specifies which data to store and restore from based on the mode set

| Mode | Description | | :- | :- | | innerHTML | Will keep the entire outer html of the element | outerHTML | Will keep only the inner html of the element | first x | Will keep only the first x elements inside the main element. To prevent cutting in weird places we recommend wrapping items in a <div style="display:contents;"> so you can control where it cuts. | last x | Same as first but takes the last elements | form | Tracks and restores only the form values, rather than the entire html

If you want to cache only a single input, we recommend wrapping it in a:

<form id="form" style="display: contents" hx-keep="form"><input name="myValue"></input></form>

hx-keep-key

Used to tell hx-keep how to identify the element, especially useful if it is shared across multiple urls or locations.

If the key is not specified, hx-keep will assume the key is ${url.pathname}#${element.id}. If both the key and id are not specified on the element, then the whole element will be ignored.

hx-keep-hash

The hash is just a simple string value, and if the hash on the element, and the hash of the stored data do not match, then the element will not be restored. This can be helpful if you want to ensure content from an old format is not being restored into a new layout when you update your site.

You could also use this as a date-stamp, so if the underlying data in a form changes it throws away the client cached form.

hx-keep-restore="off"

If set to off, then hx-keep will not restore, however it will still save when changes are made. This can be helpful if on a specific load you want it to not restore from cache, and instead override with the server's contents.

hx-keep-evict

Identical behaviour to the HX-Keep-Evict header, however you can also include the value directly in any element for it to take affect when mounted by htmx.

hx-keep-expiry

How long until the value should be cleared from local storage? Defaults to 30 minutes if unspecified. This must be an integer value with a postfix for the units.

| Postfix | Description | | s | Seconds (i.e. 30s) | | h | Hours | | d | Days |

Note: hx-keep will not automatically clear the data as soon as they expire, it will only prune data when a window is started

hx-history="false"

This library will respect hx-history, and omit any inputs or elements with this attribute when saving it's data.


CSS Classes

hx-keep

If an element has this class if means it is being cached by hx-keep and does not match what the server originally sent.

In the case of a hx-keep="form", this class will update as the form values are changed allowing for save state indicators based on whether the input is actually different from what was originally served.


Http Headers

Request: HX-Keep-Key

If a htmx request is triggered from an element with a hx-keep-key, that value will be included in the request sent to the server. This is helpful in combination with the hx-keep-evict header to clear a value from hx-keep after it has been submitted to the server.

Request: HX-Keep-Hash

Similar to HX-Keep-Key, but instead will include the hx-keep-hash value as a http header.

Response: HX-Keep-Evict

When a response with this header is received by the client, it will evict all hx-keep data where the key matches the pattern(s) supplied. This occurs before rendering meaning these values will be gone before any potential restoration by hx-keep.

The patterns are comma separated and then have their whitespace trimmed before matching.

There is only one type of wildcard supported which is the * which matches with zero to many characters (i.e. /logs/*/short/*).


JS API

Context

Get

window.hx_keep.getValue = (element: Element | null, name: string) => string | null;

Going from the provided element it will find the hx-keep form it is within, then return the value in the form.

Set

window.hx_keep.setValue = (element: Element | null, name: string, value: string, expiry?: number) => void;

Will go from the provided element and find the relevant hx-keep form, then set the provided form value. It will also change the expiry if provided (time from now in milliseconds).

If expiry is unspecified it will continue with the currently expiry for the key. However when specified it must be measured in seconds from now.

Evict

window.hx_keep.set = (pattern: string[] | string) => void;

Will delete the hx-keep entries which have the keys that match the pattern(s) given.

The patterns can use * to signify zero-many characters (i.e. /logs/*/short/*).

Prune

window.hx_keep.prune = () => void;

This will delete all of the expires entries.

Shrink

window.hx_keep.shrink = () => void;

Will prune(), if no entries are removed it will delete the oldest one. This is useful if you have ran out of localStorage capacity

Clear

window.hx_keep.clear = () => void;

Deletes all hx-keep data, this is useful for ensuring all data is removed on logout. Though you could also use a http hx-keep-evict: * header for this purpose


Examples

See https://hx-keep.ajanibilby.com/example