Skip to content

Commit 582f168

Browse files
fix(sdk): safe access localstorage in browser detection
1 parent cec3063 commit 582f168

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

.changeset/tall-balloons-clean.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@module-federation/sdk': patch
3+
---
4+
5+
Resolving issue where localstorage is disabled, for cases like android WebView, where it will be null. This wraps localstorage checks in try catch will fallback to see if document is not undefined

packages/sdk/src/logger.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,23 @@ function safeToString(info: any): string {
1010
}
1111

1212
const DEBUG_LOG = '[ FEDERATION DEBUG ]';
13+
14+
function safeGetLocalStorageItem() {
15+
try {
16+
if (typeof window !== 'undefined' && window.localStorage) {
17+
return localStorage.getItem(BROWSER_LOG_KEY) === BROWSER_LOG_VALUE;
18+
}
19+
} catch (error) {
20+
return typeof typeof document !== 'undefined';
21+
}
22+
return false;
23+
}
1324
class Logger {
1425
enable = false;
1526
identifier: string;
1627
constructor(identifier?: string) {
1728
this.identifier = identifier || DEBUG_LOG;
18-
if (
19-
isBrowserEnv() &&
20-
localStorage.getItem(BROWSER_LOG_KEY) === BROWSER_LOG_VALUE
21-
) {
29+
if (isBrowserEnv() && safeGetLocalStorageItem()) {
2230
this.enable = true;
2331
} else if (isDebugMode()) {
2432
this.enable = true;

0 commit comments

Comments
 (0)