Skip to content

Commit 674bf3f

Browse files
only remove namespaced items when clearing localStorage (#2755)
1 parent d995fb7 commit 674bf3f

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

.changeset/twenty-walls-drive.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphiql/toolkit': patch
3+
---
4+
5+
Only remove namespaced items when clearing `localStorage`

packages/graphiql-toolkit/src/storage/base.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,18 @@ export class StorageAPI {
6060
} else if (storage === null) {
6161
// Passing `null` creates a noop storage
6262
this.storage = null;
63+
} else if (typeof window !== 'undefined') {
64+
this.storage = window.localStorage;
65+
// We only want to clear the namespaced items
66+
this.storage.clear = () => {
67+
for (const key in window.localStorage) {
68+
if (key.indexOf(`${STORAGE_NAMESPACE}:`) === 0) {
69+
window.localStorage.removeItem(key);
70+
}
71+
}
72+
};
6373
} else {
64-
// When passing `undefined` we default to localStorage
65-
this.storage = typeof window !== 'undefined' ? window.localStorage : null;
74+
this.storage = null;
6675
}
6776
}
6877

0 commit comments

Comments
 (0)