Skip to content

Commit 3358219

Browse files
committed
perf improvement
1 parent 131e8f5 commit 3358219

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

packages/browser/src/core/user/index.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,12 @@ class Store {
5959
const ONE_YEAR = 365
6060

6161
export class Cookie extends Store {
62+
static _available: boolean | undefined
6263
static available(): boolean {
64+
if (Cookie._available !== undefined) {
65+
return Cookie._available
66+
}
67+
6368
let cookieEnabled = window.navigator.cookieEnabled
6469

6570
if (!cookieEnabled) {
@@ -68,6 +73,8 @@ export class Cookie extends Store {
6873
jar.remove('ajs:cookies')
6974
}
7075

76+
Cookie._available = cookieEnabled
77+
7178
return cookieEnabled
7279
}
7380

@@ -137,13 +144,21 @@ export class Cookie extends Store {
137144
}
138145

139146
export class LocalStorage extends Store {
147+
static _available: boolean | undefined
148+
140149
static available(): boolean {
150+
if (LocalStorage._available !== undefined) {
151+
return LocalStorage._available
152+
}
153+
141154
const test = 'test'
142155
try {
143156
localStorage.setItem(test, test)
144157
localStorage.removeItem(test)
158+
LocalStorage._available = true
145159
return true
146160
} catch (e) {
161+
LocalStorage._available = false
147162
return false
148163
}
149164
}

0 commit comments

Comments
 (0)