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

use-ssr-storage

v0.1.0

Published

SSR-safe localStorage/sessionStorage React hook built on useSyncExternalStore — no hydration mismatches, cross-tab sync, and typed error handling.

Readme

use-ssr-storage

An SSR-safe localStorage/sessionStorage hook for React, built on useSyncExternalStore. No hydration mismatches, cross-tab sync, typed errors, optional schema validation.

Install

npm install use-ssr-storage

Usage

import { useSSRStorage } from 'use-ssr-storage';

function ThemeToggle() {
  const [theme, setTheme, removeTheme, error] = useSSRStorage('theme', 'light');

  return (
    <button onClick={() => setTheme(theme === 'light' ? 'dark' : 'light')}>
      Current: {theme}
    </button>
  );
}

initialValue is required — it's what renders on the server and during the pre-hydration client render, so there's no T | undefined union to deal with in consumers.

API

function useSSRStorage<T>(
  key: string,
  initialValue: T,
  options?: SSRStorageOptions<T>
): [value: T, setValue: SetValue<T>, removeValue: () => void, error: SSRStorageError | null]

Options

| Option | Default | Description | |---|---|---| | storage | 'local' | 'local', 'session', or a raw Storage-shaped object (for testing/polyfills) | | serializer | JSON | Custom { parse, stringify } | | schema | — | Zod-compatible { parse }. Validates on both read and write | | syncTabs | true | Cross-tab sync via the native storage event. Only applies to real localStorage/sessionStorage | | debounceMs | 0 | Debounces the storage write only — the returned value updates immediately |

Error handling

Read and write failures are handled asymmetrically:

  • Reads never throw. A missing key, corrupted JSON, or a schema validation failure all fall back to initialValue. The failure is still surfaced via the error return value.
  • Writes throw (quota exceeded, storage disabled — e.g. Safari private mode) and populate error, so you can either try/catch around setValue/removeValue, or just read error reactively.
  • A failed write rolls back the optimistic in-memory value — the UI never shows a value that didn't actually persist.
const [value, setValue, removeValue, error] = useSSRStorage('cart', []);

try {
  setValue(newCart);
} catch (e) {
  // storage write failed — value has already rolled back
}

if (error) {
  // render a small "couldn't save" indicator, etc.
}

Known limitations

  • Different initialValues for the same key across components will log a dev-only console warning (not an error) if they diverge before the first write — pick one canonical default per key.
  • Cross-tab sync (syncTabs) only works with real localStorage/sessionStorage — the native storage event never fires for a custom Storage object passed via options.storage.
  • setValue(undefined) is treated as removeValue(), since undefined isn't meaningfully JSON-serializable.

License

This project is licensed under the MIT License