Skip to content

feat(persisted-state): Add eraseWhenDefault option. And fix incorrect value when storage key is wiped.#424

Open
jkoppel wants to merge 1 commit into
svecosystem:mainfrom
up-to-speed:fix/persisted-state-stale-current-and-erase-when-default
Open

feat(persisted-state): Add eraseWhenDefault option. And fix incorrect value when storage key is wiped.#424
jkoppel wants to merge 1 commit into
svecosystem:mainfrom
up-to-speed:fix/persisted-state-stale-current-and-erase-when-default

Conversation

@jkoppel

@jkoppel jkoppel commented Jun 16, 2026

Copy link
Copy Markdown

If you use PersistedState for settings on dynamically-generated content, you can wind up with a lot of keys. If most of these values are just the default initial value, this is a lot of wasted space, especially if the thing being stored is a rich object. Since localStorage is very limited in space, this can be a problem.

The eraseWhenDefault option greatly mitigates this problem, deleting redundant information, at the cost of a small computation overhead on every write.

The code and docs were drafted with AI, but heavily reviewed and edited by human. The test is AI-written and unreviewed. This PR comment is entirely human written.

… value when storage key is wiped.

PersistedState.current treats storage as the source of truth and falls back to an
in-memory value when the key is absent, but that fallback was never updated on write
(only in the constructor and on cross-tab events), so current could return a stale
value whenever the key was wiped.

Keep the in-memory value in sync on every write, and add an eraseWhenDefault option
that removes the key when the value deep-equals initialValue (an absent key reads back
as the default), keeping storage clean and letting a later change to the hard-coded
default take effect.
@changeset-bot

changeset-bot Bot commented Jun 16, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 69b9ed5

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant