Skip to content

Commit 7d548c8

Browse files
committed
fix: handle errors in onShouldStartLoadWithRequest callback
1 parent d09152e commit 7d548c8

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

src/YoutubeIframe.js

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ import React, {
99
} from 'react';
1010
import {View, StyleSheet, Platform} from 'react-native';
1111
import {WebView} from './WebView';
12-
import {PLAYER_STATES, PLAYER_ERROR, CUSTOM_USER_AGENT} from './constants';
12+
import {
13+
PLAYER_ERROR,
14+
PLAYER_STATES,
15+
DEFAULT_BASE_URL,
16+
CUSTOM_USER_AGENT,
17+
} from './constants';
1318
import {EventEmitter} from 'events';
1419
import {
1520
playMode,
@@ -18,9 +23,6 @@ import {
1823
PLAYER_FUNCTIONS,
1924
} from './PlayerScripts';
2025

21-
const defaultBaseUrl =
22-
'https://lonelycpp.github.io/react-native-youtube-iframe/iframe.html';
23-
2426
const YoutubeIframe = (props, ref) => {
2527
const {
2628
height,
@@ -125,8 +127,9 @@ const YoutubeIframe = (props, ref) => {
125127

126128
const onWebMessage = useCallback(
127129
event => {
128-
const message = JSON.parse(event.nativeEvent.data);
129130
try {
131+
const message = JSON.parse(event.nativeEvent.data);
132+
130133
switch (message.eventType) {
131134
case 'fullScreenChange':
132135
onFullScreenChange(message.data);
@@ -177,6 +180,20 @@ const YoutubeIframe = (props, ref) => {
177180
],
178181
);
179182

183+
const onShouldStartLoadWithRequest = useCallback(
184+
request => {
185+
try {
186+
const url = request.mainDocumentURL || request.url;
187+
return url.startsWith(baseUrlOverride || DEFAULT_BASE_URL);
188+
} catch (error) {
189+
// defaults to true in case of error
190+
// returning false stops the video from loading
191+
return true;
192+
}
193+
},
194+
[baseUrlOverride],
195+
);
196+
180197
const source = useMemo(() => {
181198
const ytScript = MAIN_SCRIPT(
182199
videoId,
@@ -194,7 +211,7 @@ const YoutubeIframe = (props, ref) => {
194211
return res;
195212
}
196213

197-
const base = baseUrlOverride || defaultBaseUrl;
214+
const base = baseUrlOverride || DEFAULT_BASE_URL;
198215
const data = ytScript.urlEncodedJSON;
199216

200217
return {uri: base + '?data=' + data};
@@ -211,23 +228,18 @@ const YoutubeIframe = (props, ref) => {
211228
return (
212229
<View style={{height, width}}>
213230
<WebView
231+
bounces={false}
214232
originWhitelist={['*']}
215233
allowsInlineMediaPlayback
216234
style={[styles.webView, webViewStyle]}
217235
mediaPlaybackRequiresUserAction={false}
236+
onShouldStartLoadWithRequest={onShouldStartLoadWithRequest}
218237
allowsFullscreenVideo={!initialPlayerParams?.preventFullScreen}
219238
userAgent={
220239
forceAndroidAutoplay
221240
? Platform.select({android: CUSTOM_USER_AGENT, ios: ''})
222241
: ''
223242
}
224-
onShouldStartLoadWithRequest={request => {
225-
return request.mainDocumentURL.startsWith(
226-
baseUrlOverride || defaultBaseUrl,
227-
);
228-
// return request.mainDocumentURL === 'about:blank';
229-
}}
230-
bounces={false}
231243
// props above this are override-able
232244

233245
// --

src/constants.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,6 @@ export const PLAYER_ERROR = {
3838

3939
export const CUSTOM_USER_AGENT =
4040
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36';
41+
42+
export const DEFAULT_BASE_URL =
43+
'https://lonelycpp.github.io/react-native-youtube-iframe/iframe.html';

0 commit comments

Comments
 (0)