Skip to content

Commit 1785b0f

Browse files
authored
fix: overly zealous localstorage warning (#1430)
1 parent 71ddfdf commit 1785b0f

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

src/core/storage.ts

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,11 @@ export class Storage {
189189
return this.removeLocalStorage(key)
190190
}
191191

192-
if (!this.localStorageEnabled() || !this.options.localStorage) {
192+
if (!this.isLocalStorageEnabled()) {
193193
return
194194
}
195195

196-
const _key = this.options.localStorage.prefix + key
196+
const _key = this.getPrefix() + key
197197

198198
try {
199199
localStorage.setItem(_key, encodeValue(value))
@@ -207,23 +207,24 @@ export class Storage {
207207
}
208208

209209
getLocalStorage(key: string): unknown {
210-
if (!this.localStorageEnabled() || !this.options.localStorage) {
210+
if (!this.isLocalStorageEnabled()) {
211211
return
212212
}
213213

214-
const _key = this.options.localStorage.prefix + key
214+
const _key = this.getPrefix() + key
215215

216216
const value = localStorage.getItem(_key)
217217

218218
return decodeValue(value)
219219
}
220220

221221
removeLocalStorage(key: string): void {
222-
if (!this.localStorageEnabled() || !this.options.localStorage) {
222+
if (!this.isLocalStorageEnabled()) {
223223
return
224224
}
225225

226-
const _key = this.options.localStorage.prefix + key
226+
const _key = this.getPrefix() + key
227+
227228
localStorage.removeItem(_key)
228229
}
229230

@@ -307,7 +308,24 @@ export class Storage {
307308
this.setCookie(key, undefined, options)
308309
}
309310

310-
localStorageEnabled(): boolean {
311+
getPrefix(): string {
312+
if (!this.options.localStorage) {
313+
throw new Error('Cannot get prefix; localStorage is off')
314+
}
315+
return this.options.localStorage.prefix
316+
}
317+
318+
isLocalStorageEnabled(): boolean {
319+
// Disabled by configuration
320+
if (!this.options.localStorage) {
321+
return false
322+
}
323+
324+
// Local Storage only exists in the browser
325+
if (!process.client) {
326+
return false
327+
}
328+
311329
// There's no great way to check if localStorage is enabled; most solutions
312330
// error out. So have to use this hacky approach :\
313331
// https://stackoverflow.com/questions/16427636/check-if-localstorage-is-available
@@ -317,10 +335,11 @@ export class Storage {
317335
localStorage.removeItem(test)
318336
return true
319337
} catch (e) {
320-
if (this.options.localStorage) {
338+
if (!this.options.ignoreExceptions) {
321339
// eslint-disable-next-line no-console
322340
console.warn(
323-
"[AUTH] Local storage is enabled in config, but browser doesn't support it"
341+
"[AUTH] Local storage is enabled in config, but browser doesn't" +
342+
' support it'
324343
)
325344
}
326345
return false

tsdoc-metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"toolPackages": [
66
{
77
"packageName": "@microsoft/api-extractor",
8-
"packageVersion": "7.19.3"
8+
"packageVersion": "7.19.4"
99
}
1010
]
1111
}

0 commit comments

Comments
 (0)