Skip to content

Commit 0295953

Browse files
authored
Merge pull request #42 from MacFJA/handle-sandboxed
Fix sandboxed storage
2 parents 244ec3d + 3dba57e commit 0295953

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
### Fixed
1010

11+
- Fix error with sandboxed storage (`localStorage` + `sessionStorage`) ([Issue#41])
1112
- (doc) Typo in README ([PR#39]) + outdated example ([PR#40])
1213
- (dev) Wrong typing ([PR#38])
1314

@@ -185,6 +186,7 @@ First version
185186
[Issue#26]: https://github.com/MacFJA/svelte-persistent-store/issues/26
186187
[Issue#31]: https://github.com/MacFJA/svelte-persistent-store/issues/31
187188
[Issue#32]: https://github.com/MacFJA/svelte-persistent-store/issues/32
189+
[Issue#41]: https://github.com/MacFJA/svelte-persistent-store/issues/41
188190
[PR#8]: https://github.com/MacFJA/svelte-persistent-store/pull/8
189191
[PR#38]: https://github.com/MacFJA/svelte-persistent-store/pull/38
190192
[PR#39]: https://github.com/MacFJA/svelte-persistent-store/pull/39

src/core.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,12 +251,20 @@ function getBrowserStorage(browserStorage: Storage, listenExternalChanges = fals
251251
}
252252
}
253253

254+
function windowStorageAvailable(name: "localStorage" | "sessionStorage" | "indexedDB"): boolean {
255+
try {
256+
return typeof window[name] === "object"
257+
} catch (e) {
258+
return false
259+
}
260+
}
261+
254262
/**
255263
* Storage implementation that use the browser local storage
256264
* @param {boolean} listenExternalChanges Update the store if the localStorage is updated from another page
257265
*/
258266
export function createLocalStorage<T>(listenExternalChanges = false): StorageInterface<T> {
259-
if (typeof window !== "undefined" && window?.localStorage) {
267+
if (windowStorageAvailable("localStorage")) {
260268
return getBrowserStorage(window.localStorage, listenExternalChanges)
261269
}
262270
warnStorageNotFound("window.localStorage")
@@ -268,7 +276,7 @@ export function createLocalStorage<T>(listenExternalChanges = false): StorageInt
268276
* @param {boolean} listenExternalChanges Update the store if the sessionStorage is updated from another page
269277
*/
270278
export function createSessionStorage<T>(listenExternalChanges = false): StorageInterface<T> {
271-
if (typeof window !== "undefined" && window?.sessionStorage) {
279+
if (windowStorageAvailable("sessionStorage")) {
272280
return getBrowserStorage(window.sessionStorage, listenExternalChanges)
273281
}
274282
warnStorageNotFound("window.sessionStorage")
@@ -302,7 +310,7 @@ export function createCookieStorage(): StorageInterface<any> {
302310
* Storage implementation that use the browser IndexedDB
303311
*/
304312
export function createIndexedDBStorage<T>(): SelfUpdateStorageInterface<T> {
305-
if (typeof indexedDB !== "object" || typeof window === "undefined" || typeof window?.indexedDB !== "object") {
313+
if (typeof indexedDB !== "object" || !windowStorageAvailable("indexedDB")) {
306314
warnStorageNotFound("IndexedDB")
307315
return createNoopSelfUpdateStorage()
308316
}

0 commit comments

Comments
 (0)