瀏覽代碼

Gracefully handle SecurityError exception if localStorage is unavailable (#3775)

* Gracefully handle SecurityError exception if localStorage is unavailable

* use Map()

Co-authored-by: Eric Kwoka <43540491+ekwoka@users.noreply.github.com>

* wip

---------

Co-authored-by: Eric Kwoka <43540491+ekwoka@users.noreply.github.com>
Co-authored-by: Caleb Porzio <calebporzio@gmail.com>
Shaun Dychko 1 年之前
父節點
當前提交
f66804f92c
共有 1 個文件被更改,包括 15 次插入1 次删除
  1. 15 1
      packages/persist/src/index.js

+ 15 - 1
packages/persist/src/index.js

@@ -1,7 +1,21 @@
 export default function (Alpine) {
 export default function (Alpine) {
     let persist = () => {
     let persist = () => {
         let alias
         let alias
-        let storage = localStorage
+        let storage
+
+        try {
+            storage = localStorage
+        } catch (e) {
+            console.error(e)
+            console.warn('Alpine: $persist is using temporary storage since localStorage is unavailable.')
+
+            let dummy = new Map();
+
+            storage = {
+                getItem: dummy.get.bind(dummy),
+                setItem: dummy.set.bind(dummy)
+            }
+        }
 
 
         return Alpine.interceptor((initialValue, getter, setter, path, key) => {
         return Alpine.interceptor((initialValue, getter, setter, path, key) => {
             let lookup = alias || `_x_${path}`
             let lookup = alias || `_x_${path}`