Skip to content

Commit

Permalink
feat(events): Add isTopFrame to shouldStartLoadForRequest (react-nati…
Browse files Browse the repository at this point in the history
…ve-webview#1537)

* Add isTopFrame to shouldStartLoadForRequest on iOS

onLoadingStart is not raised for inner frames, but onShouldStartLoadWithRequest still is. This keeps that behavior but adds isTopFrame to onShouldStartLoadWithRequest so that apps can perform their own filtering if desired.

* Update docs

Co-authored-by: Jamon Holmgren <[email protected]>
  • Loading branch information
TheAlmightyBob and jamonholmgren authored Aug 15, 2020
1 parent 621d2df commit 6a9116f
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class TopShouldStartLoadWithRequestEvent(viewId: Int, private val mData: Writabl

init {
mData.putString("navigationType", "other")
// Android does not raise shouldOverrideUrlLoading for inner frames
mData.putBoolean("isTopFrame", true)
}

override fun getEventName(): String = EVENT_NAME
Expand Down
5 changes: 3 additions & 2 deletions apple/RNCWebView.m
Original file line number Diff line number Diff line change
Expand Up @@ -937,13 +937,15 @@ - (void) webView:(WKWebView *)webView

WKNavigationType navigationType = navigationAction.navigationType;
NSURLRequest *request = navigationAction.request;
BOOL isTopFrame = [request.URL isEqual:request.mainDocumentURL];

if (_onShouldStartLoadWithRequest) {
NSMutableDictionary<NSString *, id> *event = [self baseEvent];
[event addEntriesFromDictionary: @{
@"url": (request.URL).absoluteString,
@"mainDocumentURL": (request.mainDocumentURL).absoluteString,
@"navigationType": navigationTypes[@(navigationType)]
@"navigationType": navigationTypes[@(navigationType)],
@"isTopFrame": @(isTopFrame)
}];
if (![self.delegate webView:self
shouldStartLoadForRequest:event
Expand All @@ -955,7 +957,6 @@ - (void) webView:(WKWebView *)webView

if (_onLoadingStart) {
// We have this check to filter out iframe requests and whatnot
BOOL isTopFrame = [request.URL isEqual:request.mainDocumentURL];
if (isTopFrame) {
NSMutableDictionary<NSString *, id> *event = [self baseEvent];
[event addEntriesFromDictionary: @{
Expand Down
1 change: 1 addition & 0 deletions docs/Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,7 @@ canGoForward
lockIdentifier
mainDocumentURL (iOS only)
navigationType
isTopFrame
```

---
Expand Down
4 changes: 2 additions & 2 deletions src/WebViewShared.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import escapeStringRegexp from 'escape-string-regexp';
import React from 'react';
import { Linking, View, ActivityIndicator, Text } from 'react-native';
import {
WebViewNavigationEvent,
OnShouldStartLoadWithRequest,
ShouldStartLoadRequestEvent,
} from './WebViewTypes';
import styles from './WebView.styles';

Expand Down Expand Up @@ -39,7 +39,7 @@ const createOnShouldStartLoadWithRequest = (
originWhitelist: readonly string[],
onShouldStartLoadWithRequest?: OnShouldStartLoadWithRequest,
) => {
return ({ nativeEvent }: WebViewNavigationEvent) => {
return ({ nativeEvent }: ShouldStartLoadRequestEvent) => {
let shouldStart = true;
const { url, lockIdentifier } = nativeEvent;

Expand Down
12 changes: 9 additions & 3 deletions src/WebViewTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ export interface WebViewNavigation extends WebViewNativeEvent {
mainDocumentURL?: string;
}

export interface ShouldStartLoadRequest extends WebViewNavigation {
isTopFrame: boolean;
}

export interface FileDownload {
downloadUrl: string;
}
Expand Down Expand Up @@ -149,6 +153,8 @@ export type WebViewProgressEvent = NativeSyntheticEvent<

export type WebViewNavigationEvent = NativeSyntheticEvent<WebViewNavigation>;

export type ShouldStartLoadRequestEvent = NativeSyntheticEvent<ShouldStartLoadRequest>;

export type FileDownloadEvent = NativeSyntheticEvent<FileDownload>;

export type WebViewMessageEvent = NativeSyntheticEvent<WebViewMessage>;
Expand Down Expand Up @@ -238,7 +244,7 @@ export interface WebViewNativeConfig {
}

export type OnShouldStartLoadWithRequest = (
event: WebViewNavigation,
event: ShouldStartLoadRequest,
) => boolean;

export interface CommonNativeWebViewProps extends ViewProps {
Expand All @@ -258,15 +264,15 @@ export interface CommonNativeWebViewProps extends ViewProps {
onLoadingStart: (event: WebViewNavigationEvent) => void;
onHttpError: (event: WebViewHttpErrorEvent) => void;
onMessage: (event: WebViewMessageEvent) => void;
onShouldStartLoadWithRequest: (event: WebViewNavigationEvent) => void;
onShouldStartLoadWithRequest: (event: ShouldStartLoadRequestEvent) => void;
showsHorizontalScrollIndicator?: boolean;
showsVerticalScrollIndicator?: boolean;
// TODO: find a better way to type this.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
source: any;
userAgent?: string;
/**
* Append to the existing user-agent. Overriden if `userAgent` is set.
* Append to the existing user-agent. Overridden if `userAgent` is set.
*/
applicationNameForUserAgent?: string;
}
Expand Down

0 comments on commit 6a9116f

Please sign in to comment.