Skip to content

Commit 83ce79f

Browse files
SalakarTitozzz
authored andcommitted
feat(iOS/Android): Add cacheEnabled prop (#152)
Added a new cacheEnabled prop to toggle Android & iOS webview caching behavior. BREAKING CHANGE: This change makes caching enabled by default when previously there was no caching behavior which may cause unexpected behaviour changes in your existing implementations.
1 parent de4130c commit 83ce79f

File tree

8 files changed

+56
-13
lines changed

8 files changed

+56
-13
lines changed

android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java

+18
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package com.reactnativecommunity.webview;
22

3+
import android.annotation.SuppressLint;
34
import android.annotation.TargetApi;
45
import android.app.DownloadManager;
56
import android.content.Context;
7+
68
import com.facebook.react.uimanager.UIManagerModule;
79

810
import java.net.MalformedURLException;
@@ -303,6 +305,7 @@ protected RNCWebViewBridge createRNCWebViewBridge(RNCWebView webView) {
303305
return new RNCWebViewBridge(webView);
304306
}
305307

308+
@SuppressLint("AddJavascriptInterface")
306309
public void setMessagingEnabled(boolean enabled) {
307310
if (messagingEnabled == enabled) {
308311
return;
@@ -519,6 +522,21 @@ public void setJavaScriptEnabled(WebView view, boolean enabled) {
519522
view.getSettings().setJavaScriptEnabled(enabled);
520523
}
521524

525+
@ReactProp(name = "cacheEnabled")
526+
public void setCacheEnabled(WebView view, boolean enabled) {
527+
if (enabled) {
528+
Context ctx = view.getContext();
529+
if (ctx != null) {
530+
view.getSettings().setAppCachePath(ctx.getCacheDir().getAbsolutePath());
531+
view.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
532+
view.getSettings().setAppCacheEnabled(true);
533+
}
534+
} else {
535+
view.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
536+
view.getSettings().setAppCacheEnabled(false);
537+
}
538+
}
539+
522540
@ReactProp(name = "androidHardwareAccelerationDisabled")
523541
public void setHardwareAccelerationDisabled(WebView view, boolean disabled) {
524542
if (disabled) {

docs/Reference.md

+14-3
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ This document lays out the current public properties and methods for the React N
4848
- [`incognito`](Reference.md#incognito)
4949
- [`allowFileAccess`](Reference.md#allowFileAccess)
5050
- [`saveFormDataDisabled`](Reference.md#saveFormDataDisabled)
51+
- [`cacheEnabled`](Reference.md#cacheEnabled)
5152
- [`pagingEnabled`](Reference.md#pagingEnabled)
5253
- [`allowsLinkPreview`](Reference.md#allowsLinkPreview)
5354

@@ -360,9 +361,9 @@ Boolean value to enable third party cookies in the `WebView`. Used on Android Lo
360361

361362
Sets the user-agent for the `WebView`. This will only work for iOS if you are using WKWebView, not UIWebView (see https://developer.apple.com/documentation/webkit/wkwebview/1414950-customuseragent).
362363

363-
| Type | Required | Platform |
364-
| ------ | -------- | -------- |
365-
| string | No | Android, iOS WKWebView |
364+
| Type | Required | Platform |
365+
| ------ | -------- | ---------------------- |
366+
| string | No | Android, iOS WKWebView |
366367

367368
---
368369

@@ -553,6 +554,16 @@ Sets whether the WebView should disable saving form data. The default value is `
553554

554555
---
555556

557+
### `cacheEnabled`
558+
559+
Sets whether WebView & WKWebView should use browser caching.
560+
561+
| Type | Required | Default |
562+
| ------- | -------- | ------- |
563+
| boolean | No | true |
564+
565+
---
566+
556567
### `pagingEnabled`
557568

558569
If the value of this property is true, the scroll view stops on multiples of the scroll view’s bounds when the user scrolls. The default value is false.

ios/RNCWKWebView.h

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
@property (nonatomic, assign) BOOL incognito;
4242
@property (nonatomic, assign) BOOL useSharedProcessPool;
4343
@property (nonatomic, copy) NSString *userAgent;
44+
@property (nonatomic, assign) BOOL cacheEnabled;
4445
@property (nonatomic, assign) BOOL allowsLinkPreview;
4546

4647
- (void)postMessage:(NSString *)message;

ios/RNCWKWebView.m

+3-1
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,11 @@ - (void)didMoveToWindow
8383
WKWebViewConfiguration *wkWebViewConfig = [WKWebViewConfiguration new];
8484
if (_incognito) {
8585
wkWebViewConfig.websiteDataStore = [WKWebsiteDataStore nonPersistentDataStore];
86+
} else if (_cacheEnabled) {
87+
wkWebViewConfig.websiteDataStore = [WKWebsiteDataStore defaultDataStore];
8688
}
8789
if(self.useSharedProcessPool) {
88-
wkWebViewConfig.processPool = [[RNCWKProcessPoolManager sharedManager] sharedProcessPool];
90+
wkWebViewConfig.processPool = [[RNCWKProcessPoolManager sharedManager] sharedProcessPool];
8991
}
9092
wkWebViewConfig.userContentController = [WKUserContentController new];
9193
[wkWebViewConfig.userContentController addScriptMessageHandler: self name: MessageHanderName];

ios/RNCWKWebViewManager.m

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ - (UIView *)view
4848
RCT_EXPORT_VIEW_PROPERTY(incognito, BOOL)
4949
RCT_EXPORT_VIEW_PROPERTY(pagingEnabled, BOOL)
5050
RCT_EXPORT_VIEW_PROPERTY(userAgent, NSString)
51+
RCT_EXPORT_VIEW_PROPERTY(cacheEnabled, BOOL)
5152
RCT_EXPORT_VIEW_PROPERTY(allowsLinkPreview, BOOL)
5253

5354
/**

js/WebView.android.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import ReactNative, {
1717
StyleSheet,
1818
UIManager,
1919
View,
20-
NativeModules
20+
NativeModules,
2121
} from 'react-native';
2222

2323
import invariant from 'fbjs/lib/invariant';
@@ -67,14 +67,15 @@ class WebView extends React.Component<WebViewSharedProps, State> {
6767
scalesPageToFit: true,
6868
allowFileAccess: false,
6969
saveFormDataDisabled: false,
70+
cacheEnabled: true,
7071
androidHardwareAccelerationDisabled: false,
7172
originWhitelist: defaultOriginWhitelist,
7273
};
7374

7475
static isFileUploadSupported = async () => {
7576
// native implementation should return "true" only for Android 5+
7677
return NativeModules.RNCWebView.isFileUploadSupported();
77-
}
78+
};
7879

7980
state = {
8081
viewState: this.props.startInLoadingState
@@ -151,10 +152,13 @@ class WebView extends React.Component<WebViewSharedProps, State> {
151152
injectedJavaScript={this.props.injectedJavaScript}
152153
userAgent={this.props.userAgent}
153154
javaScriptEnabled={this.props.javaScriptEnabled}
154-
androidHardwareAccelerationDisabled={this.props.androidHardwareAccelerationDisabled}
155+
androidHardwareAccelerationDisabled={
156+
this.props.androidHardwareAccelerationDisabled
157+
}
155158
thirdPartyCookiesEnabled={this.props.thirdPartyCookiesEnabled}
156159
domStorageEnabled={this.props.domStorageEnabled}
157160
messagingEnabled={typeof this.props.onMessage === 'function'}
161+
cacheEnabled={this.props.cacheEnabled}
158162
onMessage={this.onMessage}
159163
overScrollMode={this.props.overScrollMode}
160164
contentInset={this.props.contentInset}

js/WebView.ios.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,15 @@ class WebView extends React.Component<WebViewSharedProps, State> {
133133

134134
static defaultProps = {
135135
useWebKit: true,
136+
cacheEnabled: true,
136137
originWhitelist: defaultOriginWhitelist,
137138
useSharedProcessPool: true,
138139
};
139140

140141
static isFileUploadSupported = async () => {
141142
// no native implementation for iOS, depends only on permissions
142143
return true;
143-
}
144+
};
144145

145146
state = {
146147
viewState: this.props.startInLoadingState
@@ -169,10 +170,7 @@ class WebView extends React.Component<WebViewSharedProps, State> {
169170
);
170171
}
171172

172-
if (
173-
!this.props.useWebKit &&
174-
this.props.incognito
175-
) {
173+
if (!this.props.useWebKit && this.props.incognito) {
176174
console.warn(
177175
'The incognito property is not supported when useWebKit = false',
178176
);
@@ -254,13 +252,16 @@ class WebView extends React.Component<WebViewSharedProps, State> {
254252
bounces={this.props.bounces}
255253
scrollEnabled={this.props.scrollEnabled}
256254
pagingEnabled={this.props.pagingEnabled}
255+
cacheEnabled={this.props.cacheEnabled}
257256
decelerationRate={decelerationRate}
258257
contentInset={this.props.contentInset}
259258
automaticallyAdjustContentInsets={
260259
this.props.automaticallyAdjustContentInsets
261260
}
262261
hideKeyboardAccessoryView={this.props.hideKeyboardAccessoryView}
263-
allowsBackForwardNavigationGestures={this.props.allowsBackForwardNavigationGestures}
262+
allowsBackForwardNavigationGestures={
263+
this.props.allowsBackForwardNavigationGestures
264+
}
264265
incognito={this.props.incognito}
265266
userAgent={this.props.userAgent}
266267
onLoadingStart={this._onLoadingStart}

js/WebViewTypes.js

+5
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,11 @@ export type WebViewSharedProps = $ReadOnly<{|
488488
*/
489489
nativeConfig?: ?WebViewNativeConfig,
490490

491+
/**
492+
* Should caching be enabled. Default is true.
493+
*/
494+
cacheEnabled?: ?boolean,
495+
491496
style?: ViewStyleProp,
492497
children: Node,
493498
|}>;

0 commit comments

Comments
 (0)