As developers, we’ve all been there. You’re building a frontend app — maybe with React, maybe just vanilla JS — and you need to persist some data. A token here, a draft form value there, a session timer for the user’s login…

You reach for localStorage or sessionStorage, right? It’s built-in, quick, and doesn’t require cookies or a server. But then reality sets in:

  • You need to expire items after a while. Whoops, there’s no TTL.
  • You want to avoid key collisions across features. Now you’re prefixing keys manually.
  • You suddenly realize you need to support SSR, but window is undefined on the server.
  • You’re handling sensitive tokens, but there’s no way to encrypt them.

What was supposed to be easy starts looking messy. You end up with utility functions, custom wrappers, and too many try...catch blocks.

That’s the pain I wanted to solve. So I built:

🔐 Pocketstore — The Session Storage Your App Deserves

Pocketstore is a lightweight, TypeScript-first wrapper around localStorage and sessionStorage that brings modern dev experience and best practices to in-browser storage.

With Pocketstore, you get:

✅ TTL-based expiration (just like cookies)
✅ Optional AES-style encryption
✅ Key namespacing
✅ SSR-safe fallback (auto in-memory store)
✅ Fully typed API

Install it with:

npm install @m4dm4x/pocketstore

🚧 The Problems Developers Face

Let’s break down the real-world frustrations this package solves:

  1. “I need this token to auto-expire after 15 minutes” localStorage and sessionStorage don’t have built-in TTL. You’d need to store a timestamp, compare on .get(), and manually remove if expired.

➡️ Pocketstore handles this for you.

  1. “I’m using localStorage in a Next.js app and it crashes during SSR”
    window doesn’t exist on the server. You’ll get ReferenceError: window is not defined.
    _
    ➡️ Pocketstore detects the environment and falls back to an in-memory map automatically._

  2. “I need to store a JWT, but plain localStorage is not safe.”
    You want basic encryption at the very least.

➡️ Pocketstore lets you pass a secret to enable encryption.

  1. “I have multiple features using storage and my keys are colliding.” Storing everything as "user", "draft", "token" leads to chaos.

➡️ Pocketstore scopes all keys with a namespace you define.

✨ How to Use Pocketstore

Create a namespaced store:

import { createStore } from "@m4dm4x/pocketstore";

const store = createStore("myapp", {
  storage: "local" // or "session"
});

Set and get values with optional TTL:

store.set("user", { name: "Alice" }, 3600); // expires in 1 hour
const user = store.get("user");

Enable encryption with a secret:

const secureStore = createStore("auth", {
  encrypt: true,
  secret: "supersecret123",
});
secureStore.set("token", "abcdef123456", 1800);

Use safely in SSR (Next.js or Node.js)

const store = createStore("server-safe");
store.set("temp", "data"); // Works on both client and server

💡 Real-World Examples

Store login tokens (session-based):

const auth = createStore("login", { storage: "session" });
auth.set("access_token", "abcd", 1800);

Save form draft progress:

const draft = createStore("signup", { storage: "local" });
draft.set("step1", { name: "Alice" }, 900);

📦 Install & Try It

npm install @m4dm4x/pocketstore
Or explore the GitHub repo: 👉 https://github.com/kritarth1107/pocketstore

🧠 Why Not Just Use localStorage?

Sure, localStorage works… until it doesn’t. As apps grow, so do your needs:

  • Expiry
  • Security
  • Namespacing
  • Server safety
  • Type safety

You either keep reinventing the wheel or reach for a modern solution like Pocketstore.

It’s small. Typed. No dependencies. Works anywhere.

🚀 Ready to Use It?

If you’ve ever Googled:

  • “JavaScript expire localStorage items”
  • “How to store session data without cookies”
  • “SSR-safe localStorage alternative”
  • “TypeScript localStorage wrapper”

…Pocketstore is built for you.

Let’s stop fighting the storage API. Let’s make it work for us.

👉 Try it here: https://www.npmjs.com/package/@m4dm4x/pocketstore
⭐ Star the repo: https://github.com/kritarth1107/pocketstore

Built with love, coffee, and a lot of window is not defined errors ☕️