noggin

LocalStorage provider

The localStorage provider backs a noggin with a single YAML document in window.localStorage. It's the browser-native counterpart to the file provider: same atomic-write semantics, same change events, plus cross-tab synchronization via the DOM storage event.

At a glance

Schemelocalstorage://
Module@noggin/engine/providers/localstorage
Default inBrowser hosts (docs site playground, sandboxed renderers)
PersistentYes — per origin, per browser profile
Read-onlyNo
Cross-tab syncVia the DOM storage event
Same-tab driftDetected via a periodic getItem + diff poll (default 500 ms)
Storage layoutSingle key: noggin:<slot>

When to use

Use this when you want a noggin that survives a page reload and needs no server. It's the right choice for:

If you're inside Node, want disk persistence, or need multi-process locking, use the file provider instead.

Quick start

import { openNoggin } from '@noggin/engine';
import '@noggin/engine/providers/localstorage';

const noggin = await openNoggin('localstorage://groceries');
await noggin.push({ title: 'milk' });
await noggin.dispose();

Or skip the registry lookup with the direct factory:

import { openLocalStorageNoggin } from '@noggin/engine/providers/localstorage';

const noggin = await openLocalStorageNoggin({ slot: 'groceries' });

URL syntax

A localstorage://<slot> URL maps to the localStorage key noggin:<slot>. The slot is the noggin's identifier; the full storage key includes the noggin: prefix so the engine can use shared origins without colliding with unrelated keys.

URLSlotStorage key
localstorage://groceriesgroceriesnoggin:groceries
localstorage://playground (default)noggin:playground
localstorage:groceriesgroceriesnoggin:groceries

Use the exported localStorageKeyFor(uri) helper if you need to poke at the underlying storage from host code (e.g. to wipe a specific slot).

Options

openLocalStorageNoggin(opts) accepts:

OptionDefaultPurpose
slot'playground'The slot name (URL path segment)
storageglobalThis.localStorageA custom Storage-shaped object — useful for tests with node-localstorage
windowglobalThis.windowA Window-shaped object the provider attaches the storage listener to
pollIntervalMs500Interval (ms) for the same-tab drift poll. Set to 0 to disable — cross-tab sync via the DOM storage event keeps working either way.

The same options work as positional args when using openNoggin('localstorage://...', { … }).

Persistence and behaviour

Convenience methods

The returned noggin exposes three extra methods on top of the standard Noggin surface for hosts that want playground-style demo flows:

MethodPurpose
snapshot()Read the current document directly
reset()Wipe the slot (fires onDidChange)
loadDocument(doc)Replace the slot wholesale (e.g. "Load sample data")
hasData()True iff the slot has non-empty items

These don't appear on a Noggin typed against the engine surface; narrow with instanceof LocalStorageNoggin (also exported) when you need them.

Error codes you might see

CodeWhen
no-locationThe host runs where globalThis.localStorage is undefined and no storage option was supplied
disposedAn apply() was issued after dispose()

Storage limits

localStorage enforces a per-origin quota — typically ~5 MB. A single noggin is generally tiny (a few KB), but a host with many slots in the same origin can hit the cap. The provider doesn't truncate or compress; if setItem throws, the verb rejects with the underlying error and the in-memory state stays at the old snapshot.