Skip to content

fix(localstorage): strip base from watch keys and skip keys outside it - #882

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

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

Conversation

@mufasil

@mufasil mufasil commented Nov 19, 2026 •

Copy link
Copy Markdown

Resolves #853

With base set, the storage event listener passed ev.key straight to the watch callback. A watcher on localStorageDriver({ base: "app" }) got app:user:1 instead of user:1, so the key didn't match what getItem and getKeys use, and writes to localStorage keys outside base were reported as changes too. session-storage reuses this driver, so it gets the same fix.

The listener now skips keys that don't start with ${base}: and strips the base from the rest, the same way getKeys does.

On the tests, the existing watch tests dispatched keys without the base and expected them back unchanged, which matched the bug. They now dispatch test:s1:random_file and expect s1:random_file. A new test checks that __external_key__ and testing:s1:a (same prefix, no separator) aren't reported.

Summary by CodeRabbit

  • Bug Fixes
    • Local storage watchers now ignore events without a key and events outside the configured storage prefix.
    • For matching events, callbacks receive the key without the configured prefix.

The storage event listener passed `ev.key` to the watch callback as is.
With `base` set, every key came back with the base still on it, and
writes to unrelated localStorage keys were reported as changes.

Resolves unjs#853
@mufasil
mufasil requested a review from pi0 as a code owner November 19, 2026 03:17
@vercel

vercel Bot commented Nov 19, 2026

Copy link
Copy Markdown

@mufasil 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 Nov 19, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The local storage watcher now ignores keyless events and events outside the configured base. For accepted events, it removes the base prefix before calling the watcher callback. Tests cover these cases and unwatch behavior.

Changes: Local storage watcher

Layer / File(s) Summary
Filter and normalize watch events
src/drivers/localstorage.ts, test/drivers/localstorage.test.ts
The watcher ignores keyless events and keys outside the configured base. It passes accepted keys to the callback without the base prefix. Tests cover accepted keys, ignored keys, and unwatch behavior.

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

Change: Bug fix · Severity of issue fixed: Medium

Possibly related PRs

  • unjs/unstorage#871: Changes the same local storage watcher to filter keys by base and remove the prefix.
Merge Risk: Minimal · up to 3128e

The change makes localStorage and sessionStorage watch keys consistent with getItem and getKeys when a base is configured, and it ignores writes outside that base. No actionable merge risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: the localstorage watch listener now strips the configured base prefix from keys and skips keys outside that base. The raw summary and PR objectives both describe thi…
Linked Issues check Passed [#853] The requirement is that the localStorage watch listener emit keys without base and skip writes outside base. At the reviewed head, watch in src/drivers/localstorage.ts returns early whe…
Out of Scope Changes check Passed The PR changes only src/drivers/localstorage.ts and test/drivers/localstorage.test.ts, according to the PR summary. The source change is limited to the storage listener in watch. The test changes up…
Full details: Title check

Explanation

The title names the main change: the localstorage watch listener now strips the configured base prefix from keys and skips keys outside that base. The raw summary and PR objectives both describe this exact behavior, and the linked issue #853 asks for the same fix. The title is specific and is not vague, misleading, or off-topic.

Full details: Linked Issues check

Explanation

[#853] The requirement is that the localStorage watch listener emit keys without base and skip writes outside base. At the reviewed head, watch in src/drivers/localstorage.ts returns early when ev.key is empty and when base is set and ev.key does not start with ${base}:. For matching keys it strips base.length + 1 characters before calling callback, which matches the getKeys logic. The issue example (app:user:1 reported as user:1, other not reported) is covered by this logic. session-storage reuses this driver, so it receives the same behavior. The test file at head covers the required cases: the existing watch test dispatches test:s1:random_file and expects s1:random_file, the unwatch test uses the base-prefixed key, and the new test 'watch ignores keys outside base' checks that __external_key__ and testing:s1:a produce no callback. The whole-PR base blob was unavailable, so the before-state was judged from the PR summary, not from a diff. The head behavior was read directly. Tests were not executed during this review.

Full details: Out of Scope Changes check

Explanation

The PR changes only src/drivers/localstorage.ts and test/drivers/localstorage.test.ts, according to the PR summary. The source change is limited to the storage listener in watch. The test changes update the watch expectations to the base-prefixed keys and add one test for keys outside base. All changes directly address #853. No unrelated refactoring or unrelated files appear in the supplied evidence. The whole-PR diff could not be read directly, so this verdict relies on the summary for the file list.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
Fix failing CI checks

  • 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

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 28edfcdf-92a6-410b-948a-7c4b8a9f5e08
Commits

Reviewing files that changed from the base of the PR and between 5781aae and 3128e67.

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.

@mufasil

mufasil commented Nov 19, 2026

Copy link
Copy Markdown
Author

Closing this as a duplicate of #854, which fixes the same thing and was opened first. Sorry, I missed it when I checked.

@mufasil mufasil closed this Nov 19, 2026
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