Skip to content

Commit 536f1ee

Browse files
author
Shiranuit
authored
Merge pull request #718 from kuzzleio/fix/error-with-react-native
Fix error with React Native
2 parents a7b70a8 + fdb9750 commit 536f1ee

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

src/utils/debug.js

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,33 @@ let NODE_DEBUG;
33
/* eslint no-undef: 0 */
44

55
function shouldDebug() {
6-
if (typeof window === "undefined") {
7-
// Avoid multiple calls to process.env
8-
if (!NODE_DEBUG) {
9-
NODE_DEBUG = (process.env.DEBUG || "").includes("kuzzle-sdk");
6+
/**
7+
* Some framework like react-native or other might emulate the window object
8+
* but when on plateforms like iOS / Android, the window.location is undefined.
9+
*
10+
* So we need to check if window.location is defined before using it otherwise
11+
* we will get an error.
12+
*
13+
* If something went wrong, be sure to return false to avoid any error.
14+
*/
15+
try {
16+
if (typeof window === "undefined") {
17+
// Avoid multiple calls to process.env
18+
if (!NODE_DEBUG) {
19+
NODE_DEBUG = (process.env.DEBUG || "").includes("kuzzle-sdk");
20+
}
21+
22+
return NODE_DEBUG;
1023
}
1124

12-
return NODE_DEBUG;
25+
return (
26+
window.debugKuzzleSdk ||
27+
(window.location &&
28+
new URL(window.location).searchParams.get("debugKuzzleSdk") !== null)
29+
);
30+
} catch (e) {
31+
return false;
1332
}
14-
15-
return (
16-
window.debugKuzzleSdk ||
17-
new URL(window.location).searchParams.get("debugKuzzleSdk") !== null
18-
);
1933
}
2034

2135
/**

0 commit comments

Comments
 (0)