Skip to content

Commit 101cb47

Browse files
committed
guard localStorage usage - fixes #146
1 parent f35b06e commit 101cb47

1 file changed

Lines changed: 5 additions & 2 deletions

File tree

‎storage.js‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,11 @@ let usePolyfill = true
3939

4040
/* c8 ignore start */
4141
try {
42-
// if the same-origin rule is violated, accessing localStorage might thrown an error
43-
if (typeof localStorage !== 'undefined' && localStorage) {
42+
// Only probe localStorage in a window context. Node.js (>= 25) also defines a `localStorage`
43+
// global, but accessing it (even via `typeof`) emits an ExperimentalWarning unless
44+
// `--localstorage-file` is set. See https://github.com/dmonad/lib0/issues/146
45+
// If the same-origin rule is violated, accessing localStorage might throw an error.
46+
if (typeof window !== 'undefined' && typeof localStorage !== 'undefined' && localStorage) {
4447
_localStorage = localStorage
4548
usePolyfill = false
4649
}

0 commit comments

Comments
 (0)