Skip to Content
Octopus Energy Packages

Local Storage

APIs exported from @octopus-energy/measurements/storage/local-storage

Use this entry point for browser localStorage persistence.

import { createLocalStorageAdapter } from "@octopus-energy/measurements/storage/local-storage";

createLocalStorageAdapter

Creates a StorageAdapter backed by window.localStorage with optional TTL-based expiration and scoped keys.

The adapter subscribes to native storage events for cross-tab changes and a same-tab custom event for writes performed by this package. It is safe for SSR: server snapshots return null and browser-only operations no-op when window.localStorage is unavailable.

Shared across tabs

localStorage is shared across all tabs of the same origin. A write in one tab is visible (and triggers listeners) in every other tab. If you need per-tab isolation, consider createSessionStorageAdapter or the nuqs adapter.

import { createLocalStorageAdapter } from "@octopus-energy/measurements/storage/local-storage";
createLocalStorageAdapter

Prop

Type

import { createLocalStorageAdapter } from "@octopus-energy/measurements/storage/local-storage";
import { usePreferences } from "@octopus-energy/measurements/preferences";
import { useTimeframe } from "@octopus-energy/measurements/timeframe";

const storage = createLocalStorageAdapter({ scope: "electricity" });
const [timeframe, timeframeActions] = useTimeframe({ storage, timezone });
const [preferences, preferencesActions] = usePreferences({ storage });
CreateLocalStorageAdapterOptions

Prop

Type

Scoping

When scope is provided, all keys are suffixed with :{scope} to prevent collisions between multiple instances on the same page.

const elec = createLocalStorageAdapter({ scope: "electricity" });
// "measurements:timeframe" → "measurements:timeframe:electricity"

const gas = createLocalStorageAdapter({ scope: "gas" });
// "measurements:timeframe" → "measurements:timeframe:gas"

Scoping changes stored keys

Scoping is a clean break. Once a scope is adopted, previously stored unscoped keys are ignored.


On this page