Skip to content

Commit b92d084

Browse files
committed
fix(android): improved cache control interceptor
1 parent 53372cb commit b92d084

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

packages/https/platforms/android/java/com/nativescript/https/CacheInterceptor.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,22 @@ public class CacheInterceptor {
1010
Request originalRequest = chain.request();
1111
String cacheControlHeader = originalRequest.header("Cache-Control");
1212
Response originalResponse = chain.proceed(originalRequest);
13-
if (cacheControlHeader != null) {
14-
return originalResponse.newBuilder().header("Cache-Control", cacheControlHeader).removeHeader("Pragma").build();
15-
} else {
13+
14+
// Only touch GET responses
15+
if (!"GET".equalsIgnoreCase(originalRequest.method())) {
1616
return originalResponse;
1717
}
18+
19+
String respCc = originalResponse.header("Cache-Control");
20+
// If server didn't provide caching or forbids it, add a safe fallback
21+
if (cacheControlHeader != null && (respCc == null || respCc.isEmpty() || respCc.contains("no-store") || respCc.contains("no-cache"))) {
22+
// revalidate on network (max-age=0) but allow stale fallback if error
23+
// stale-if-error may not be honored by everything but OkHttp will cache, enabling our fallback
24+
return originalResponse.newBuilder()
25+
.removeHeader("Pragma")
26+
.header("Cache-Control", cacheControlHeader)
27+
.build();
28+
}
29+
return originalResponse;
1830
};
1931
}

0 commit comments

Comments
 (0)