Skip to content

Commit 26793a0

Browse files
Minishlinkgrabbou
authored andcommitted
Add HTTP cache by default (like iOS) (#18348)
Summary: <!-- Thank you for sending the PR! We appreciate you spending the time to work on these changes. Help us understand your motivation by explaining why you decided to make this change. You can learn more about contributing to React Native here: http://facebook.github.io/react-native/docs/contributing.html Happy contributing! --> On iOS, the cache is enabled by default but it's not the case on Android. This PR adds 10Mo of HTTP cache by default. 10Mo was chosen arbitrarily. Fixes #16795 <!--(Write your test plan here. If you changed any code, please provide us with clear instructions on how you verified your changes work. Bonus points for screenshots and videos!)--> I wrote it so there's no breaking change for those already using the `OkHttpClientProvider.createClient` and `OkHttpClientProvider.createClientBuilder` public methods. 1. react-native init TestAndroidCache 2. Followed https://facebook.github.io/react-native/docs/android-building-from-source.html to build with this branch as react-native 3. Added the following code in the App.js (with correct URL_WITH_CACHE_CONTROL) 4. Made sure the debugger is closed 5. Throttled my mac's network speed with the `Network link conditioner` (edge) 6. Tapped "Fetch", it shows ~4000 7. Tapped "Fetch" again, it shows ~6 ```js import React, { Component } from 'react'; import { StyleSheet, Alert, View, Button } from 'react-native'; type Props = {}; export default class App extends Component<Props> { fetch = () => { const start = Date.now(); fetch('{URL_WITH_CACHE_CONTROL}').then(() => { const diff = Date.now() - start; Alert.alert(diff.toString()); }); }; render() { return ( <View style={styles.container}> <Button title={"fetch"} onPress={this.fetch} /> </View> ); } } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#F5FCFF', }, }); ``` None <!-- Help reviewers and the release process by writing your own release notes **INTERNAL and MINOR tagged notes will not be included in the next version's final release notes.** CATEGORY [----------] TYPE [ CLI ] [-------------] LOCATION [ DOCS ] [ BREAKING ] [-------------] [ GENERAL ] [ BUGFIX ] [-{Component}-] [ INTERNAL ] [ ENHANCEMENT ] [ {File} ] [ IOS ] [ FEATURE ] [ {Directory} ] |-----------| [ ANDROID ] [ MINOR ] [ {Framework} ] - | {Message} | [----------] [-------------] [-------------] |-----------| [CATEGORY] [TYPE] [LOCATION] - MESSAGE EXAMPLES: [IOS] [BREAKING] [FlatList] - Change a thing that breaks other things [ANDROID] [BUGFIX] [TextInput] - Did a thing to TextInput [CLI] [FEATURE] [local-cli/info/info.js] - CLI easier to do things with [DOCS] [BUGFIX] [GettingStarted.md] - Accidentally a thing/word [GENERAL] [ENHANCEMENT] [Yoga] - Added new yoga thing/position [INTERNAL] [FEATURE] [./scripts] - Added thing to script that nobody will see --> [ANDROID] [FEATURE] [Networking] Add HTTP caching Pull Request resolved: #18348 Differential Revision: D13680430 Pulled By: hramos fbshipit-source-id: 1e49cf75702db72b20a316e9cdd971605f8fe7e0
1 parent ec887c2 commit 26793a0

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

ReactAndroid/src/main/java/com/facebook/react/modules/network/NetworkingModule.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public interface ResponseHandler {
163163
* @param context the ReactContext of the application
164164
*/
165165
public NetworkingModule(final ReactApplicationContext context) {
166-
this(context, null, OkHttpClientProvider.createClient(), null);
166+
this(context, null, OkHttpClientProvider.createClient(context), null);
167167
}
168168

169169
/**
@@ -174,7 +174,7 @@ public NetworkingModule(final ReactApplicationContext context) {
174174
public NetworkingModule(
175175
ReactApplicationContext context,
176176
List<NetworkInterceptorCreator> networkInterceptorCreators) {
177-
this(context, null, OkHttpClientProvider.createClient(), networkInterceptorCreators);
177+
this(context, null, OkHttpClientProvider.createClient(context), networkInterceptorCreators);
178178
}
179179

180180
/**
@@ -183,7 +183,7 @@ public NetworkingModule(
183183
* caller does not provide one explicitly
184184
*/
185185
public NetworkingModule(ReactApplicationContext context, String defaultUserAgent) {
186-
this(context, defaultUserAgent, OkHttpClientProvider.createClient(), null);
186+
this(context, defaultUserAgent, OkHttpClientProvider.createClient(context), null);
187187
}
188188

189189
@Override

ReactAndroid/src/main/java/com/facebook/react/modules/network/OkHttpClientProvider.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,19 @@
77

88
package com.facebook.react.modules.network;
99

10+
import android.content.Context;
1011
import android.os.Build;
1112

1213
import com.facebook.common.logging.FLog;
1314

15+
import java.io.File;
1416
import java.util.ArrayList;
1517
import java.util.List;
1618
import java.util.concurrent.TimeUnit;
1719

1820
import javax.annotation.Nullable;
1921

22+
import okhttp3.Cache;
2023
import okhttp3.ConnectionSpec;
2124
import okhttp3.OkHttpClient;
2225
import okhttp3.TlsVersion;
@@ -57,6 +60,13 @@ public static OkHttpClient createClient() {
5760
return createClientBuilder().build();
5861
}
5962

63+
public static OkHttpClient createClient(Context context) {
64+
if (sFactory != null) {
65+
return sFactory.createNewNetworkModuleClient();
66+
}
67+
return createClientBuilder(context).build();
68+
}
69+
6070
public static OkHttpClient.Builder createClientBuilder() {
6171
// No timeouts by default
6272
OkHttpClient.Builder client = new OkHttpClient.Builder()
@@ -68,6 +78,24 @@ public static OkHttpClient.Builder createClientBuilder() {
6878
return enableTls12OnPreLollipop(client);
6979
}
7080

81+
public static OkHttpClient.Builder createClientBuilder(Context context) {
82+
int cacheSize = 10 * 1024 * 1024; // 10 Mo
83+
return createClientBuilder(context, cacheSize);
84+
}
85+
86+
public static OkHttpClient.Builder createClientBuilder(Context context, int cacheSize) {
87+
OkHttpClient.Builder client = createClientBuilder();
88+
89+
if (cacheSize == 0) {
90+
return client;
91+
}
92+
93+
File cacheDirectory = new File(context.getCacheDir(), "http-cache");
94+
Cache cache = new Cache(cacheDirectory, cacheSize);
95+
96+
return client.cache(cache);
97+
}
98+
7199
/*
72100
On Android 4.1-4.4 (API level 16 to 19) TLS 1.1 and 1.2 are
73101
available but not enabled by default. The following method

0 commit comments

Comments
 (0)