Skip to content

Commit

Permalink
only remove namespaced items when clearing localStorage (#2755)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasheyenbrock authored Sep 7, 2022
1 parent d995fb7 commit 674bf3f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/twenty-walls-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphiql/toolkit': patch
---

Only remove namespaced items when clearing `localStorage`
13 changes: 11 additions & 2 deletions packages/graphiql-toolkit/src/storage/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,18 @@ export class StorageAPI {
} else if (storage === null) {
// Passing `null` creates a noop storage
this.storage = null;
} else if (typeof window !== 'undefined') {
this.storage = window.localStorage;
// We only want to clear the namespaced items
this.storage.clear = () => {
for (const key in window.localStorage) {
if (key.indexOf(`${STORAGE_NAMESPACE}:`) === 0) {
window.localStorage.removeItem(key);
}
}
};
} else {
// When passing `undefined` we default to localStorage
this.storage = typeof window !== 'undefined' ? window.localStorage : null;
this.storage = null;
}
}

Expand Down

0 comments on commit 674bf3f

Please sign in to comment.