mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-05-29 00:41:07 +08:00
Enhance image freshness check before stored into cache (#23226)
Summary:
Currently, before we store the image to cache, we only respect `Cache-Control`, actually, we also may need to check `Expires`、`Last-Modified`, refer to [MDN docs](https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching#Freshness), and [okhttp](568a91c44a/okhttp/src/main/java/okhttp3/internal/cache/CacheStrategy.java (L268)) respect the `MDN`, so in iOS, we can also respect this.
[iOS] [Fixed] - Respect `MDN` cache strategy before cache the image.
After `PR`, if image's response do not contains `Cache-Control`, it would also checks `Expires`、`Last-Modified` refers to [MDN docs](https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching#Freshness).
Pull Request resolved: https://github.com/facebook/react-native/pull/23226
Differential Revision: D13895627
Pulled By: cpojer
fbshipit-source-id: aa377511c31badd752d7887ed6cbcdf6be4b80b3
This commit is contained in:
committed by
Facebook Github Bot
parent
e405e84fc3
commit
e98d5a289a
@@ -106,33 +106,49 @@ static NSString *RCTCacheKeyForImage(NSString *imageTag, CGSize size, CGFloat sc
|
||||
size:(CGSize)size
|
||||
scale:(CGFloat)scale
|
||||
resizeMode:(RCTResizeMode)resizeMode
|
||||
responseDate:(NSString *)responseDate
|
||||
cacheControl:(NSString *)cacheControl
|
||||
response:(NSURLResponse *)response
|
||||
{
|
||||
NSString *cacheKey = RCTCacheKeyForImage(url, size, scale, resizeMode);
|
||||
BOOL shouldCache = YES;
|
||||
NSDate *staleTime;
|
||||
NSArray<NSString *> *components = [cacheControl componentsSeparatedByString:@","];
|
||||
for (NSString *component in components) {
|
||||
if ([component containsString:@"no-cache"] || [component containsString:@"no-store"] || [component hasSuffix:@"max-age=0"]) {
|
||||
shouldCache = NO;
|
||||
break;
|
||||
} else {
|
||||
NSRange range = [component rangeOfString:@"max-age="];
|
||||
if (range.location != NSNotFound) {
|
||||
NSInteger seconds = [[component substringFromIndex:range.location + range.length] integerValue];
|
||||
NSDate *originalDate = [self dateWithHeaderString:responseDate];
|
||||
staleTime = [originalDate dateByAddingTimeInterval:(NSTimeInterval)seconds];
|
||||
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
|
||||
NSString *cacheKey = RCTCacheKeyForImage(url, size, scale, resizeMode);
|
||||
BOOL shouldCache = YES;
|
||||
NSString *responseDate = ((NSHTTPURLResponse *)response).allHeaderFields[@"Date"];
|
||||
NSDate *originalDate = [self dateWithHeaderString:responseDate];
|
||||
NSString *cacheControl = ((NSHTTPURLResponse *)response).allHeaderFields[@"Cache-Control"];
|
||||
NSDate *staleTime;
|
||||
NSArray<NSString *> *components = [cacheControl componentsSeparatedByString:@","];
|
||||
for (NSString *component in components) {
|
||||
if ([component containsString:@"no-cache"] || [component containsString:@"no-store"] || [component hasSuffix:@"max-age=0"]) {
|
||||
shouldCache = NO;
|
||||
break;
|
||||
} else {
|
||||
NSRange range = [component rangeOfString:@"max-age="];
|
||||
if (range.location != NSNotFound) {
|
||||
NSInteger seconds = [[component substringFromIndex:range.location + range.length] integerValue];
|
||||
staleTime = [originalDate dateByAddingTimeInterval:(NSTimeInterval)seconds];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (shouldCache) {
|
||||
if (staleTime) {
|
||||
@synchronized(_cacheStaleTimes) {
|
||||
_cacheStaleTimes[cacheKey] = staleTime;
|
||||
if (shouldCache) {
|
||||
if (!staleTime && originalDate) {
|
||||
NSString *expires = ((NSHTTPURLResponse *)response).allHeaderFields[@"Expires"];
|
||||
NSString *lastModified = ((NSHTTPURLResponse *)response).allHeaderFields[@"Last-Modified"];
|
||||
if (expires) {
|
||||
staleTime = [self dateWithHeaderString:expires];
|
||||
} else if (lastModified) {
|
||||
NSDate *lastModifiedDate = [self dateWithHeaderString:lastModified];
|
||||
if (lastModifiedDate) {
|
||||
NSTimeInterval interval = [originalDate timeIntervalSinceDate:lastModifiedDate] / 10;
|
||||
staleTime = [originalDate dateByAddingTimeInterval:interval];
|
||||
}
|
||||
}
|
||||
}
|
||||
if (staleTime) {
|
||||
@synchronized(_cacheStaleTimes) {
|
||||
_cacheStaleTimes[cacheKey] = staleTime;
|
||||
}
|
||||
}
|
||||
return [self addImageToCache:image forKey:cacheKey];
|
||||
}
|
||||
return [self addImageToCache:image forKey:cacheKey];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user