Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] Add cacheEnabled prop #152

Merged
merged 12 commits into from Jan 30, 2019
@@ -1,8 +1,11 @@
package com.reactnativecommunity.webview;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;

import com.facebook.react.uimanager.UIManagerModule;

import java.util.LinkedList;
import java.util.List;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -303,6 +306,7 @@ protected RNCWebViewBridge createRNCWebViewBridge(RNCWebView webView) {
return new RNCWebViewBridge(webView);
}

@SuppressLint("AddJavascriptInterface")
public void setMessagingEnabled(boolean enabled) {
if (messagingEnabled == enabled) {
return;
Expand Down Expand Up @@ -481,6 +485,21 @@ public void setJavaScriptEnabled(WebView view, boolean enabled) {
view.getSettings().setJavaScriptEnabled(enabled);
}

@ReactProp(name = "enableCache")
public void setCacheEnabled(WebView view, boolean enabled) {
if (enabled) {
Context ctx = view.getContext();
if (ctx != null) {
Titozzz marked this conversation as resolved.
Show resolved Hide resolved
view.getSettings().setAppCachePath(ctx.getCacheDir().getAbsolutePath());
view.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
view.getSettings().setAppCacheEnabled(true);
}
} else {
view.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
view.getSettings().setAppCacheEnabled(false);
}
}

@ReactProp(name = "overScrollMode")
public void setOverScrollMode(WebView view, String overScrollModeString) {
Integer overScrollMode;
Expand Down
11 changes: 11 additions & 0 deletions docs/Reference.md
Expand Up @@ -46,6 +46,7 @@ This document lays out the current public properties and methods for the React N
- [`allowsBackForwardNavigationGestures`](Reference.md#allowsbackforwardnavigationgestures)
- [`allowFileAccess`](Reference.md#allowFileAccess)
- [`saveFormDataDisabled`](Reference.md#saveFormDataDisabled)
- [`enableCache`](Reference.md#enableCache)

## Methods Index

Expand Down Expand Up @@ -517,6 +518,16 @@ Sets whether the WebView should disable saving form data. The default value is `
| ------- | -------- | -------- |
| boolean | No | Android |

---

### `enableCache`

Sets whether WebView & WKWebView should use browser caching.

| Type | Required | Default |
| ------- | -------- | ------- |
| boolean | No | true |

## Methods

### `extraNativeComponentConfig()`
Expand Down
1 change: 1 addition & 0 deletions ios/RNCWKWebView.h
Expand Up @@ -38,6 +38,7 @@ shouldStartLoadForRequest:(NSMutableDictionary<NSString *, id> *)request
@property (nonatomic, assign) BOOL hideKeyboardAccessoryView;
@property (nonatomic, assign) BOOL allowsBackForwardNavigationGestures;
@property (nonatomic, copy) NSString *userAgent;
@property (nonatomic, assign) BOOL enableCache;

- (void)postMessage:(NSString *)message;
- (void)injectJavaScript:(NSString *)script;
Expand Down
3 changes: 3 additions & 0 deletions ios/RNCWKWebView.m
Expand Up @@ -94,6 +94,9 @@ - (void)didMoveToWindow
};

WKWebViewConfiguration *wkWebViewConfig = [WKWebViewConfiguration new];
if (_enableCache) {
wkWebViewConfig.websiteDataStore = [WKWebsiteDataStore defaultDataStore];
}
wkWebViewConfig.userContentController = [WKUserContentController new];
[wkWebViewConfig.userContentController addScriptMessageHandler: self name: MessageHanderName];
wkWebViewConfig.allowsInlineMediaPlayback = _allowsInlineMediaPlayback;
Expand Down
1 change: 1 addition & 0 deletions ios/RNCWKWebViewManager.m
Expand Up @@ -46,6 +46,7 @@ - (UIView *)view
RCT_EXPORT_VIEW_PROPERTY(hideKeyboardAccessoryView, BOOL)
RCT_EXPORT_VIEW_PROPERTY(allowsBackForwardNavigationGestures, BOOL)
RCT_EXPORT_VIEW_PROPERTY(userAgent, NSString)
RCT_EXPORT_VIEW_PROPERTY(enableCache, BOOL)

/**
* Expose methods to enable messaging the webview.
Expand Down
3 changes: 2 additions & 1 deletion js/WebView.android.js
Expand Up @@ -70,6 +70,7 @@ class WebView extends React.Component<WebViewSharedProps, State> {
allowFileAccess: false,
saveFormDataDisabled: false,
originWhitelist: WebViewShared.defaultOriginWhitelist,
enableCache: true,
};

static isFileUploadSupported = async () => {
Expand Down Expand Up @@ -290,7 +291,7 @@ class WebView extends React.Component<WebViewSharedProps, State> {
const { onMessage } = this.props;
onMessage && onMessage(event);
};

onLoadingProgress = (event: WebViewProgressEvent) => {
const { onLoadProgress} = this.props;
onLoadProgress && onLoadProgress(event);
Expand Down
1 change: 1 addition & 0 deletions js/WebView.ios.js
Expand Up @@ -130,6 +130,7 @@ class WebView extends React.Component<WebViewSharedProps, State> {

static defaultProps = {
useWebKit: true,
enableCache: true,
originWhitelist: WebViewShared.defaultOriginWhitelist,
};

Expand Down
5 changes: 5 additions & 0 deletions js/WebViewTypes.js
Expand Up @@ -445,6 +445,11 @@ export type WebViewSharedProps = $ReadOnly<{|
*/
nativeConfig?: ?WebViewNativeConfig,

/**
* Should caching be enabled. Default is true.
*/
enableCache?: ?boolean,

style?: ViewStyleProp,
children: Node,
|}>;