Skip to content

Commit e92a47b

Browse files
authored
fix(networking): require XHRInterceptor based on React Native version (#1556)
## Please verify the following: - [x] `yarn build-and-test:local` passes ## Describe your PR Resolves #420 #1554 - Now we try to require two different known paths for XHRInterceptor in React Native. - Implemented error handling for loading the XHRInterceptor module, ensuring network monitoring is disabled gracefully if it fails to load. ## Test Plan Install PR changes using [contributing guide](https://docs.infinite.red/reactotron/contributing/#bring-your-own-application) - Install `reactotron-react-native` in a `"react-native": "^0.79.0"` app and verify that network calls are logged https://github.com/user-attachments/assets/47826a64-c498-4bc8-a69e-b2b464150552 - Install `reactotron-react-native` in a `"react-native": "0.78.0"` app and verify that network calls are logged https://github.com/user-attachments/assets/1b5da333-5710-484f-a352-c1583f24c8d5
1 parent 88be6b8 commit e92a47b

File tree

1 file changed

+56
-1
lines changed

1 file changed

+56
-1
lines changed

lib/reactotron-react-native/src/plugins/networking.ts

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,61 @@
1-
import XHRInterceptor from "react-native/Libraries/Network/XHRInterceptor"
21
import type { ReactotronCore, Plugin } from "reactotron-core-client"
32

3+
// Attempt to require XHRInterceptor using static paths
4+
let XHRInterceptorModule
5+
try {
6+
// Try the new path first (for RN >= 0.79)
7+
// Yay breaking changes :( https://github.com/facebook/react-native/releases/tag/v0.79.0#:~:text=APIs%3A%20Move-,XHRInterceptor,-API%20to%20src
8+
XHRInterceptorModule = require("react-native/src/private/inspector/XHRInterceptor")
9+
} catch (e) {
10+
try {
11+
// Fallback to the old path (for RN < 0.79)
12+
XHRInterceptorModule = require("react-native/Libraries/Network/XHRInterceptor")
13+
} catch (e2) {
14+
console.error("Reactotron: Failed to require XHRInterceptor from both known paths.", e, e2)
15+
console.warn(
16+
"Reactotron: XHRInterceptor could not be loaded. Network monitoring will be disabled."
17+
)
18+
// Assign a dummy object later if checks fail
19+
XHRInterceptorModule = null // Indicate failure to require
20+
}
21+
}
22+
23+
let XHRInterceptor
24+
if (XHRInterceptorModule) {
25+
// Check if methods are directly on the module
26+
if (
27+
typeof XHRInterceptorModule.setSendCallback === "function" &&
28+
typeof XHRInterceptorModule.setResponseCallback === "function" &&
29+
typeof XHRInterceptorModule.enableInterception === "function"
30+
) {
31+
XHRInterceptor = XHRInterceptorModule
32+
}
33+
// Check if methods are on the default export
34+
else if (
35+
XHRInterceptorModule.default &&
36+
typeof XHRInterceptorModule.default.setSendCallback === "function" &&
37+
typeof XHRInterceptorModule.default.setResponseCallback === "function" &&
38+
typeof XHRInterceptorModule.default.enableInterception === "function"
39+
) {
40+
XHRInterceptor = XHRInterceptorModule.default
41+
}
42+
}
43+
44+
// If still no valid XHRInterceptor after checking module and module.default, assign the dummy
45+
if (!XHRInterceptor) {
46+
// Log error only if we initially managed to require *something*
47+
if (XHRInterceptorModule) {
48+
console.error("Reactotron: Required XHRInterceptor module does not have expected methods.")
49+
console.warn("Reactotron: Network monitoring will be disabled.")
50+
}
51+
// Assign a dummy object to prevent crashes later when calling its methods
52+
XHRInterceptor = {
53+
setSendCallback: () => {},
54+
setResponseCallback: () => {},
55+
enableInterception: () => {},
56+
}
57+
}
58+
459
/**
560
* Don't include the response bodies for images by default.
661
*/

0 commit comments

Comments
 (0)