Browse Source

feat(android): Expose cacheMode property (#895)

Andres Castano 5 years ago
parent
commit
5da59251ce

+ 21
- 0
android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java View File

257
     }
257
     }
258
   }
258
   }
259
 
259
 
260
+  @ReactProp(name = "cacheMode")
261
+  public void setCacheMode(WebView view, String cacheModeString) {
262
+    Integer cacheMode;
263
+    switch (cacheModeString) {
264
+      case "LOAD_CACHE_ONLY":
265
+        cacheMode = WebSettings.LOAD_CACHE_ONLY;
266
+        break;
267
+      case "LOAD_CACHE_ELSE_NETWORK":
268
+        cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK;
269
+        break;  
270
+      case "LOAD_NO_CACHE":
271
+        cacheMode = WebSettings.LOAD_NO_CACHE;
272
+        break;
273
+      case "LOAD_DEFAULT":
274
+      default:
275
+        cacheMode = WebSettings.LOAD_DEFAULT;
276
+        break;
277
+    }
278
+    view.getSettings().setCacheMode(cacheMode);
279
+  }
280
+
260
   @ReactProp(name = "androidHardwareAccelerationDisabled")
281
   @ReactProp(name = "androidHardwareAccelerationDisabled")
261
   public void setHardwareAccelerationDisabled(WebView view, boolean disabled) {
282
   public void setHardwareAccelerationDisabled(WebView view, boolean disabled) {
262
     ReactContext reactContext = (ReactContext) view.getContext();
283
     ReactContext reactContext = (ReactContext) view.getContext();

+ 17
- 0
docs/Reference.md View File

55
 - [`allowFileAccess`](Reference.md#allowFileAccess)
55
 - [`allowFileAccess`](Reference.md#allowFileAccess)
56
 - [`saveFormDataDisabled`](Reference.md#saveFormDataDisabled)
56
 - [`saveFormDataDisabled`](Reference.md#saveFormDataDisabled)
57
 - [`cacheEnabled`](Reference.md#cacheEnabled)
57
 - [`cacheEnabled`](Reference.md#cacheEnabled)
58
+- [`cacheMode`](Reference.md#cacheMode)
58
 - [`pagingEnabled`](Reference.md#pagingEnabled)
59
 - [`pagingEnabled`](Reference.md#pagingEnabled)
59
 - [`allowsLinkPreview`](Reference.md#allowsLinkPreview)
60
 - [`allowsLinkPreview`](Reference.md#allowsLinkPreview)
60
 - [`sharedCookiesEnabled`](Reference.md#sharedCookiesEnabled)
61
 - [`sharedCookiesEnabled`](Reference.md#sharedCookiesEnabled)
985
 
986
 
986
 ---
987
 ---
987
 
988
 
989
+### `cacheMode`
990
+
991
+Overrides the way the cache is used. The way the cache is used is based on the navigation type. For a normal page load, the cache is checked and content is re-validated as needed. When navigating back, content is not revalidated, instead the content is just retrieved from the cache. This property allows the client to override this behavior.
992
+
993
+Possible values are:
994
+- `LOAD_DEFAULT` - Default cache usage mode. If the navigation type doesn't impose any specific behavior, use cached resources when they are available and not expired, otherwise load resources from the network.
995
+- `LOAD_CACHE_ELSE_NETWORK` - Use cached resources when they are available, even if they have expired. Otherwise load resources from the network.
996
+- `LOAD_NO_CACHE` - Don't use the cache, load from the network.
997
+- `LOAD_CACHE_ONLY` - Don't use the network, load from the cache. 
998
+ 
999
+| Type    | Required | Default      | Platform |
1000
+| ------- | -------- | -------------| -------- |
1001
+| string  | No       | LOAD_DEFAULT | Android  |
1002
+
1003
+---
1004
+
988
 ### `pagingEnabled`
1005
 ### `pagingEnabled`
989
 
1006
 
990
 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.
1007
 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.

+ 16
- 0
src/WebViewTypes.ts View File

145
 
145
 
146
 export type OverScrollModeType = 'always' | 'content' | 'never';
146
 export type OverScrollModeType = 'always' | 'content' | 'never';
147
 
147
 
148
+export type CacheMode = 'LOAD_DEFAULT' | 'LOAD_CACHE_ONLY' | 'LOAD_CACHE_ELSE_NETWORK' | 'LOAD_NO_CACHE';
149
+
148
 export interface WebViewSourceUri {
150
 export interface WebViewSourceUri {
149
   /**
151
   /**
150
    * The URI to load in the `WebView`. Can be a local or remote file.
152
    * The URI to load in the `WebView`. Can be a local or remote file.
237
 }
239
 }
238
 
240
 
239
 export interface AndroidNativeWebViewProps extends CommonNativeWebViewProps {
241
 export interface AndroidNativeWebViewProps extends CommonNativeWebViewProps {
242
+  cacheMode?: CacheMode;
240
   allowFileAccess?: boolean;
243
   allowFileAccess?: boolean;
241
   scalesPageToFit?: boolean;
244
   scalesPageToFit?: boolean;
242
   allowUniversalAccessFromFileURLs?: boolean;
245
   allowUniversalAccessFromFileURLs?: boolean;
459
   onNavigationStateChange?: (event: WebViewNavigation) => void;
462
   onNavigationStateChange?: (event: WebViewNavigation) => void;
460
   onContentSizeChange?: (event: WebViewEvent) => void;
463
   onContentSizeChange?: (event: WebViewEvent) => void;
461
 
464
 
465
+  /**
466
+   * https://developer.android.com/reference/android/webkit/WebSettings.html#setCacheMode(int)
467
+   * Set the cacheMode. Possible values are:
468
+   * 
469
+   * - `'LOAD_DEFAULT'` (default)
470
+   * - `'LOAD_CACHE_ELSE_NETWORK'`
471
+   * - `'LOAD_NO_CACHE'`
472
+   * - `'LOAD_CACHE_ONLY'`
473
+   *
474
+   * @platform android
475
+   */
476
+  cacheMode?: CacheMode;
477
+
462
   /**
478
   /**
463
    * https://developer.android.com/reference/android/view/View#OVER_SCROLL_NEVER
479
    * https://developer.android.com/reference/android/view/View#OVER_SCROLL_NEVER
464
    * Sets the overScrollMode. Possible values are:
480
    * Sets the overScrollMode. Possible values are: