Skip to content

Commit f66804f

Browse files
ShaunDychkoekwokacalebporzio
authored
Gracefully handle SecurityError exception if localStorage is unavailable (#3775)
* Gracefully handle SecurityError exception if localStorage is unavailable * use Map() Co-authored-by: Eric Kwoka <[email protected]> * wip --------- Co-authored-by: Eric Kwoka <[email protected]> Co-authored-by: Caleb Porzio <[email protected]>
1 parent 3184106 commit f66804f

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

packages/persist/src/index.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
11
export default function (Alpine) {
22
let persist = () => {
33
let alias
4-
let storage = localStorage
4+
let storage
5+
6+
try {
7+
storage = localStorage
8+
} catch (e) {
9+
console.error(e)
10+
console.warn('Alpine: $persist is using temporary storage since localStorage is unavailable.')
11+
12+
let dummy = new Map();
13+
14+
storage = {
15+
getItem: dummy.get.bind(dummy),
16+
setItem: dummy.set.bind(dummy)
17+
}
18+
}
519

620
return Alpine.interceptor((initialValue, getter, setter, path, key) => {
721
let lookup = alias || `_x_${path}`

0 commit comments

Comments
 (0)