Skip to content

Commit 3323b93

Browse files
feat: exclude DEV server url from network logs
1 parent 60088ac commit 3323b93

File tree

3 files changed

+15
-19
lines changed

3 files changed

+15
-19
lines changed

src/modules/Instabug.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,13 @@ export const willRedirectToStore = () => {
633633
NativeInstabug.willRedirectToStore();
634634
};
635635

636+
/**
637+
* This API has be called when changing the default Metro server port (8081) to exclude the DEV URL from network logging.
638+
*/
639+
export const setMetroDevServerPort = (port: number) => {
640+
InstabugConstants.METRO_SERVER_URL = port.toString();
641+
};
642+
636643
export const componentDidAppearListener = (event: ComponentDidAppearEvent) => {
637644
if (_isFirstScreen) {
638645
_lastScreen = event.componentName;

src/modules/NetworkLogger.ts

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,18 @@ import type { RequestHandler } from '@apollo/client';
33
import InstabugConstants from '../utils/InstabugConstants';
44
import xhr, { NetworkData, ProgressCallback } from '../utils/XhrNetworkInterceptor';
55
import { isContentTypeNotAllowed, reportNetworkLog } from '../utils/InstabugUtils';
6-
import { NativeModules } from 'react-native';
6+
import { metroDevServerPort } from './Instabug';
77

88
export type { NetworkData };
99

1010
export type NetworkDataObfuscationHandler = (data: NetworkData) => Promise<NetworkData>;
1111
let _networkDataObfuscationHandler: NetworkDataObfuscationHandler | null | undefined;
1212
let _requestFilterExpression = 'false';
1313

14-
const filterURL = (url: string): string => {
15-
const [protocol, rest] = url.split('://');
16-
return (rest || protocol).split('/')[0];
17-
};
18-
19-
const getDevServerURL = (): string | null => {
20-
if (NativeModules.SourceCode) {
21-
const { scriptURL } = NativeModules.SourceCode;
22-
if (scriptURL) {
23-
return filterURL(scriptURL.toString());
24-
}
25-
}
26-
return null;
27-
};
14+
function getPortFromUrl(url: string) {
15+
const portMatch = url.match(/:(\d+)(?=\/|$)/);
16+
return portMatch ? portMatch[1] : null;
17+
}
2818

2919
/**
3020
* Sets whether network logs should be sent with bug reports.
@@ -44,10 +34,8 @@ export const setEnabled = (isEnabled: boolean) => {
4434
}
4535

4636
if (__DEV__) {
47-
const devServerURL = getDevServerURL();
48-
const networkServerURL = filterURL(network.url);
49-
// Skip logging for requests to the dev server
50-
if (devServerURL === networkServerURL) {
37+
const urlPort = getPortFromUrl(network.url);
38+
if (urlPort === InstabugConstants.METRO_SERVER_URL) {
5139
return;
5240
}
5341
}

src/utils/InstabugConstants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const InstabugConstants = {
1111
'IBG-RN: Expected key and value passed to setUserAttribute to be of type string',
1212
REMOVE_USER_ATTRIBUTES_ERROR_TYPE_MESSAGE:
1313
'IBG-RN: Expected key and value passed to removeUserAttribute to be of type string',
14+
METRO_SERVER_URL: '8081',
1415
};
1516

1617
export default InstabugConstants;

0 commit comments

Comments
 (0)