Closed DevJett closed 1 year ago
To make it extera secure we can do it like so
try{
localStorage.setItem('t.e.s.t', 'test');
localStorage.removeItem('t.e.s.t');
if('localStorage' in global && global.localStorage){
ls = global.localStorage ;
}
}catch (e){
console.error(`Access denied! Failed to read the 'localStorage' property from 'Window': Access is denied for this document`)
}
Actually, the operation in the
ls
variable that checks iflocalStorage
is enabled is insecure and it causes crashes the SPA's such as ReactJS. For example, if we try toimport ls from "local-storage"
inCodesandbox
live editor it's going to crash the whole application even though we didn't use thelocal-storage
functions yet. See the problem here: codesandboxI just fixed that with
try,catch
, and tested all the following functionsAlso, we no longer need to use the try,carch in
set
function since thels
variable is secure now, and I don't know why you are returning a boolean, is that important?