整理這些技術筆記真的很花時間,如果你願意 關閉 Adblock 支持我,我會把這份感謝轉換成更多「踩坑轉避坑」的內容給你!ヽ(・∀・)ノ
本地開發遇到「Uncaught DOMException: Failed to read the 'localStorage' property from 'Window': Access is denied for this document.」
前言
最近遇到一個很特別的狀況,也就是在本地開發時 localStorage 會噴「Uncaught DOMException: Failed to read the 'localStorage' property from 'Window': Access is denied for this document.」這個錯,所以紀錄一下這個問題。
問題點
找了一份有使用 localStorage 開發的專案,然後使用 Live Server 等模擬伺服器開啟之後,開啟瀏覽器輸入 http://localhost:5500/,接下來就會在 Console 控制台看到以下錯誤訊息
Uncaught DOMException: Failed to read the ‘localStorage’ property from ‘Window’: Access is denied for this document.
Uncaught DOMException: Failed to read the ‘sessionStorage’ property from ‘Window’: Access is denied for this document.

解決方式
解決方式非常簡單,只需要將該網域加入到允許網域內就可以正常運作了

但有趣的事情是,第三方 Cookie 封鎖的現象只會在 http://localhost:5500/ 這個網域,而不會在 http://127.0.0.1:5500/ 網域。
參考文獻
整理這些技術筆記真的很花時間,如果你願意 關閉 Adblock 支持我,我會把這份感謝轉換成更多「踩坑轉避坑」的內容給你!ヽ(・∀・)ノ