@@ -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 */
258266export 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 */
270278export 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 */
304312export 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