diff --git a/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java b/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java index dcc928c..a19a081 100644 --- a/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java +++ b/android/src/main/java/com/reactnativecommunity/webview/RNCWebViewManager.java @@ -257,6 +257,27 @@ public class RNCWebViewManager extends SimpleViewManager { } } + @ReactProp(name = "cacheMode") + public void setCacheMode(WebView view, String cacheModeString) { + Integer cacheMode; + switch (cacheModeString) { + case "LOAD_CACHE_ONLY": + cacheMode = WebSettings.LOAD_CACHE_ONLY; + break; + case "LOAD_CACHE_ELSE_NETWORK": + cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK; + break; + case "LOAD_NO_CACHE": + cacheMode = WebSettings.LOAD_NO_CACHE; + break; + case "LOAD_DEFAULT": + default: + cacheMode = WebSettings.LOAD_DEFAULT; + break; + } + view.getSettings().setCacheMode(cacheMode); + } + @ReactProp(name = "androidHardwareAccelerationDisabled") public void setHardwareAccelerationDisabled(WebView view, boolean disabled) { ReactContext reactContext = (ReactContext) view.getContext(); diff --git a/docs/Reference.md b/docs/Reference.md index d2b5e37..f4aa756 100644 --- a/docs/Reference.md +++ b/docs/Reference.md @@ -55,6 +55,7 @@ This document lays out the current public properties and methods for the React N - [`allowFileAccess`](Reference.md#allowFileAccess) - [`saveFormDataDisabled`](Reference.md#saveFormDataDisabled) - [`cacheEnabled`](Reference.md#cacheEnabled) +- [`cacheMode`](Reference.md#cacheMode) - [`pagingEnabled`](Reference.md#pagingEnabled) - [`allowsLinkPreview`](Reference.md#allowsLinkPreview) - [`sharedCookiesEnabled`](Reference.md#sharedCookiesEnabled) @@ -985,6 +986,22 @@ Sets whether WebView should use browser caching. --- +### `cacheMode` + +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. + +Possible values are: +- `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. +- `LOAD_CACHE_ELSE_NETWORK` - Use cached resources when they are available, even if they have expired. Otherwise load resources from the network. +- `LOAD_NO_CACHE` - Don't use the cache, load from the network. +- `LOAD_CACHE_ONLY` - Don't use the network, load from the cache. + +| Type | Required | Default | Platform | +| ------- | -------- | -------------| -------- | +| string | No | LOAD_DEFAULT | Android | + +--- + ### `pagingEnabled` 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. diff --git a/src/WebViewTypes.ts b/src/WebViewTypes.ts index 1039c65..7027069 100644 --- a/src/WebViewTypes.ts +++ b/src/WebViewTypes.ts @@ -145,6 +145,8 @@ export type DataDetectorTypes = export type OverScrollModeType = 'always' | 'content' | 'never'; +export type CacheMode = 'LOAD_DEFAULT' | 'LOAD_CACHE_ONLY' | 'LOAD_CACHE_ELSE_NETWORK' | 'LOAD_NO_CACHE'; + export interface WebViewSourceUri { /** * The URI to load in the `WebView`. Can be a local or remote file. @@ -237,6 +239,7 @@ export interface CommonNativeWebViewProps extends ViewProps { } export interface AndroidNativeWebViewProps extends CommonNativeWebViewProps { + cacheMode?: CacheMode; allowFileAccess?: boolean; scalesPageToFit?: boolean; allowUniversalAccessFromFileURLs?: boolean; @@ -459,6 +462,19 @@ export interface AndroidWebViewProps extends WebViewSharedProps { onNavigationStateChange?: (event: WebViewNavigation) => void; onContentSizeChange?: (event: WebViewEvent) => void; + /** + * https://developer.android.com/reference/android/webkit/WebSettings.html#setCacheMode(int) + * Set the cacheMode. Possible values are: + * + * - `'LOAD_DEFAULT'` (default) + * - `'LOAD_CACHE_ELSE_NETWORK'` + * - `'LOAD_NO_CACHE'` + * - `'LOAD_CACHE_ONLY'` + * + * @platform android + */ + cacheMode?: CacheMode; + /** * https://developer.android.com/reference/android/view/View#OVER_SCROLL_NEVER * Sets the overScrollMode. Possible values are: