Skip to content

fix(localstorage): strip base from watched keys and ignore keys outside it - #854

Open
RaphaelFakhri wants to merge 1 commit into
unjs:mainfrom
RaphaelFakhri:fix/localstorage-watch-base
Open

RaphaelFakhri wants to merge 1 commit into
unjs:mainfrom
RaphaelFakhri:fix/localstorage-watch-base

Conversation

@RaphaelFakhri

@RaphaelFakhri RaphaelFakhri commented Oct 18, 2026 •

Copy link
Copy Markdown

resolves #853

The localstorage driver's storage event listener passes ev.key to the watch callback without applying base. With base: "app", a write to user:1 from another tab is reported as app:user:1, and writes to unrelated keys outside base are reported too. Other driver methods (getItem, getKeys, clear) already add or strip base.

This change:

  • Ignores storage events for keys outside base.
  • Strips the base: prefix before the driver calls the callback.

The behavior is unchanged when no base is set. session-storage reuses this driver and gets the same fix.

Tests

  • The existing watch localstorage and unwatch localstorage tests now dispatch the key that localStorage actually holds (test:s1:random_file, because the test driver uses base: "test"). They still expect the watcher to receive s1:random_file.
  • New test watch ignores keys outside of base dispatches other:random_file and expects no event.

Without the source change, all three tests fail. With it, they pass. pnpm build, pnpm test:types, pnpm vitest run and pnpm lint pass.

Summary by CodeRabbit

  • Bug Fixes
    • Local-storage watchers now ignore events outside the configured base and report matching keys without the base prefix. Update and remove event classification remains unchanged.

@RaphaelFakhri
RaphaelFakhri requested a review from pi0 as a code owner October 18, 2026 11:07
@vercel

vercel Bot commented Oct 18, 2026

Copy link
Copy Markdown

@RaphaelFakhri is attempting to deploy a commit to the unjs Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 31e8b4e7-af91-4d6b-b08e-a5721265703b
📥 Commits

Reviewing files that changed from the base of the PR and between b33715a and 2f1a599.

📒 Files selected for processing: 2
  • src/drivers/localstorage.ts
  • test/drivers/localstorage.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

📝 Walkthrough

The local-storage watcher now filters storage events by the configured base. It reports accepted keys without the base prefix. Tests cover matching and non-matching event keys, and unwatch behavior.

Changes: Base-aware watcher

Layer / File(s) Summary
Filter and report storage events
src/drivers/localstorage.ts, test/drivers/localstorage.test.ts
The watcher ignores event keys outside the configured base and removes the base prefix from accepted keys. Tests cover namespaced keys, ignored keys, and unwatch behavior.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Merge Risk: ⚪ Minimal · up to 2f1a5

Matching local-storage events are reported with base-relative keys, while events outside the configured base are ignored. No actionable merge-blocking risk remains.

Pre-merge checks: Passed 5
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: stripping the configured base from watched keys and ignoring keys outside that base.
Linked Issues check Passed Issue [#853] requires localstorage and session-storage watchers to respect base. The listener now accepts only keys with the normalized base: prefix and reports the key without that prefix. It…
Out of Scope Changes check Passed The pull request changes only the localstorage watcher implementation and its focused tests. The implementation change directly supports [#853]. The test updates and new regression test are supporting…
Docstring Coverage Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 2…
Full details: Linked Issues check

Explanation

Issue [#853] requires localstorage and session-storage watchers to respect base. The listener now accepts only keys with the normalized base: prefix and reports the key without that prefix. It preserves the full key when no base is configured. session-storage reuses this driver. The tests cover prefixed event keys, unprefixed callback keys, and ignored outside-base keys.

Full details: Out of Scope Changes check

Explanation

The pull request changes only the localstorage watcher implementation and its focused tests. The implementation change directly supports [#853]. The test updates and new regression test are supporting changes. No unrelated production behavior or unrelated files changed.

Full details: Docstring Coverage

Explanation

No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 2 files.

✨ Finishing Touches: 💡 1
🛠️ Fix failing CI checks: 💡
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

This branch has not been deployed

No deployments
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.

localstorage / session-storage: watch ignores the base option

1 participant