From 7fb331c24aea5688ccf5941b6f4c998b050625c0 Mon Sep 17 00:00:00 2001 From: q_h Date: Tue, 15 Jun 2021 20:59:34 +0300 Subject: [PATCH] fix: more reliable testing of localstorage access (#308) --- src/uploadx/lib/store.ts | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/uploadx/lib/store.ts b/src/uploadx/lib/store.ts index fa9c2711..38efb6c7 100644 --- a/src/uploadx/lib/store.ts +++ b/src/uploadx/lib/store.ts @@ -14,7 +14,17 @@ class Store { } } -export const store = - typeof window !== 'undefined' && 'localStorage' in window - ? new Store() - : new Map(); +export const store = isLocalStorageAvailable() ? new Store() : new Map(); + +function isLocalStorageAvailable(): boolean { + try { + const key = 'uploadxLocalStorageTest'; + const value = 'value'; + localStorage.setItem(key, value); + const getValue = localStorage.getItem(key); + localStorage.removeItem(key); + return getValue === value; + } catch { + return false; + } +}