Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDWebImage: After adding .refreshCached as option, get is being called without request headers If-None-Match and If-Modified-Since. How to cache those as well from response http headers and send in request http headers? #3589

Open
srk-mnnit opened this issue Aug 24, 2023 · 14 comments
Labels

Comments

@srk-mnnit
Copy link

Issue Description and Steps

As per my understanding it must be adding http header and we'll get 304 (not modified in response).

@dreampiggy
Copy link
Contributor

dreampiggy commented Aug 25, 2023

Try SDWebImageRefreshCached ?

https://sdwebimage.github.io/documentation/sdwebimage/sdwebimageoptions/refreshcached

The HTTP E-Tag based is using NSURLSession implementation (So, the NSURLRequest's cachePolicy must set to useProtocolCachePolicy)

The default options will change cache policy to reloadIgnoringLocalCacheData, so this cause the HTTP cache system not work.

See more: #2904

@srk-mnnit
Copy link
Author

Simply used

imageView.sd_setImage(
         with: url,
         placeholderImage: UIImage(named: .defaultImage),
         options: .refreshCached
)

there's nothing like If-Modified-Since and If-None-Match in request header, and all the time i'm getting 200 status code in response what i can see through cocoadebug.

@srk-mnnit
Copy link
Author

i hardcoded header in AppDelegate to check

SDWebImageDownloader.shared.setValue("Tue, 09 Jan 2024 09:28:33 GMT", forHTTPHeaderField: "If-Modified-Since")
SDWebImageDownloader.shared.setValue("\"39c4e99964d5e9b69149ab5854d94c3e\"", forHTTPHeaderField: "If-None-Match")

and i'm getting 304 for this.

@dreampiggy can you help me with this for all cached url data through library?

@dreampiggy
Copy link
Contributor

If you add refreshCached. URLSession (from Apple) will set this header

You'll may see twice callback during a single image loading. One from the URLSession and which may hit 304, one from our local cache check.

@srk-mnnit
Copy link
Author

By default, requests are not being sent with If-Modified-Since and If-None-Match headers to the server due to which we are not getting 304. But we expect the Status Code to be 304 if Etag matches, how can we achieve this with SDWebImage library.

@srk-mnnit
Copy link
Author

Additionally, if i want to override header for each image request, how to get response header including etag and last-modified from cache?

@dreampiggy
Copy link
Contributor

dreampiggy commented Jan 15, 2024

Use requestModifier, it's per-image-request
Use responseModifier for response

@srk-mnnit
Copy link
Author

Does SDWebImage store headers along with image data in cache?

@dreampiggy
Copy link
Contributor

dreampiggy commented Jan 16, 2024

Does SDWebImage store headers along with image data in cache?

No.

But you can customize.

See that UIImage.sd_extendedObject, which is used to store a medata defined by user, to the disk cache, for the same cache key

Which use Xattr for backend.

@dreampiggy
Copy link
Contributor

There is one documentation, I've already added some details explain about some features.

https://sdwebimage.github.io/documentation/sdwebimage/

The sd_extendedObject (Seems the extension on UIKit type does not listed in documentation) :

 Read and Write the extended object and bind it to the image. Which can hold some extra metadata like Image's scale factor, URL rich link, date, etc.
 The extended object should conforms to NSCoding, which we use `NSKeyedArchiver` and `NSKeyedUnarchiver` to archive it to data, and write to disk cache.
 @note The disk cache preserve both of the data and extended data with the same cache key. For manual query, use the `SDDiskCache` protocol method `extendedDataForKey:` instead.
 @note You can specify arbitrary object conforms to NSCoding (NSObject protocol here is used to support object using `NS_ROOT_CLASS`, which is not NSObject subclass). If you load image from disk cache, you should check the extended object class to avoid corrupted data.
 @warning This object don't need to implements NSSecureCoding (but it's recommended),  because we allows arbitrary class.

@dreampiggy
Copy link
Contributor

dreampiggy commented Jan 16, 2024

#3589 (comment)

Which means:

Inside your response modifier, Set the sd_extendedObject of downloaded image

Then the extended object will be written to disk cache via Xattr. For the same cache key.

Next time you can use SDImageCache or SDDiskCache to query the extended object

@srk-mnnit
Copy link
Author

Thank you @dreampiggy

@srk-mnnit
Copy link
Author

Is it possible for this header to be internally included in upcoming updates of SDWebImage when adding refreshCached as an option?

@dreampiggy
Copy link
Contributor

dreampiggy commented Jan 17, 2024

Is it possible for this header to be internally included in upcoming updates of SDWebImage when adding refreshCached as an option?

I don't know actually. And have no plan, since I'll not work on iOS SDK (espacially network part) and focus on some other scope like Compiler currently.

The current refreshCache option behavior is not implemented by SDWebImage actually.

We throw all the cache related logic into URLSession and Apple's SDK...

If URLSession does not implements HTTP Cache control, or has bugs on some firmware version, it will broken.

About Feature request

I guess you means that all the refreshCache should be implements from the scratch without rely on URLSession's own cache control. There are already some talk, but seems no one create PRs for this.

You're welcome to create implementation (actually this is a standalone feature, which don't need SDWebImage any code, you can create a pure Demo using URLSession for that 304 status code handling and using HTTP HEAD method. like https://developer.apple.com/forums/thread/50586)

Maybe AFNetworking (Alamofire) has the similar demo ?

See #2904

@dreampiggy dreampiggy added the help wanted Extra attention is needed label Jan 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants