Skip to content

Commit 2e0b125

Browse files
committed
StoryScreen: lazy-load WebView on first web tab visit instead of preloading
1 parent 89f63f6 commit 2e0b125

1 file changed

Lines changed: 62 additions & 56 deletions

File tree

screens/StoryScreen.jsx

Lines changed: 62 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ export default function StoryScreen() {
225225
<TouchableHighlight
226226
onPress={() => {
227227
setTabView('web');
228+
setWebMounted(true);
228229
}}
229230
onLongPress={() => {
230231
Haptics.selectionAsync();
@@ -386,8 +387,10 @@ export default function StoryScreen() {
386387
];
387388
const tabViews = ['web', 'comments'];
388389
const [tabView, setTabView] = useState(tab);
390+
const [webMounted, setWebMounted] = useState(tab === 'web');
389391
useEffect(() => {
390392
setTabView(tab);
393+
if (tab === 'web') setWebMounted(true);
391394
}, [tab]);
392395

393396
const initialScrollY = useStore((state) => state.storyScroll?.get?.(id) || 0);
@@ -639,69 +642,70 @@ export default function StoryScreen() {
639642
],
640643
}}
641644
>
642-
<WebView
643-
ref={webViewRef}
644-
style={{ backgroundColor: colors.background }}
645-
applicationNameForUserAgent={`${Application.applicationName}/${Application.nativeApplicationVersion}`}
646-
source={{ uri: url }}
647-
originWhitelist={[
648-
'http://*',
649-
'https://*',
650-
'data:*',
651-
'about:*',
652-
]}
653-
decelerationRate="normal"
654-
allowsInlineMediaPlayback
655-
contentInsetAdjustmentBehavior="automatic"
656-
automaticallyAdjustContentInsets
657-
automaticallyAdjustsScrollIndicatorInsets
658-
allowsBackForwardNavigationGestures
659-
renderLoading={() => null}
660-
onNavigationStateChange={(navState) => {
661-
setNavState(navState);
662-
}}
663-
onLoadStart={() => {
664-
progressAnim.setValue(0);
665-
progressOpacityAnim.setValue(1);
666-
addLink(url);
667-
}}
668-
onLoadEnd={() => {
669-
Animated.timing(progressAnim, {
670-
toValue: 1,
671-
duration: 300,
672-
useNativeDriver: false,
673-
}).start(() => {
674-
Animated.timing(progressOpacityAnim, {
675-
toValue: 0,
676-
delay: 100,
645+
{webMounted && (
646+
<WebView
647+
ref={webViewRef}
648+
style={{ backgroundColor: colors.background }}
649+
applicationNameForUserAgent={`${Application.applicationName}/${Application.nativeApplicationVersion}`}
650+
source={{ uri: url }}
651+
originWhitelist={[
652+
'http://*',
653+
'https://*',
654+
'data:*',
655+
'about:*',
656+
]}
657+
decelerationRate="normal"
658+
allowsInlineMediaPlayback
659+
contentInsetAdjustmentBehavior="automatic"
660+
automaticallyAdjustContentInsets
661+
automaticallyAdjustsScrollIndicatorInsets
662+
allowsBackForwardNavigationGestures
663+
renderLoading={() => null}
664+
onNavigationStateChange={(navState) => {
665+
setNavState(navState);
666+
}}
667+
onLoadStart={() => {
668+
progressAnim.setValue(0);
669+
progressOpacityAnim.setValue(1);
670+
addLink(url);
671+
}}
672+
onLoadEnd={() => {
673+
Animated.timing(progressAnim, {
674+
toValue: 1,
677675
duration: 300,
678676
useNativeDriver: false,
679-
}).start();
680-
});
681-
}}
682-
onLoadProgress={(e) => {
683-
const { progress, loading } = e.nativeEvent;
684-
Animated.timing(progressAnim, {
685-
toValue: progress,
686-
duration: 1000,
687-
useNativeDriver: false,
688-
}).start(() => {
689-
if (progress > 0.99) {
677+
}).start(() => {
690678
Animated.timing(progressOpacityAnim, {
691679
toValue: 0,
692680
delay: 100,
693681
duration: 300,
694682
useNativeDriver: false,
695683
}).start();
696-
}
697-
});
698-
setNavState({
699-
...navState,
700-
loading,
701-
});
702-
}}
703-
onMessage={() => {}}
704-
injectedJavaScript={`
684+
});
685+
}}
686+
onLoadProgress={(e) => {
687+
const { progress, loading } = e.nativeEvent;
688+
Animated.timing(progressAnim, {
689+
toValue: progress,
690+
duration: 1000,
691+
useNativeDriver: false,
692+
}).start(() => {
693+
if (progress > 0.99) {
694+
Animated.timing(progressOpacityAnim, {
695+
toValue: 0,
696+
delay: 100,
697+
duration: 300,
698+
useNativeDriver: false,
699+
}).start();
700+
}
701+
});
702+
setNavState({
703+
...navState,
704+
loading,
705+
});
706+
}}
707+
onMessage={() => {}}
708+
injectedJavaScript={`
705709
try {
706710
document.querySelectorAll('video[autoplay]').forEach(v => v.playsInline = true);
707711
var observer = new MutationObserver(function(mutations) {
@@ -711,7 +715,8 @@ export default function StoryScreen() {
711715
} catch (e) {}
712716
true;
713717
`}
714-
/>
718+
/>
719+
)}
715720
</Animated.View>
716721
<ScrollView
717722
pointerEvents="none"
@@ -778,6 +783,7 @@ export default function StoryScreen() {
778783
const index = e.nativeEvent.selectedSegmentIndex;
779784
const tab = tabViews[index].toLowerCase();
780785
setTabView(tab);
786+
if (tab === 'web') setWebMounted(true);
781787
}}
782788
/>
783789
</Stack.Toolbar.View>

0 commit comments

Comments
 (0)