19

Context

Currently working a HTML & JS heavy project that is a react native project. All HTML & JS are referenced locally (nothing over http).

Problem

Whenever I make changes to HTML or JS I don't see the changes (after refreshing the native code or simply running it again) so I'm forced to completely uninstall the app.

Question

Is there a way to ignore the cached version of these files (am assuming a caching mechanism exists)

I haven't found any valid resource regarding this topic, so any feedback would be greatly appreciated, thanks.

1
  • did you found and solution, i am facing same problem in my react native app. Commented Feb 5, 2021 at 9:38

4 Answers 4

5

I am assuming that you are using React Native's WebView component to render your HTML and JavaScript. This component relies on the native UIWebView and WebView components, so you can modify the RN WebView using the same procedures as for those. For example, in the iOS case, you can do the following:

[[NSURLCache sharedURLCache] removeAllCachedResponses];
[[NSURLCache sharedURLCache] setDiskCapacity:0];
[[NSURLCache sharedURLCache] setMemoryCapacity:0];

You can put that code in your didFinishLaunchingWithOptions method of AppDelegate.m. Based on this answer.

In the Android case you cannot access the cache unless you have access to the WebView's instance (according to this answer), so what you can do is create your own WebView using the code of the RN WebView combined with the cache-deleting functionality. It's not as complicated as it might seem.

Sign up to request clarification or add additional context in comments.

Comments

4

Looking at the WebView.android.js file (node_modules/react-native-webview/lib/WebView.android.js), there is a property in WebView.defaultProps called cacheEnabled and it is set to true.

You could try setting the cacheEnabled property to false in your WebView JSX:

<WebView
   cacheEnabled={false}
   source={{ uri: env.APP_URL }} style={{flex:1}} />

2 Comments

Will it work when there is no internet connectivity ?
@Sagar, I haven't tested that, but probably not. With the cache disabled, the assets wouldn't exist locally.
1

For me, I used https://github.com/joeferraro/react-native-cookies, and it does clear the cookies for me. Call the following before you use WebView

CookieManager
  .clearAll(true)
  .then((res) => {
    console.log('LoginScreen CookieManager.clearAll =>', res)
  })

Comments

0

For android, the "App info" setting, under Storage will allow you to clear cache. This is how you can force a reload of any resources you are displaying in the WebView.

1 Comment

This might be a quicker alternative, but I was hoping to find a configuration in the RN project itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.