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

@sveltebase/state

v4.0.0

Published

Reactive state for Svelte 5 — plain values in memory, or values that stick around in a cookie.

Readme

@sveltebase/state

Reactive state for Svelte 5 — plain values in memory, or values that stick around in a cookie.

Install

bun add @sveltebase/state svelte

For cookie-backed state you’ll also want a schema library (Zod, Valibot, etc.):

bun add zod

In-memory state

import { State } from "@sveltebase/state";

const count = new State(0);

count.current;      // 0
count.current = 1;  // set directly
count.set((n) => n + 1); // or update from the previous value

That’s it. current is reactive, so Svelte will re-render when it changes.

Cookie-backed state

PersistentState keeps a value in a cookie and validates it with any Standard Schema library (Zod works out of the box).

import { z } from "zod";
import { PersistentState } from "@sveltebase/state";

const theme = new PersistentState(
  "theme",
  z.enum(["light", "dark"]).default("light")
);

theme.current; // "light" | "dark"
theme.current = "dark";
theme.set((t) => (t === "dark" ? "light" : "dark"));
  • The first argument is the cookie name.
  • The schema defines the shape and the default (via .default(...)).
  • Invalid replacements through current or set are rejected; the previous value stays put.
  • Nested mutations are reactive and persist, but bypass schema validation. Use state.set((value) => ({ ...value, count: nextCount })) when validation is required; avoid mutating the callback argument before returning.

In the browser the cookie is read on construction and written on every change. Cookies use path: "/", sameSite: "Lax", a one-year lifetime, and secure on HTTPS.

SvelteKit setup

So SSR and the browser start with the same value, pass only the state’s serialized cookie value into init.

src/routes/+layout.server.ts

export function load({ cookies }) {
  return { locale: cookies.get("locale") };
}

src/lib/state.ts

import { z } from "zod";
import { PersistentState } from "@sveltebase/state";

export const locale = new PersistentState(
  "locale",
  z.enum(["en", "uz"]).default("en")
);

src/routes/+layout.svelte

<script lang="ts">
  import { locale } from "$lib/state";

  let { data } = $props();
  locale.init(() => data.locale);
</script>

{@render children()}

You can pass the serialized cookie value directly or as a function — use a function when the data comes from reactive load props.

init does nothing in the browser. On the server it parses the supplied JSON cookie value, validates it, and sets the value. Missing or invalid cookies fall back to the schema default.

How cookies behave

| Situation | What happens | | --- | --- | | Browser, cookie present | Hydrates from document.cookie | | Browser, cookie missing/invalid | Uses the schema default | | Server, before init | Uses the schema default | | Server, after init | Uses the request cookie (or default if missing) | | Invalid write via current | Throws; old value is kept |

Pass the value returned by cookies.get(key), not the full cookie collection or an already parsed value. init() or init(undefined) uses the schema default on the server.

Values are stored as JSON. Older URI-encoded cookies are still accepted.

Lifetime

Create persistent state once for a long-lived browser state value. Each instance owns a persistence effect and currently has no disposal method.

License

ISC

Agent skills (TanStack Intent)

This package ships its own skill and a shared Sveltebase overview. From your app:

npx @tanstack/intent@latest install
npx @tanstack/intent@latest list
npx @tanstack/intent@latest load '@sveltebase/state#sveltebase'
npx @tanstack/intent@latest load '@sveltebase/state#state'

Select this package during Intent's first-time permission review. The skills come from your installed package version; older releases may not include them.